python-project-generator 3.2.2

Generates a Python project structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
use anyhow::{bail, Result};

use crate::{
    file_manager::save_file_with_content,
    project_info::{ProjectInfo, ProjectManager, Pyo3PythonManager},
};

fn create_dockercompose_file(project_info: &ProjectInfo) -> String {
    let base_name = &project_info.project_slug;

    format!(
        r#"services:
  backend:
    image: {base_name}-backend:latest
    restart: unless-stopped
    networks:
      - traefik-public-{base_name}
      - default
    build:
      context: .
    container_name: {base_name}-backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://127.0.0.1:8000/api/v1/health"]
      interval: 10s
      retries: 5
      start_period: 30s
      timeout: 10s
    depends_on:
      db:
        condition: service_healthy
        restart: true
      valkey:
        condition: service_healthy
        restart: true
      migrations:
        condition: service_completed_successfully
    env_file:
      - .env
    environment:
      - POSTGRES_HOST=db
      - VALKEY_HOST=valkey
    labels:
      - traefik.enable=true
      - traefik.docker.network=traefik-public-{base_name}
      - traefik.constraint-label=traefik-public

      - traefik.http.services.${{STACK_NAME?Variable not set}}-backend.loadbalancer.server.port=8000

      # Rate limiting middleware
      - traefik.http.middlewares.${{STACK_NAME?Variable not set}}-api-rate-limit.ratelimit.burst=50
      - traefik.http.middlewares.${{STACK_NAME?Variable not set}}-api-rate-limit.ratelimit.average=25

      # Security headers middleware (backend-specific)
      - traefik.http.middlewares.${{STACK_NAME?Variable not set}}-security-headers.headers.contenttypenosniff=true
      - traefik.http.middlewares.${{STACK_NAME?Variable not set}}-security-headers.headers.referrerpolicy=strict-origin-when-cross-origin
      - traefik.http.middlewares.${{STACK_NAME?Variable not set}}-security-headers.headers.forcestsheader=true
      - traefik.http.middlewares.${{STACK_NAME?Variable not set}}-security-headers.headers.stsincludesubdomains=true
      - traefik.http.middlewares.${{STACK_NAME?Variable not set}}-security-headers.headers.stsseconds=31536000

      - traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-http.rule=Host(`api.${{DOMAIN?Variable not set}}`)
      - traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-http.entrypoints=http

      - traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-https.rule=Host(`api.${{DOMAIN?Variable not set}}`) - traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-https.entrypoints=https - traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-https.tls=true
      - traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-https.tls.certresolver=le

      # Enable redirection for HTTP and HTTPS
      - traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-http.middlewares=https-redirect

      # Enable rate limiting and security headers
      - traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-https.middlewares=${{STACK_NAME?Variable not set}}-api-rate-limit,${{STACK_NAME?Variable not set}}-security-headers

  db:
    image: postgres:18-alpine
    restart: unless-stopped
    container_name: {base_name}-db
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $POSTGRES_USER -d $POSTGRES_DB"]
      interval: 10s
      retries: 5
      start_period: 30s
      timeout: 10s
    expose:
      - 5432
    env_file:
      - .env
    environment:
      - POSTGRES_PASSWORD=${{POSTGRES_PASSWORD?Variable not set}}
      - POSTGRES_USER=${{POSTGRES_USER?Variable not set}}
      - POSTGRES_DB=${{POSTGRES_DB?Variable not set}}
    volumes:
      - {base_name}-db-data:/var/lib/postgresql/data

  valkey:
    image: valkey/valkey:8-alpine
    restart: unless-stopped
    container_name: {base_name}-valkey
    healthcheck:
      test:
        [
          "CMD",
          "valkey-cli",
          "--no-auth-warning",
          "-a",
          "${{VALKEY_PASSWORD?Variable not set}}",
          "ping",
        ]
      interval: 10s
      retries: 5
      start_period: 30s
      timeout: 10s
    expose:
      - 6379
    env_file:
      - .env
    command: valkey-server --requirepass ${{VALKEY_PASSWORD?Variable not set}}
    volumes:
      - {base_name}-valkey-data:/var/lib/valkey/data

  migrations:
    image: ghcr.io/sanders41/sqlx-migration-runner:1
    container_name: {base_name}-migrations
    env_file:
      - .env
    environment:
      - POSTGRES_HOST=db
      - DATABASE_URL=postgresql://${{POSTGRES_USER}}:${{POSTGRES_PASSWORD}}@db:5432/${{POSTGRES_DB}}
    depends_on:
      db:
        condition: service_healthy
        restart: true
    volumes:
      - ./migrations:/migrations

volumes:
  {base_name}-db-data:
  {base_name}-valkey-data:

networks:
  traefik-public-{base_name}:
    name: traefik-public-{base_name}
    # Allow setting it to false for testing
    external: true
"#
    )
}

