File tree Expand file tree Collapse file tree 11 files changed +205
-0
lines changed Expand file tree Collapse file tree 11 files changed +205
-0
lines changed Original file line number Diff line number Diff line change
1
+ # python-web-api-flask-traefic-postgres-pop
2
+
3
+ ## Description
4
+ Creates an api of ` pop ` for a flask project.
5
+ Has the ability to query by parameters.
6
+ If path is not found, will default to 404 error.
7
+ Uses self-signed ssl.
8
+
9
+ ## Tech stack
10
+ - python
11
+ - flask
12
+ - postgres
13
+ - reverse proxy
14
+ - ssl
15
+
16
+ ## Docker stack
17
+ - alpine: edge
18
+ - python: latest
19
+ - postgresql
20
+ - traefik: v2 .4
21
+
22
+ ## To run
23
+ ` sudo ./install.sh -u `
24
+ - Endpoint
25
+ - [ Availble] ( https://myapi.docker.localhost/pop )
26
+
27
+ ## To stop (optional)
28
+ ` sudo ./install.sh -d `
29
+
30
+ ## For help
31
+ ` sudo ./install.sh -h `
32
+
33
+ ## Credit
34
+ - https://stackabuse.com/using-sqlalchemy-with-flask-and-postgresql/
Original file line number Diff line number Diff line change
1
+ CREATE SEQUENCE pop_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1 ;
2
+
3
+ CREATE TABLE "public "." pop" (
4
+ " id" integer DEFAULT nextval(' pop_id_seq' ) NOT NULL ,
5
+ " name" text NOT NULL ,
6
+ " color" text NOT NULL
7
+ ) WITH (oids = false);
Original file line number Diff line number Diff line change
1
+ INSERT INTO " public" ." pop" (name, color)
2
+ VALUES
3
+ (' RC Cola' , ' brown' ),
4
+ (' Sprite' , ' clear' ),
5
+ (' Verners' , ' brown' ),
6
+ (' Mt. Lightening' , ' green' );
Original file line number Diff line number Diff line change
1
+ version : " 3"
2
+
3
+ services :
4
+ db :
5
+ image : postgres:alpine
6
+ environment :
7
+ - POSTGRES_DB=beverage
8
+ - POSTGRES_USER=maria
9
+ - POSTGRES_HOST_AUTH_METHOD=trust
10
+ volumes :
11
+ - ./db/sql:/docker-entrypoint-initdb.d
12
+
13
+ py-srv :
14
+ build : py-srv
15
+ command : python app.py
16
+ ports :
17
+ - " 5000"
18
+ labels :
19
+ - traefik.enable=true
20
+ - traefik.http.routers.myapp.tls=true
21
+ - traefik.http.routers.myapp.rule=Host("myapi.docker.localhost")
22
+
23
+ traefik :
24
+ image : " traefik:v2.4"
25
+ container_name : " traefik"
26
+ labels :
27
+ - " traefik.enable=true"
28
+ - " traefik.http.routers.traefik=true"
29
+ ports :
30
+ # HTTPS / SSL port
31
+ - " 443:443"
32
+ # The Traefik Web UI port (enabled by api:insecure: true in traefik.yml)
33
+ - " 8080:8080"
34
+ volumes :
35
+ - ./traefik/traefik.yml:/etc/traefik/traefik.yml:ro
36
+ - ./traefik/config.yml:/etc/traefik/config.yml:ro
37
+ - ./traefik/cert:/etc/certs:ro
38
+ - /var/run/docker.sock:/var/run/docker.sock:ro
Original file line number Diff line number Diff line change
1
+ [2021-05-14 10:31:53 INFO]: install::setup-logging ended
2
+ ================
3
+ [2021-05-14 10:31:53 INFO]: install::start-up started
4
+ [2021-05-14 10:31:53 INFO]: install::start-up running image
5
+ [2021-05-14 10:31:53 INFO]: install::start-up ended
6
+ ================
Original file line number Diff line number Diff line change
1
+ FROM alpine:edge
2
+
3
+ WORKDIR /cert
4
+
5
+ RUN apk upgrade --update-cache --available && \
6
+ apk add openssl && \
7
+ rm -rf /var/cache/apk/*
8
+
9
+ RUN openssl req -new -text -passout pass:abcd -subj /CN=localhost -out server.req -keyout privkey.pem
10
+
11
+ RUN openssl rsa -in privkey.pem -passin pass:abcd -out server.key
12
+
13
+ RUN openssl req -x509 -in server.req -text -key server.key -out server.crt
14
+
15
+ RUN chmod -R +x * && ls
16
+
17
+ CMD ["pwd" ]
Original file line number Diff line number Diff line change
1
+
2
+ FROM python:latest
3
+
4
+ COPY bin/ /app
5
+
6
+ WORKDIR /app
7
+
8
+ RUN pip install -r requirements.txt
Original file line number Diff line number Diff line change
1
+ from flask import Flask
2
+ from flask_sqlalchemy import SQLAlchemy
3
+
4
+ app = Flask (__name__ )
5
+ app .config ['SQLALCHEMY_DATABASE_URI' ] = "postgresql://maria:pass@db:5432/beverage"
6
+ app .config ['SQLALCHEMY_TRACK_MODIFICATIONS' ] = False
7
+ db = SQLAlchemy (app )
8
+
9
+ class PopModel (db .Model ):
10
+ __tablename__ = 'pop'
11
+
12
+ id = db .Column (db .Integer , primary_key = True )
13
+ name = db .Column (db .String ())
14
+ color = db .Column (db .String ())
15
+
16
+ def __init__ (self , name , color ):
17
+ self .name = name
18
+ self .color = color
19
+
20
+ @app .route ('/pop' )
21
+ def handle_beverage ():
22
+ pops = PopModel .query .all ()
23
+ results = [
24
+ {
25
+ "name" : pop .name ,
26
+ "color" : pop .color
27
+ } for pop in pops ]
28
+
29
+ return {"count" : len (results ), "pop" : results , "message" : "success" }
30
+
31
+
32
+ @app .errorhandler (404 )
33
+ def page_not_found (e ):
34
+ return "<h1>404</h1><p>The resource could not be found.</p>" , 404
35
+
36
+ if __name__ == "__main__" :
37
+ app .run (host = '0.0.0.0' , port = 5000 , debug = True )
Original file line number Diff line number Diff line change
1
+ Flask
2
+ Flask-SQLAlchemy
3
+ psycopg2
Original file line number Diff line number Diff line change
1
+ http :
2
+ routers :
3
+ traefik :
4
+ rule : " Host(`traefik.docker.localhost`)"
5
+ service : " api@internal"
6
+ tls :
7
+ domains :
8
+ - main : " docker.localhost"
9
+ sans :
10
+ - " *.docker.localhost"
11
+ - main : " domain.local"
12
+ sans :
13
+ - " *.domain.local"
14
+
15
+ tls :
16
+ certificates :
17
+ - certFile : " /etc/certs/server.crt"
18
+ keyFile : " /etc/certs/server.key"
Original file line number Diff line number Diff line change
1
+ global :
2
+ sendAnonymousUsage : false
3
+
4
+ api :
5
+ dashboard : true
6
+ insecure : true
7
+
8
+ providers :
9
+ docker :
10
+ endpoint : " unix:///var/run/docker.sock"
11
+ watch : true
12
+ exposedByDefault : false
13
+
14
+ file :
15
+ filename : /etc/traefik/config.yml
16
+ watch : true
17
+
18
+ log :
19
+ level : INFO
20
+ format : common
21
+
22
+ entryPoints :
23
+ http :
24
+ address : " :80"
25
+ http :
26
+ redirections :
27
+ entryPoint :
28
+ to : https
29
+ scheme : https
30
+ https :
31
+ address : " :443"
You can’t perform that action at this time.
0 commit comments