Skip to content

Commit

Permalink
Finished project
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsf1 committed Apr 4, 2021
1 parent 8ddace1 commit 742e95c
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PYTHONUNBUFFERED=True
PYTHONUNBUFFERED=True
6 changes: 4 additions & 2 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
api: python3 -m bottle --bind=localhost:$PORT --debug --reload user
api: python3 -m bottle --bind=localhost:$PORT --debug --reload timeline
gateway: python3 -m bottle --bind=localhost:$PORT --debug --reload gateway
users: python3 -m bottle --bind=localhost:$PORT --debug --reload user
timelines: python3 -m bottle --bind=localhost:$PORT --debug --reload timeline
# foreman start -m gateway=1,users=1,timelines=3
24 changes: 24 additions & 0 deletions etc/gateway.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#reverse proxy
[proxy]
upstreams = [
"http://localhost:5200"
]
#These are tree instances for the load balancer to direct the users requests
[users]
userports = [
"http://localhost:5100",
"http://localhost:5101",
"http://localhost:5102"
]
#These are tree instances for the load balancer to direct the timeline requests
[timeline]
timelineports = [
"http://localhost:5000",
"http://localhost:5001",
"http://localhost:5002"
]

[logging]
config = logging.ini
requests = true

37 changes: 37 additions & 0 deletions etc/logging.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Simple logging configuration for Python
#
# NOTE: Don't add spaces between comma-separated lists in this file
#

[DEFAULT]
filename = 'gateway.log'

[loggers]
keys = root

[logger_root]
level = DEBUG
handlers = console,logfile

[handlers]
keys = console,logfile

[handler_console]
class = StreamHandler
args = (sys.stderr,)
formatter = simple

[handler_logfile]
class = FileHandler
args = (%(filename)s,)
formatter = dated

[formatters]
keys = simple,dated

[formatter_simple]
format = %(levelname)s in %(name)s: %(message)s

[formatter_dated]
format = [%(asctime)s] %(levelname)s in %(name)s: %(message)s
4 changes: 4 additions & 0 deletions etc/timeline.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[sqlite]
dbfile2 = ./timeline.db
commit = auto

5 changes: 5 additions & 0 deletions etc/user.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[sqlite]
dbfile = ./user.db
#dbfile2 = ./timeline.db
commit = auto

3 changes: 2 additions & 1 deletion gateway.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ timelineports = [

[logging]
config = logging.ini
requests = true
requests = true

58 changes: 54 additions & 4 deletions gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
import logging.config

import bottle
from bottle import route, request, response
from bottle import route, request, response, get, auth_basic



import requests
from requests import get as retrieve
# from requests.api import get as gets


# Allow JSON values to be loaded from app.config[key]
Expand All @@ -26,9 +30,9 @@ def json_config(key):
# Set up app and logging
#
app = bottle.default_app()
app.config.load_config('gateway.ini')
app.config.load_config('./etc/gateway.ini')

logging.config.fileConfig(app.config['logging.config'])
#logging.config.fileConfig(app.config['logging.config'])

userPortList = json_config('users.userports')
# print("This is the user Port List: ", userPortList)
Expand Down Expand Up @@ -81,6 +85,31 @@ def json_error_handler(res):



# def auth_required(f):
# @wraps(f)
# def decorated(*args, **kwargs):
# auth = request.authorization
# if auth and auth.username == 'username' and auth.password == 'password':
# return f(*args, **kwargs)

# return response("Could not verify your login", 401, {'WWW-Authenticate' : 'Basic realm="Login Required'})
# return decorated

def is_authenticated_user(user, password):
pathForCheckPassword = f'/user/{user}/{password}'
checkPasswordURL = retrieve(urlPathHelper5100(pathForCheckPassword)).json()

for key, value in checkPasswordURL.items():
keyCheckpassword = value


if keyCheckpassword == 'true':
return True
else:
return False




n = -1
i = -1
Expand All @@ -95,7 +124,27 @@ def roundRobinCycle(list):
return list[i % len(list)]



def urlPathHelper5200(path):
# return 'http://127.0.0.1:5000' + path
return "http://localhost:5200" + path

def urlPathHelper5100(path):
# return 'http://127.0.0.1:5100' + path
return "http://localhost:5100" + path

@get('/home/<username>')
def usernameHome(username):
pathForUserService = f'/user/{username}/follower/list'
userIDList = retrieve(urlPathHelper5100(pathForUserService)).json()
ids = userIDList['followerList']
timelineList = []
for id in ids:
print(type(id))
for id in ids:
pathForTimelineService = f'/timeline/{str(id)}'
timeline = retrieve(urlPathHelper5200(pathForTimelineService)).json()
timelineList.append(timeline)
return {"list": timelineList}

@route('<url:re:.*>', method='ANY')
def gateway(url):
Expand Down Expand Up @@ -162,3 +211,4 @@ def gateway(url):
continue
response.set_header(name, value)
return upstream_response.content

12 changes: 12 additions & 0 deletions timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ def execute(db, sql, args=()):
#user, posts, timeline
######################Routes#####################

@get('/timeline/<id:int>')
def userTimelineFromID(id, db):
userPost = query(db, 'SELECT author, text, postID, timestamp, userID FROM posts WHERE userID = ? ORDER BY postID desc LIMIT 25', [id])
if not userPost:
abort(404)
print(userPost)
# userTimeline = userPost.reverse()
return {'user_timeline': userPost}




#http GET localhost:5100/timeline/Alfonso
@get('/timeline/<username>')
def getUserTimeline(username, db):
Expand Down
14 changes: 14 additions & 0 deletions user.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ def addFollower(db):

return addingFollower

@get('/user/<username>/follower/list')
def followingList(username, db):
userID = query(db, 'SELECT userID from users WHERE username = ?', [username], one=True)
if not userID:
abort(404)

followingUserDict = query(db, 'SELECT followerID from followers WHERE userID = ?', [userID['userID']])
followingUserList = []

for item in followingUserDict:
followingUserList.append(item['followerID'])

return {'followerList':followingUserList}



@delete("/user/follower/remove")
Expand Down

0 comments on commit 742e95c

Please sign in to comment.