Data Framework API Application that routing with FastAPI.
Table of Contents:
This application require run on local intra network (on premise) server. The OS is Window server 2016. The solution that we found to run this application is,
- Directly with command line
- Docker on WSL
- Window service
- Window service by NSSM
- Window Container on Hyper-V
Linux Container on Hyper-V (LCOW)Window task scheduler
-
First, you must install python version
3.9.13
on your server. -
Start create your python virtual environment for this application.
$ python -m vnev vnev $ venv\Scripts\activate
-
Install all of necessary packages from requirement file.
(venv) $ pip install -r requirements.txt --no-cache-dir (venv) $ ren .{demo}.env .env
[!NOTE] Make sure for no caching any packages in pip by this command,
(venv) $ pip cache purge
-
Run your application local
(venv) $ uvicorn main:app --reload
-
(Optional) If you want to stop your application, you can use
Ctrl+C
and deactivate your virtual environment.(venv) $ deactivate
-
First, you must check your server installed WSL
Reference:
docs: Use Docker for windows in WSL docs: Unable to locate package docker-ce on a 64bit Ubuntu -
Check Docker service running
$ sudo service docker start
-
Go to the project directory (in where your Dockerfile is, containing your app directory).
-
Build your FastAPI image:
$ sudo docker build -t dedp-fastapi . --no-cache $ sudo docker image ls
Or, use multistage image building:
$ sudo docker build -t dedp-fastapi -f multistage.Dockerfile . --no-cache
-
Run a container based on your image:
$ cp .{demo}.env .env $ sudo docker run -d --env-file ./.env --name dedp-fastapi -p 8000:8000 dedp-fastapi
[!NOTE] If you want to clear any storage in Docker,
$ docker system prune -a
-
Check this container running in background
$ curl http://127.0.0.1:8000/api/v1/docs
$ sudo docker ps
-
Install the latest pywin32.exe and Window Service requirements with pip
$ pip install pywin32 --upgrade --no-cache $ pip install -r requirements.wins.txt --no-cache
-
Compile your service.py using pyinstaller
$ pyinstaller --paths "%cd%\venv\Lib\site-packages" \ --onefile service.py \ --hidden-import=win32timezone \ --clean --uac-admin \ --add-data '.env;.'
[!WARNING] In argument
--add-data
on unix systems, you should write:
instead of;
[!NOTE] After install
service.py
, it will create/dist
and/build
folders[!NOTE]
--paths
: The pyinstaller will search for imports here
--hidden-import
: Which modules should be imported by pyinstaller from the path -
Installing service with startup == Automatic
$ .\dist\service.exe --startup=auto install
$ service.exe start $ service.exe stop $ service.exe debug $ service.exe remove
[!NOTE] If you want to set the
StartUp= Manual
, then don't use--startup=auto
, while installing service
If you want to set theStartUp= Automatic
, then use--startup=delayed
, while installing service
Use--startup
argument before install argument[!WARNING] This option has the Bug of
Error 1053: The service did not respond timely
when start the service while debugging did not raise any error -
(Optional) Add file .exe file to Windows Service directly
$ sc.exe create "FastAPIServiceName" binPath= "%cd%\dist\service.exe" \ DisplayName= "FastAPI Service DisplayName" start= auto
$ sc.exe create "FastAPIServiceName" binPath= "%cd%\venv\Scripts\python.exe %cd%\service.py" \ DisplayName= "FastAPI Service DisplayName" start= auto
$ sc.exe start $ sc.exe delete demo_application
-
Now your python service is installed as Windows service now. You can see it in Service Manager and registry under:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\FastAPIServiceName
Note
Another way to install Windows service, pip install pysc
Reference:
docs: How do you run a Python script as a Service in Windows
-
Downloads and unzip NSSM package to server from NSSM Download
nssm-{version} ├─── src ├─── win32 │ └─── nssm.exe ├─── win64 │ └─── nssm.exe <---- Use this file for run NSSM ├─── ChangeLog.txt └─── README.txt
-
Install service with NSSM
$ .\nssm\win64\nssm.exe install "FastAPIService" "%cd%\runserver.bat"
-
(Optional) Set up logging from NSSM
stdout
andstderr
$ .\nssm\win64\nssm.exe set "FastAPIService" AppStdout "%cd%\logs\FastAPIService.log" $ .\nssm\win64\nssm.exe set "FastAPIService" AppStderr "%cd%\logs\FastAPIService.log" $ .\nssm\win64\nssm.exe set "FastAPIService" AppRotateFiles 1 $ .\nssm\win64\nssm.exe set "FastAPIService" AppRotateOnline 1 $ .\nssm\win64\nssm.exe set "FastAPIService" AppRotateSeconds 86400 $ .\nssm\win64\nssm.exe set "FastAPIService" AppRotateBytes 1048576
-
Start Window service
$ sc.exe start "FastAPIService"
-
(Optional) NSSM command line
$ .\nssm\win64\nssm.exe restart "FastAPIService" $ .\nssm\win64\nssm.exe edit "FastAPIService" $ .\nssm\win64\nssm.exe stop "FastAPIService" $ .\nssm\win64\nssm.exe remove "FastAPIService"
-
Check all NSSM service
$ Get-WmiObject win32_service | ?{$_.PathName -like '*nssm*'} | select Name, DisplayName, State, PathName
Reference:
docs: How to run a Python script Windows service NSSM
- Install Docker module on PowerShell
$ Install-Module DockerMsftProvider -Force
$ Install-Package Docker -ProviderName DockerMsftProvider -Force
$ Restart-Computer
- Install Docker module on PowerShell
$ Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart
$ Install-Module DockerProvider
$ Install-Package Docker -ProviderName DockerProvider -RequiredVersion preview
Reboot your machine manual once again.
$ Set-Content -Value "`{`"experimental`":true`}" -Path C:\ProgramData\docker\config\daemon.json
- Download LCOW and install in Linux Container folder
$ [Environment]::SetEnvironmentVariable("LCOW_SUPPORTED", "1", "Machine")
$ Restart-Service docker
Reference:
docs: How to run Python script in Windows
docs: Linux Containers on Windows server 2016
The simple way to test this application running by request directly to hearth check route.
$ curl http://localhost:8000/health/
This project base on MIT License and depend on dependency package license in requirement file.