pub fn save_dockercompose_file(project_info: &ProjectInfo) -> Result<()> {
    let base = &project_info.base_dir();
    let file_path = base.join("docker-compose.yml");
    let file_content = create_dockercompose_file(project_info);

    save_file_with_content(&file_path, &file_content)?;

    Ok(())
}

fn create_dockercompose_override_file(project_info: &ProjectInfo) -> String {
    let base_name = &project_info.project_slug;

    format!(
        r#"services:
  proxy:
    image: traefik:3
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - "80:80"
      - "8090:8080"
    # Duplicate the command from docker-compose.yml to add --api.insecure=true
    command:
      # Enable Docker in Traefik, so that it reads labels from Docker services
      - --providers.docker
      # Add a constraint to only use services with the label for this stack
      - --providers.docker.constraints=Label(`traefik.constraint-label`, `traefik-public`)
      # Do not expose all Docker services, only the ones explicitly exposed
      - --providers.docker.exposedbydefault=false
      # Create an entrypoint "http" listening on port 80
      - --entrypoints.http.address=:80
      # Enable the access log, with HTTP requests
      - --accesslog
      # Enable the Traefik log, for configurations and errors
      - --log
      # Enable debug logging for local development
      - --log.level=DEBUG
      # Enable the Dashboard and API
      - --api
      # Enable the Dashboard and API in insecure mode for local development
      - --api.insecure=true
    labels:
      # Enable Traefik for this service, to make it available in the public network
      - traefik.enable=true
      - traefik.constraint-label=traefik-public
    networks:
      - traefik-public-{base_name}
      - default

  backend:
    image: {base_name}-backend
    restart: no
    healthcheck:
      test: ["CMD", "curl", "-f", "http://127.0.0.1:8000/api/v1/health"]
      interval: 10s
      retries: 5
      start_period: 10s
      timeout: 5s
    ports:
      - "8000:8000"
    networks:
      - traefik-public-{base_name}
      - default
    build:
      context: .
    container_name: {base_name}-backend
    depends_on:
      db:
        condition: service_healthy
        restart: true
      valkey:
        condition: service_healthy
        restart: true
    env_file:
      - .env
    environment:
      - SECRET_KEY=someKey
      - POSTGRES_HOST=db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=test_password
      - VALKEY_HOST=valkey
      - VALKEY_PASSWORD=test_password
      - ENVIRONMENT=local
    labels:
      - traefik.enable=true
      - traefik.docker.network=traefik-public-{base_name}
      - traefik.constraint-label=traefik-public
      - traefik.http.services.${{STACK_NAME:-{base_name}}}-backend.loadbalancer.server.port=8000
      - traefik.http.routers.${{STACK_NAME:-{base_name}}}-backend-http.rule=Host(`api.127.0.0.1`)
      - traefik.http.routers.${{STACK_NAME:-{base_name}}}-backend-http.entrypoints=http
      - traefik.http.routers.${{STACK_NAME:-{base_name}}}-backend-https.rule=
      - traefik.http.routers.${{STACK_NAME:-{base_name}}}-backend-https.entrypoints=
      - traefik.http.routers.${{STACK_NAME:-{base_name}}}-backend-https.tls=
      - traefik.http.routers.${{STACK_NAME:-{base_name}}}-backend-https.tls.certresolver=
      - traefik.http.routers.${{STACK_NAME:-{base_name}}}-backend-http.middlewares=

  db:
    restart: "no"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $POSTGRES_USER -d $POSTGRES_DB"]
      interval: 10s
      retries: 5
      start_period: 30s
      timeout: 10s
    ports:
      - "5432:5432"

  valkey:
    restart: "no"
    # By default only 16 databases are allowed. Bumping this just for testing so that tests can
    # run in parallel without impacting each other
    command: valkey-server --requirepass test_password --databases 100
    healthcheck:
      test:
        [
          "CMD",
          "valkey-cli",
          "--no-auth-warning",
          "-a",
          "${{VALKEY_PASSWORD?Variable not set}}",
          "ping",
        ]
      interval: 10s
      retries: 5
      start_period: 30s
      timeout: 10s
    ports:
      - 6379:6379

networks:
  traefik-public-{base_name}:
    # For local dev, don't expect an external Traefik network
    external: false
"#
    )
}

