Skip to content

Commit 4d66fd4

Browse files
committed
Started repository handling, removed app.run and app configuration values and separated it into its own script
1 parent 3267970 commit 4d66fd4

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

app.py

+61-14
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ def init(sanic, loop):
2727
async def auth_handler(request):
2828
"""Handles authentication requests"""
2929
req = request.json
30-
if not req: raise exc.InvalidUsage("Bad request")
30+
if not req:
31+
raise exc.InvalidUsage("Bad request")
3132

3233
# Ensure required data is included in the request
3334
username = req.get('username')
3435
password = req.get('password')
35-
if not (username and password): raise exc.InvalidUsage("Bad request")
36+
if not (username and password):
37+
raise exc.InvalidUsage("Bad request")
3638

3739
# Ensure user exists in database
3840
user = await db['users'].find_one({ "username": username })
@@ -57,19 +59,23 @@ async def auth_handler(request):
5759
async def new_user_handler(request):
5860
"""Handles requests for new users"""
5961
req = request.json
60-
if not req: raise exc.InvalidUsage("Bad request")
62+
if not req:
63+
raise exc.InvalidUsage("Bad request")
6164

6265
# Ensure required data is included in the request
6366
username = req.get('username')
6467
email = req.get('email')
6568
password = req.get('password')
66-
if not (username and email and password): raise exc.InvalidUsage("Bad request")
69+
if not (username and email and password):
70+
raise exc.InvalidUsage("Bad request")
6771

6872
# Ensure user does not already exist in database
6973
user = await db['users'].find_one({ "username": username })
70-
if user is not None: return res.json({ "message": "A user with this username already exists", "status": 409 })
74+
if user is not None:
75+
return res.json({ "message": "A user with this username already exists", "status": 409 })
7176
user = await db['users'].find_one({ "email": email })
72-
if user is not None: return res.json({ "message": "A user with this email already exists", "status": 409 })
77+
if user is not None:
78+
return res.json({ "message": "A user with this email already exists", "status": 409 })
7379

7480
# Hash password
7581
hashed_pass = ph.hash(password)
@@ -93,14 +99,55 @@ async def new_user_handler(request):
9399
"token": token
94100
})
95101

102+
@app.route('/api/user/<user_id:int>', methods=['GET', 'POST'])
103+
async def user_handler(req, user_id):
104+
"""TODO Handles requests for existing users"""
105+
if not user_id:
106+
raise exc.InvalidUsage("Bad request")
107+
#if req.method == 'GET':
108+
raise exc.NotFound("Soon™")
109+
110+
@app.route('/api/repo', methods=['POST'])
111+
async def new_repo_handler(req):
112+
"""TODO New repo"""
113+
raise exc.NotFound("Soon™")
114+
115+
# Existing repo
116+
@app.route('/api/repo/<repo_id:int>', methods=['GET', 'POST', 'DELETE'])
117+
async def repo_handler(req, repo_id):
118+
"""Handles requests for existing repositories"""
119+
if not repo_id:
120+
raise exc.InvalidUsage("Bad request")
121+
122+
# Get repository
123+
if req.method == 'GET':
124+
# TODO auth check
125+
126+
repo = await db['repos'].find_one({ "_id": repo_id })
127+
if not repo:
128+
raise exc.NotFound("Resource not found")
129+
130+
# Temporary confirmation
131+
return res.json({ "message": f"You've requested repository ID {repo_id}" })
132+
133+
# Update repository
134+
elif req.method == 'POST':
135+
repo = await db['repos'].find_one({ "_id": repo_id })
136+
if not repo:
137+
raise exc.Forbidden("Repository doesn't exist")
138+
else:
139+
# TODO Update repo
140+
pass
141+
142+
# Delete repository
143+
elif req.method == 'DELETE':
144+
repo = await db['repos'].find_one({ "_id": repo_id })
145+
if not repo:
146+
raise exc.Forbidden("Repository doesn't exist")
147+
else:
148+
return res.json({ "message": "testing" })
149+
96150
@app.exception(exc.SanicException)
97151
def errors(request, exception):
98152
"""Handles errors"""
99-
return res.json({ "error": exception.args[0], "status": exception.status_code })
100-
101-
if __name__ == "__main__":
102-
app.run(
103-
host=config.get('app_host', '0.0.0.0'),
104-
port=config.get('app_port', 80),
105-
debug=True
106-
)
153+
return res.json({ "error": exception.args[0], "status": exception.status_code })

config.json

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"app_host": "localhost",
3-
"app_port": 8080,
42
"mongo_host": "localhost",
53
"mongo_port": 27017,
64
"mongo_db_name": "codeecho"

runserver.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3.6 -m sanic app.app --host 0.0.0.0 --port 8080

0 commit comments

Comments
 (0)