INFO 153B/253B: Backend Web Architecture
Kay Ashaolu
Pros
Cons
CREATE TABLE links (
long_url VARCHAR(1000) NOT NULL,
short_url VARCHAR(20) NOT NULL,
hit_count BIGINT DEFAULT 0,
created DATE
);
Pros
Cons
def put(key, value):
pass
def get(key):
return value
Pros
Cons
{
"long_url": "http://www.google.com",
"short_url": "qwelmw",
"hit_count": 2
}
docker create network my-network
(within container: http://quote-service-container/quote/sunday)
version: "3.7"
services:
db:
image: postgres:latest
volumes:
- postgres-volume:/var/lib/postgresql/data
- ./init.db.sql:/docker-entrypoint-initdb.d/init.db.sql
docker-compose up
# spins up all defined containers, images, networks, and containers in yaml file
docker-compse down
# shuts down all defined containers, images, networks, and containers in yaml file
docker-compose down --rmi all -v --remove-orphans
# shuts down and deletes all defined containers, images, networks, and
# containers in yaml file. Use this if you are editing code and want to restart
# your containers
version: "3.7"
services:
db:
image: postgres:latest
volumes:
- postgres-volume:/var/lib/postgresql/data
- ./init.db.sql:/docker-entrypoint-initdb.d/init.db.sql
environment:
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: quote-db
quote-service:
build: ./quote-service/
image: quote-service-image
ports:
- "5050:5000"
volumes:
postgres-volume: {}
CREATE TABLE IF NOT EXISTS quotes (
day_of_week VARCHAR(20) NOT NULL,
quote VARCHAR(2000) NOT NULL
);
INSERT INTO quotes (day_of_week, quote) VALUES
('sunday', 'Life is about making an impact, not making an income. -Kevin Kruse'),
('monday', 'Whatever the mind of man can conceive and believe, it can achieve. -Napoleon Hill'),
('tuesday', 'Strive not to be a success, but rather to be of value. -Albert Einstein'),
('wednesday', 'You miss 100% of the shots you dont take. -Wayne Gretzky'),
('thursday', 'Every strike brings me closer to the next home run. -Babe Ruth'),
('friday', 'We become what we think about. -Earl Nightingale'),
('saturday', 'Life is what happens to you while you are busy making other plans. -John Lennon');
from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import text
import json
app = Flask(__name__)
db = SQLAlchemy()
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:postgres@db:5432/quote-db"
db.init_app(app)
def gen_response(day_of_week):
if not day_of_week:
response = {"message": "We need the day_of_week in order to send a quote"}
response_code = 400
else:
stmt = text("SELECT quote FROM quotes WHERE day_of_week = :x")
stmt = stmt.bindparams(x=day_of_week)
result = db.session.execute(stmt).all()
if not result:
response = {"message": "Sorry we don't know that day of the week" }
response_code = 404
else:
response = {"day": day_of_week, "quote": result[0].quote}
response_code = 200
return json.dumps(response), response_code
@app.route('/quote/<day_of_week>')
def quote_of_the_day(day_of_week):
return gen_response(day_of_week)
@app.route('/quote', methods = ['GET'])
@app.route('/quote/', methods = ['GET'])
def quote_of_the_day_get():
day_of_week = request.args.get("day_of_week")
return gen_response(day_of_week)
@app.route('/quote', methods = ['POST'])
def quote_of_the_day_post():
data = request.get_json()
day_of_week = data.get("day_of_week")
return gen_response(day_of_week)