pub fn save_dockercompose_override_file(project_info: &ProjectInfo) -> Result<()> {
    let base = &project_info.base_dir();
    let file_path = base.join("docker-compose.override.yml");
    let file_content = create_dockercompose_override_file(project_info);

    save_file_with_content(&file_path, &file_content)?;

    Ok(())
}

fn create_dockercompose_traefik_file(project_info: &ProjectInfo) -> String {
    let base_name = &project_info.project_slug;

    format!(
        r#"services:
  traefik:
    image: traefik:3
    container_name: {base_name}-traefik
    ports:
      # Listen on port 80, default for HTTP, necessary to redirect to HTTPS
      - 80:80
      # Listen on port 443, default for HTTPS
      - 443:443
    restart: unless-stopped
    env_file:
      - .env
    labels:
      # Enable Traefik for this service, to make it available in the public network
      - traefik.enable=true
      # Use the traefik-public network (declared below)
      - traefik.docker.network=traefik-public
      # Define the port inside of the Docker service to use
      - traefik.http.services.traefik-dashboard.loadbalancer.server.port=8080
      # Make Traefik use this domain (from an environment variable) in HTTP
      - traefik.http.routers.traefik-dashboard-http.entrypoints=http
      - traefik.http.routers.traefik-dashboard-http.rule=Host(`traefik.${{DOMAIN?Variable not set}}`)
      # traefik-https the actual router using HTTPS
      - traefik.http.routers.traefik-dashboard-https.entrypoints=https
      - traefik.http.routers.traefik-dashboard-https.rule=Host(`traefik.${{DOMAIN?Variable not set}}`)
      - traefik.http.routers.traefik-dashboard-https.tls=true
      # Use the "le" (Let's Encrypt) resolver created below
      - traefik.http.routers.traefik-dashboard-https.tls.certresolver=le
      # Use the special Traefik service api@internal with the web UI/Dashboard
      - traefik.http.routers.traefik-dashboard-https.service=api@internal
      # https-redirect middleware to redirect HTTP to HTTPS
      - traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
      - traefik.http.middlewares.https-redirect.redirectscheme.permanent=true
      # traefik-http set up only to use the middleware to redirect to https
      - traefik.http.routers.traefik-dashboard-http.middlewares=https-redirect
      # admin-auth middleware with HTTP Basic auth
      # Using the environment variables USERNAME and HASHED_PASSWORD
      - traefik.http.middlewares.admin-auth.basicauth.users=${{USERNAME?Variable not set}}:${{HASHED_PASSWORD?Variable not set}}
      # Enable HTTP Basic auth, using the middleware created above
      - traefik.http.routers.traefik-dashboard-https.middlewares=admin-auth
    volumes:
      # Add Docker as a mounted volume, so that Traefik can read the labels of other services
      - /var/run/docker.sock:/var/run/docker.sock:ro
      # Mount the volume to store the certificates
      - {base_name}-traefik-public-certificates:/certificates
    command:
      # Enable Docker in Traefik, so that it reads labels from Docker services
      - --providers.docker
      # Do not expose all Docker services, only the ones explicitly exposed
      - --providers.docker.exposedbydefault=false
      # Create an entrypoint "http" listening on port 80
      - --entrypoints.http.address=:80
      # Create an entrypoint "https" listening on port 443
      - --entrypoints.https.address=:443
      # Create the certificate resolver "le" for Let's Encrypt, uses the environment variable EMAIL
      - --certificatesresolvers.le.acme.email=${{EMAIL?Variable not set}}
      # Store the Let's Encrypt certificates in the mounted volume
      - --certificatesresolvers.le.acme.storage=/certificates/acme.json
      # Use the TLS Challenge for Let's Encrypt
      - --certificatesresolvers.le.acme.tlschallenge=true
      # Enable the access log, with HTTP requests
      - --accesslog
      # Enable the Traefik log, for configurations and errors
      - --log
      # Enable the Dashboard and API
      - --api
    networks:
      # Use the public network created to be shared between Traefik and
      # any other service that needs to be publicly available with HTTPS
      - traefik-public

volumes:
  # Create a volume to store the certificates, even if the container is recreated
  {base_name}-traefik-public-certificates:

networks:
  # Use the previously created public network "traefik-public", shared with other
  # services that need to be publicly available via this Traefik
  traefik-public:
    name: traefik-public
    external: true
"#
    )
}

