Skip to content

EvertonAlauk/flask-microservice-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Internet Banking

Requirements

  • Flask
  • SQLAlchemy
  • Flask-Migrate
  • Postgres
  • Gunicorn
  • PyJWT
  • Github Actions
  • Prometheus
  • Grafana
  • Docker & docker-compose
  • Kubernetes
  • Minikube

Sumary

Docker-compose

docker-compose up -d

or

K8S

Minikube

make minikube

Namespace and apps

make kubernetes

Create the databases

make databases

Check your services

make kube-services 
kubectl -n internet-banking get services
NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
bank-account-svc   ClusterIP   10.109.219.209   <none>        5002/TCP   18d
postgres-svc       ClusterIP   10.107.25.52     <none>        5432/TCP   18d
user-svc           ClusterIP   10.97.213.50     <none>        5001/TCP   18d

With httpie:

Run the user API throught the kubernetes container

Notice that your services with Flask application has avaliable at 5001, 5002 and 5003 port address. Use ingress to acess then.

make kube-ingress    
kubectl -n internet-banking get ingress
NAME      CLASS     HOSTS   ADDRESS        PORTS   AGE
ingress   ingress   *       192.168.49.2   80      15d

User

http -f POST <ingress-address>:80/user username="user" email="user@teste.com" password="password123" name="user"
HTTP/1.1 200 OK
Connection: close
Content-Length: 54
Content-Type: application/json
Date: Sun, 14 Mar 2021 23:38:35 GMT
Server: gunicorn/20.0.4

[
    {
        "active": true,
        "created": "2021-03-15T00:08:14.180200",
        "email": "user@teste.com",
        "id": 1,
        "name": "user",
        "username": "user"
    }
]
http <ingress-address>:80/user
HTTP/1.1 200 OK
Connection: close
Content-Length: 54
Content-Type: application/json
Date: Sun, 14 Mar 2021 23:39:01 GMT
Server: gunicorn/20.0.4

[
    {
        "active": true,
        "created": "2021-03-15T00:08:14.180200",
        "email": "user@teste.com",
        "id": 1,
        "name": "user",
        "username": "user"
    }
]

Auth JWT

http -f POST <ingress-address>:80/user/auth -a user:password123
HTTP/1.1 200 OK
Connection: close
Content-Length: 54
Content-Type: application/json
Date: Sun, 14 Mar 2021 23:39:01 GMT
Server: gunicorn/20.0.4

{
    "expired": 1615813569.092066,
    "token": "token"
}

Bank Account: Balance

It's important, just one time, to include a balance for user. In this case, use the POST method.

http -f POST <ingress-address>:80/bank_account/balance/ value="2500.00" 'Authorization: Bearer {token}'
HTTP/1.1 200 OK
Connection: close
Content-Length: 40
Content-Type: application/json
Date: Sun, 21 Mar 2021 13:58:50 GMT
Server: gunicorn/20.0.4

{
    "id": 1,
    "user_id": 1,
    "value": 2500.0
}

Otherwise, GET method for check the balance.

http <ingress-address>:80/bank_account/balance/ 'Authorization: Bearer {token}'
HTTP/1.1 200 OK
Connection: close
Content-Length: 40
Content-Type: application/json
Date: Sun, 21 Mar 2021 13:58:50 GMT
Server: gunicorn/20.0.4

{
    "id": 1,
    "user_id": 1,
    "value": 2500.0
}

Bank Account: Credit

http -f POST <ingress-address>:80/bank_account/credit value="2.6" 'Authorization: Bearer {token}'
HTTP/1.1 200 OK
Connection: close
Content-Length: 38
Content-Type: application/json
Date: Sat, 20 Mar 2021 22:33:03 GMT
Server: gunicorn/20.0.4

{
    "id": 1,
    "user_id": 1,
    "value": 2.6
}

Bank Account: Debit

http -f POST <ingress-address>:80/bank_account/debit value="2.6" 'Authorization: Bearer {token}'
HTTP/1.1 200 OK
Connection: close
Content-Length: 38
Content-Type: application/json
Date: Sat, 20 Mar 2021 22:33:03 GMT
Server: gunicorn/20.0.4

{
    "id": 1,
    "user_id": 1,
    "value": 2.6
}

Bank Account: Statement

http <ingress-address>:80/bank_account/statement 'Authorization: Bearer {token}'
HTTP/1.1 200 OK
Connection: close
Content-Length: 142
Content-Type: application/json
Date: Sun, 21 Mar 2021 13:57:57 GMT
Server: gunicorn/20.0.4

{
    "credits": [
        {
            "id": 1,
            "user_id": 1,
            "value": 2.6
        },
        {
            "id": 2,
            "user_id": 1,
            "value": 5.1
        }
    ],
    "debits": [
        {
            "id": 1,
            "user_id": 1,
            "value": 4.9
        }
    ]
}

Monitoring and metrics

Prometheus

http://localhost:9090

Grafana

http://localhost:3000

Automation Testing

Jenkins

http://localhost:8081
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

Copy the password and join the Jenkins plataform.

About

A sample microservice with Docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published