33
loading...
This website collects cookies to deliver better user experience
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
pip install fastapi
pip install uvicorn
import requests
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"Hello": "API"}
@app.get("/domain/{domain}")
def get_domain(domain: str):
response = requests.get("http://ip-api.com/json/" + domain)
return response.json()
pip install requests
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
fastapi==0.61.1
h11==0.9.0
idna==2.10
pydantic==1.6.1
requests==2.24.0
starlette==0.13.6
urllib3==1.25.10
uvicorn==0.11.8
websockets==8.1
uvicorn main:app
.
├── docker-compose.yaml
├── domainchecker
│ ├── Dockerfile
│ └── app
│ └── DomainChecker
│ ├── main.py
│ └── requirements.txt
└── nginx
├── Dockerfile
├── fullchain.pem
├── nginx.conf
├── options-ssl-nginx.conf
├── privkey.pem
└── ssl-dhparams.pem
version: "3.7"
services:
web:
build: nginx
ports:
- 80:80
- 443:443
depends_on:
- api
api:
build: domainchecker
environment:
- PORT=8080
ports:
- 8080:8080
=> domainchecker/Dockerfile
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY ./app/DomainChecker/requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
COPY ./app/DomainChecker /app
=> nginx/Dockerfile
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
COPY fullchain.pem /etc/letsencrypt/live/api.domain.com/fullchain.pem
COPY privkey.pem /etc/letsencrypt/live/api.domain.com/privkey.pem
COPY options-ssl-nginx.conf /etc/letsencrypt/options-ssl-nginx.conf
COPY ssl-dhparams.pem /etc/letsencrypt/ssl-dhparams.pem
=> nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # set to 'on' if nginx worker_processes > 1
# 'use epoll;' to enable for Linux 2.6+
# 'use kqueue;' to enable for FreeBSD, OSX
}
http {
include mime.types;
# fallback in case we can't determine a type
default_type application/octet-stream;
access_log /var/log/nginx/access.log combined;
sendfile on;
upstream app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response
# for UNIX domain socket setups
#server unix:/tmp/gunicorn.sock fail_timeout=0;
# for a TCP configuration
server api:8080 fail_timeout=0;
}
server {
# if no Host match, close the connection to prevent host spoofing
listen 80 default_server;
return 444;
}
server {
# use 'listen 80 deferred;' for Linux
# use 'listen 80 accept_filter=httpready;' for FreeBSD
client_max_body_size 4G;
# set the correct host(s) for your site
server_name api.domain.com;
keepalive_timeout 5;
# path for static files
root /path/to/app/current/public;
location / {
# checks for static file, if not found proxy to app
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_pass http://app_server;
}
#error_page 500 502 503 504 /500.html;
#location = /500.html {
# root /path/to/app/current/public;
#}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/api.domain.com/fullchain.p>
ssl_certificate_key /etc/letsencrypt/live/api.domain.com/privkey>
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = api.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name api.domain.com;
return 404; # managed by Certbot
}}
sudo docker-compose up --build