pub fn save_dockercompose_traefik_file(project_info: &ProjectInfo) -> Result<()> {
    let base = &project_info.base_dir();
    let file_path = base.join("docker-compose.treafik.yml");
    let file_content = create_dockercompose_traefik_file(project_info);

    save_file_with_content(&file_path, &file_content)?;

    Ok(())
}

fn create_dockerfile(project_info: &ProjectInfo) -> Result<String> {
    let python_version = &project_info.python_version;
    let source_dir = &project_info.source_dir;
    match project_info.project_manager {
        ProjectManager::Uv => Ok(format!(
            r#"# syntax=docker/dockerfile:1

FROM ubuntu:24.04 AS builder

WORKDIR /app

ENV \
  PYTHONUNBUFFERED=true \
  UV_PYTHON_INSTALL_DIR=/opt/uv/python \
  UV_LINK_MODE=copy

RUN : \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  build-essential \
  curl \
  ca-certificates \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# Install uv
ADD https://astral.sh/uv/install.sh /uv-installer.sh

RUN sh /uv-installer.sh && rm /uv-installer.sh

ENV PATH="/root/.local/bin:$PATH"

# Create virtual environment and download Python
RUN uv venv -p {python_version}

COPY . ./

RUN --mount=type=cache,target=/root/.cache/uv \
  uv sync --locked --no-dev --no-editable


# Build production stage
FROM ubuntu:24.04 AS prod

RUN useradd appuser

WORKDIR /app

RUN chown appuser:appuser /app

ENV \
  PYTHONUNBUFFERED=true \
  PATH="/app/.venv/bin:$PATH" \
  PORT="8000"

COPY --from=builder /app/.venv /app/.venv
COPY --from=builder /app/{source_dir} /app/{source_dir}
COPY --from=builder /opt/uv/python /opt/uv/python
COPY ./scripts/entrypoint.sh /app

RUN chmod +x /app/entrypoint.sh

EXPOSE 8000

USER appuser

ENTRYPOINT ["./entrypoint.sh"]
"#,
        )),
        ProjectManager::Poetry => Ok(format!(
            r#"# syntax=docker/dockerfile:1

FROM ubuntu:24.04 AS builder

WORKDIR /app

ENV \
  PYTHONUNBUFFERED=true \
  POETRY_NO_INTERACTION=true \
  POETRY_VIRTUALENVS_IN_PROJECT=true \
  POETRY_CACHE_DIR=/tmp/poetry_cache

RUN : \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  build-essential \
  curl \
  ca-certificates \
  software-properties-common \
  && add-apt-repository ppa:deadsnakes/ppa \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  python{python_version} \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# Install Poetry
RUN curl -sSL https://install.python-poetry.org | python{python_version} -

ENV PATH="/root/.local/bin:$PATH"

COPY pyproject.toml poetry.lock ./

COPY . ./

RUN --mount=type=cache,target=$POETRY_CACHE_DIR \
  poetry config virtualenvs.in-project true \
  && poetry install --only=main


# Build production stage
FROM ubuntu:24.04 AS prod

RUN useradd appuser

WORKDIR /app

RUN chown appuser:appuser /app

ENV \
  PYTHONUNBUFFERED=true \
  PATH="/app/.venv/bin:$PATH" \
  PORT="8000"

RUN : \
  && apt-get update \
  && apt-get install -y --no-install-recommends\
  software-properties-common \
  && add-apt-repository ppa:deadsnakes/ppa \
  && apt-get update \
  && apt-get install -y --no-install-recommends python{python_version} \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/.venv /app/.venv
COPY --from=builder /app/{source_dir} /app/{source_dir}
COPY ./scripts/entrypoint.sh /app

RUN chmod +x /app/entrypoint.sh

EXPOSE 8000

USER appuser

ENTRYPOINT ["./entrypoint.sh"]
"#
        )),
        ProjectManager::Setuptools => Ok(format!(
            r#"# syntax=docker/dockerfile:1

FROM ubuntu:24.04 AS builder

WORKDIR /app

ENV \
  PYTHONUNBUFFERED=true \
  PATH="/root/.local/bin:$PATH"

RUN : \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  build-essential \
  software-properties-common \
  && add-apt-repository ppa:deadsnakes/ppa \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  python{python_version} \
  python{python_version}-dev \
  python{python_version}-venv \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# Create virtual environment
RUN python{python_version} -m venv .venv

COPY . ./

RUN .venv/bin/python -m pip install -r requirements.txt


# Build production stage
FROM ubuntu:24.04 AS prod

ENV \
  PYTHONUNBUFFERED=true \
  PATH="/app/.venv/bin:$PATH" \
  PORT="8000"

RUN : \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  software-properties-common \
  && add-apt-repository ppa:deadsnakes/ppa \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  python{python_version} \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

RUN useradd appuser

WORKDIR /app

RUN chown appuser:appuser /app

COPY --from=builder /app/.venv /app/.venv
COPY --from=builder /app/{source_dir} /app/{source_dir}
COPY ./scripts/entrypoint.sh /app

RUN chmod +x /app/entrypoint.sh

EXPOSE 8000

USER appuser

ENTRYPOINT ["./entrypoint.sh"]
"#
        )),
        ProjectManager::Maturin => {
            if let Some(project_manager) = &project_info.pyo3_python_manager {
                match project_manager {
                    Pyo3PythonManager::Uv => Ok(format!(
                        r#"# syntax=docker/dockerfile:1

FROM ubuntu:24.04 AS builder

WORKDIR /app

ENV \
  PYTHONUNBUFFERED=true \
  UV_PYTHON_INSTALL_DIR=/opt/uv/python \
  UV_LINK_MODE=copy

RUN : \
 && apt-get update \
  && apt-get install -y --no-install-recommends \
  build-essential \
  curl \
  ca-certificates \
  libssl-dev \
  pkg-config \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# Install uv
ADD https://astral.sh/uv/install.sh /uv-installer.sh

RUN sh /uv-installer.sh && rm /uv-installer.sh

# Install rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal

ENV PATH="/root/.local/bin:/root/.cargo/bin:$PATH"

# Create virtual environment and download Python
RUN uv venv -p {python_version}

COPY pyproject.toml Cargo.toml Cargo.lock README.md LICENSE ./
COPY src/ ./src
RUN mkdir {source_dir}

RUN --mount=type=cache,target=/app/target/ \
  --mount=type=cache,target=/usr/local/cargo/git/db \
  --mount=type=cache,target=/usr/local/cargo/registry/ \
  uv tool run maturin develop -r

COPY uv.lock ./

RUN --mount=type=cache,target=/root/.cache/uv \
  uv sync --locked --no-dev --no-install-project

COPY . /app


# Build production stage
FROM ubuntu:24.04 AS prod

RUN useradd appuser

WORKDIR /app

RUN chown appuser:appuser /app

ENV \
  PYTHONUNBUFFERED=true \
  PATH="/app/.venv/bin:$PATH" \
  PORT="8000"

COPY --from=builder /app/.venv /app/.venv
COPY --from=builder /app/{source_dir} /app/{source_dir}
COPY --from=builder /opt/uv/python /opt/uv/python
COPY ./scripts/entrypoint.sh /app

RUN chmod +x /app/entrypoint.sh

EXPOSE 8000

USER appuser

ENTRYPOINT ["./entrypoint.sh"]
"#,
                    )),
                    Pyo3PythonManager::Setuptools => Ok(format!(
                        r#"# syntax=docker/dockerfile:1

FROM ubuntu:24.04 AS builder

WORKDIR /app

ENV \
  PYTHONUNBUFFERED=true \
  UV_PYTHON_INSTALL_DIR=/opt/uv/python \
  UV_LINK_MODE=copy

RUN : \
 && apt-get update \
  && apt-get install -y --no-install-recommends \
  build-essential \
  curl \
  ca-certificates \
  libssl-dev \
  pkg-config \
  software-properties-common \
  && add-apt-repository ppa:deadsnakes/ppa \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  python{python_version} \
  python{python_version}-dev \
  python{python_version}-venv \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# Install uv
ADD https://astral.sh/uv/install.sh /uv-installer.sh

RUN sh /uv-installer.sh && rm /uv-installer.sh

# Install rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal

ENV PATH="/root/.local/bin:/root/.cargo/bin:$PATH"

# Create virtual environment
RUN python{python_version} -m venv .venv

COPY pyproject.toml Cargo.toml Cargo.lock README.md LICENSE ./
COPY src/ ./src
RUN mkdir {source_dir}

RUN --mount=type=cache,target=/app/target/ \
  --mount=type=cache,target=/usr/local/cargo/git/db \
  --mount=type=cache,target=/usr/local/cargo/registry/ \
  uv tool run maturin develop -r

COPY requirements.txt ./

RUN --mount=type=cache,target=/root/.cache/uv \
  .venv/bin/python -m pip install -r requirements.txt

COPY . /app


RUN --mount=type=cache,target=/usr/local/cargo/git/db \
  --mount=type=cache,target=/usr/local/cargo/registry/ \
  .venv/bin/python -m pip install -r requirements.txt \
  && uv tool run maturin develop -r


# Build production stage
FROM ubuntu:24.04 AS prod

RUN useradd appuser

WORKDIR /app

RUN chown appuser:appuser /app

ENV \
  PYTHONUNBUFFERED=true \
  PATH="/app/.venv/bin:$PATH" \
  PORT="8000"

RUN : \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  software-properties-common \
  && add-apt-repository ppa:deadsnakes/ppa \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
  python{python_version} \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/.venv /app/.venv
COPY --from=builder /app/{source_dir} /app/{source_dir}
COPY ./scripts/entrypoint.sh /app

RUN chmod +x /app/entrypoint.sh

EXPOSE 8000

USER appuser

ENTRYPOINT ["./entrypoint.sh"]
"#
                    )),
                }
            } else {
                bail!("A PyO3 python manager is required for Maturin projects")
            }
        }
    }
}

