forked from namukang/email-bin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
59 lines (49 loc) · 1.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from gevent import monkey; monkey.patch_all()
from gevent.wsgi import WSGIServer
import os, datetime, urlparse, re
import logging
logging.basicConfig()
from pymongo import Connection
from flask import Flask, request, Response
from access_control import crossdomain
EMAIL_REGEX = re.compile(r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}')
# domains allowed to invoke the XMLHttpRequest API
#PRD
#ALLOWED_DOMAINS = ['stationfy.com']
#DEV
ALLOWED_DOMAINS = ['*']
app = Flask(__name__)
@app.route('/signup', methods=['POST'])
@crossdomain(origin=ALLOWED_DOMAINS)
def signup():
email = request.form['email']
user_type = request.form['user_type']
if email and re.match(EMAIL_REGEX, email):
signup = {
'email': email,
'ip': request.access_route[0],
'time': datetime.datetime.utcnow(),
'type' : user_type
}
app.database.signups.insert(signup)
return Response("Thanks for signing up!", status=201)
else:
return Response("Sorry, your email address is invalid.", status=400)
@app.route('/healthcheck', methods=['GET'])
@crossdomain(origin=ALLOWED_DOMAINS)
def healthcheck():
count = app.database.signups.find().count()
return Response("MongoDB ok.", status=200)
def connect_to_db():
"""Connect to database"""
MONGOLAB_URI = os.environ['MONGOLAB_URI']
MONGODB_HOST = urlparse.urlparse(MONGOLAB_URI).geturl()
MONGODB_PORT = urlparse.urlparse(MONGOLAB_URI).port
DATABASE_NAME = urlparse.urlparse(MONGOLAB_URI).path[1:]
connection = Connection(MONGODB_HOST, MONGODB_PORT)
app.database = connection[DATABASE_NAME]
if __name__ == '__main__':
connect_to_db()
port = int(os.environ.get('PORT', 5000))
http_server = WSGIServer(('', port), app)
http_server.serve_forever()