Skip to content

Commit 6589db2

Browse files
committed
feat: add service support template files
1 parent cb36fbd commit 6589db2

6 files changed

Lines changed: 108 additions & 4 deletions

File tree

copier.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ project_description:
2121
type: str
2222
help: Your project description
2323

24+
project_type:
25+
type: str
26+
choices:
27+
- cli
28+
- service
29+
default: cli
30+
help: Type of the project
31+
32+
service_framework:
33+
type: str
34+
choices:
35+
- fastapi
36+
- flask
37+
default: fastapi
38+
when: "{{ project_type == 'service' }}"
39+
help: Web framework to use
40+
2441
repository_namespace:
2542
type: str
2643
help: Your repository namespace

template/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ help:
3535
[[print(f'\033[36m{m[0]:<20}\033[0m {m[1]}') for m in re.findall(r'^([a-zA-Z_-]+):.*?## (.*)$$', open(makefile).read(), re.M)] for makefile in ('$(MAKEFILE_LIST)').strip().split()]"
3636

3737
.DEFAULT_GOAL := help
38+
39+
{% if project_type == 'service' %}
40+
.PHONY: serve
41+
serve: ## Run the service
42+
@echo "🚀 Running service"
43+
{% if service_framework == 'flask' %}
44+
@uv run flask --app {{package_name}}.app run --host 0.0.0.0 --port 8000
45+
{% elif service_framework == 'fastapi' %}
46+
@uv run uvicorn {{package_name}}.main:app --reload --host 0.0.0.0 --port 8000
47+
{% endif %}
48+
{% endif %}

template/pyproject.toml.jinja

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ license = "AGPL-3.0"
99
license-files = ["LICENSE"]
1010

1111
dependencies = [
12-
"typer>=0.20"
12+
{%- if project_type == 'cli' %}
13+
"typer>=0.20",
14+
{%- elif project_type == 'service' %}
15+
{%- if service_framework == 'fastapi' %}
16+
"fastapi>=0.110.0",
17+
{%- elif service_framework == 'flask' %}
18+
"flask>=3.0.0",
19+
{%- endif %}
20+
"uvicorn>=0.27.0",
21+
"pydantic-settings>=2.2.0",
22+
{%- endif %}
1323
]
1424

1525
[project.urls]

template/{{package_name}}/main.py

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{% if project_type == 'cli' -%}
2+
import typer
3+
4+
app = typer.Typer()
5+
6+
7+
@app.command()
8+
def hello(name: str):
9+
print(f"Hello {name}")
10+
11+
12+
if __name__ == "__main__":
13+
app()
14+
15+
{%- elif project_type == 'service' -%}
16+
{%- if service_framework == 'fastapi' -%}
17+
from fastapi import FastAPI
18+
from .settings import settings
19+
import uvicorn
20+
21+
app = FastAPI(title="{{ project_name }}")
22+
23+
24+
@app.get("/")
25+
def read_root():
26+
return {"Hello": "World", "env": settings.environment}
27+
28+
@app.get("/health")
29+
def health_check():
30+
return {"status": "ok"}
31+
32+
33+
if __name__ == "__main__":
34+
uvicorn.run("{{ package_name }}.main:app", host="0.0.0.0", port=8000, reload=True)
35+
36+
{%- elif service_framework == 'flask' -%}
37+
from flask import Flask
38+
from .settings import settings
39+
40+
app = Flask(__name__)
41+
42+
43+
@app.route("/")
44+
def hello_world():
45+
return {"Hello": "World", "env": settings.environment}
46+
47+
@app.route("/health")
48+
def health_check():
49+
return {"status": "ok"}
50+
51+
52+
if __name__ == "__main__":
53+
# For development only. Use a production WSGI server for deployment.
54+
app.run(host="0.0.0.0", port=8000, debug=True)
55+
{%- endif -%}
56+
{%- endif -%}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pydantic_settings import BaseSettings, SettingsConfigDict
2+
3+
4+
class Settings(BaseSettings):
5+
model_config = SettingsConfigDict(
6+
env_file=".env", env_ignore_empty=True, extra="ignore"
7+
)
8+
9+
environment: str = "local"
10+
debug: bool = False
11+
12+
13+
settings = Settings()

0 commit comments

Comments
 (0)