pub fn save_dockerfile(project_info: &ProjectInfo) -> Result<()> {
    let base = &project_info.base_dir();
    let file_path = base.join("Dockerfile");
    let file_content = create_dockerfile(project_info)?;

    save_file_with_content(&file_path, &file_content)?;

    Ok(())
}

fn create_dockerignore(project_info: &ProjectInfo) -> String {
    let mut info = r#"__pycache__
app.egg-info
*.pyc
.mypy_cache
.pytest_cache
.ruff_cache
.coverage
htmlcov
.cache
.venv
.env*
*.log
Dockerfile
.dockerignore
.git
tests
tests-results
"#
    .to_string();

    if project_info.project_manager == ProjectManager::Maturin {
        info.push_str("target\n");
    }

    info
}

pub fn save_dockerfileignore(project_info: &ProjectInfo) -> Result<()> {
    let base = &project_info.base_dir();
    let file_path = base.join(".dockerignore");
    let file_content = create_dockerignore(project_info);

    save_file_with_content(&file_path, &file_content)?;

    Ok(())
}

fn create_entrypoint_script(project_info: &ProjectInfo) -> String {
    let module = &project_info.module_name();

    format!(
        r#"#!/bin/bash

CORES=$(nproc --all)
WORKERS=$((($CORES * 2 + 1) > 8 ? 8 : ($CORES * 2 + 1)))

echo Starting Granian with $WORKERS workers

.venv/bin/granian ./{module}/main:app --host 0.0.0.0 --port 8000 --interface asgi --no-ws --workers ${{WORKERS}} --runtime-mode st --loop uvloop --log-level info --log --workers-lifetime 10800 --respawn-interval 30 --process-name granian-at-reporter
"#
    )
}

pub fn save_entrypoint_script(project_info: &ProjectInfo) -> Result<()> {
    let base = &project_info.base_dir();
    let file_path = base.join("scripts/entrypoint.sh");
    let file_content = create_entrypoint_script(project_info);

    save_file_with_content(&file_path, &file_content)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::project_info::{DatabaseManager, LicenseType, ProjectInfo, Pyo3PythonManager};
    use insta::assert_yaml_snapshot;
    use std::fs::create_dir_all;
    use tmp_path::tmp_path;

    #[tmp_path]
    fn project_info_dummy() -> ProjectInfo {
        ProjectInfo {
            project_name: "My project".to_string(),
            project_slug: "my-project".to_string(),
            source_dir: "my_project".to_string(),
            project_description: "This is a test".to_string(),
            creator: "Arthur Dent".to_string(),
            creator_email: "authur@heartofgold.com".to_string(),
            license: LicenseType::Mit,
            copyright_year: Some("2023".to_string()),
            version: "0.1.0".to_string(),
            python_version: "3.11".to_string(),
            min_python_version: "3.10".to_string(),
            project_manager: ProjectManager::Poetry,
            pyo3_python_manager: Some(Pyo3PythonManager::Uv),
            is_application: true,
            is_async_project: false,
            github_actions_python_test_versions: vec![
                "3.10".to_string(),
                "3.11".to_string(),
                "3.12".to_string(),
                "3.13".to_string(),
                "3.14".to_string(),
            ],
            max_line_length: 100,
            use_dependabot: true,
            dependabot_schedule: None,
            dependabot_day: None,
            use_continuous_deployment: true,
            use_release_drafter: true,
            use_multi_os_ci: true,
            include_docs: false,
            docs_info: None,
            project_root_dir: Some(tmp_path),
            is_fastapi_project: true,
            database_manager: Some(DatabaseManager::AsyncPg),
        }
    }

    #[test]
    fn test_save_dockerfile_uv() {
        let mut project_info = project_info_dummy();
        project_info.project_manager = ProjectManager::Uv;
        let base = project_info.base_dir();
        create_dir_all(&base).unwrap();
        let expected_file = base.join("Dockerfile");
        save_dockerfile(&project_info).unwrap();

        assert!(expected_file.is_file());

        let content = std::fs::read_to_string(expected_file).unwrap();

        assert_yaml_snapshot!(content);
    }

    #[test]
    fn test_save_dockerfile_poetry() {
        let mut project_info = project_info_dummy();
        project_info.project_manager = ProjectManager::Poetry;
        let base = project_info.base_dir();
        create_dir_all(&base).unwrap();
        let expected_file = base.join("Dockerfile");
        save_dockerfile(&project_info).unwrap();

        assert!(expected_file.is_file());

        let content = std::fs::read_to_string(expected_file).unwrap();

        assert_yaml_snapshot!(content);
    }

    #[test]
    fn test_save_dockerfile_setuptools() {
        let mut project_info = project_info_dummy();
        project_info.project_manager = ProjectManager::Setuptools;
        let base = project_info.base_dir();
        create_dir_all(&base).unwrap();
        let expected_file = base.join("Dockerfile");
        save_dockerfile(&project_info).unwrap();

        assert!(expected_file.is_file());

        let content = std::fs::read_to_string(expected_file).unwrap();

        assert_yaml_snapshot!(content);
    }

    #[test]
    fn test_save_dockerfile_maturin_uv() {
        let mut project_info = project_info_dummy();
        project_info.project_manager = ProjectManager::Maturin;
        project_info.pyo3_python_manager = Some(Pyo3PythonManager::Uv);
        let base = project_info.base_dir();
        create_dir_all(&base).unwrap();
        let expected_file = base.join("Dockerfile");
        save_dockerfile(&project_info).unwrap();

        assert!(expected_file.is_file());

        let content = std::fs::read_to_string(expected_file).unwrap();

        assert_yaml_snapshot!(content);
    }

    #[test]
    fn test_save_dockerfile_maturin_setuptools() {
        let mut project_info = project_info_dummy();
        project_info.project_manager = ProjectManager::Maturin;
        project_info.pyo3_python_manager = Some(Pyo3PythonManager::Setuptools);
        let base = project_info.base_dir();
        create_dir_all(&base).unwrap();
        let expected_file = base.join("Dockerfile");
        save_dockerfile(&project_info).unwrap();

        assert!(expected_file.is_file());

        let content = std::fs::read_to_string(expected_file).unwrap();

        assert_yaml_snapshot!(content);
    }
}