Skip to content

Commit

Permalink
feat: update cxx, go, py template
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Sep 12, 2023
1 parent 0d685ec commit b1de8e3
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 12 deletions.
2 changes: 1 addition & 1 deletion template/cxx/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cargo-generate.toml
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
docker-compose.yml
compose.yml
LICENSE*
Makefile
README.md
33 changes: 25 additions & 8 deletions template/cxx/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
.PHONY: dev clean
.PHONY: help
.DEFAULT_GOAL := help

# create a image "{{project-name}}" with tag "latest"
# based on the Dockerfile in the current directory
dev:
docker image build . -t {{project-name}}
docker container run --rm {{project-name}}
APP_NAME := go

clean:
docker image rm {{project-name}}
# build image
image:
@docker image build -t $(APP_NAME) .

# buildx cross

# Show help
help:
@echo ""
@echo "Usage:"
@echo " make [target]"
@echo ""
@echo "Targets:"
@awk '/^[a-zA-Z\-_0-9]+:/ \
{ \
helpMessage = match(lastLine, /^# (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
} \
} { lastLine = $$0 }' $(MAKEFILE_LIST)
12 changes: 12 additions & 0 deletions template/cxx/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: Compose specification

service:
{{project-name}}:
build: .

networks:
{{project-name}}-net:
name: {{project-name}}-net
ipam:
config:
- subnet: 172.16.238.0/24
Empty file removed template/cxx/docker-compose.yml
Empty file.
10 changes: 10 additions & 0 deletions template/go/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# all start with .(dot), including directories and files
.*
cargo-generate.toml
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
compose.yml
LICENSE*
Makefile
README.md
9 changes: 9 additions & 0 deletions template/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM golang:{{go_version}}-alpine AS builder

LABEL author="{{authors}}"

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
2 changes: 1 addition & 1 deletion template/go/cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ prompt = "Project description?"
[placeholders.go_version]
type = "string"
prompt = "Go version? e.g. 1.18, 1.21"
regex = "^[0-9]+(\\.[0-9]+){0,2}$"
regex = "^[0-9]+(\\.[0-9]+){1,2}$"
12 changes: 12 additions & 0 deletions template/go/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: Compose specification

service:
{{project-name}}:
build: .

networks:
{{project-name}}-net:
name: {{project-name}}-net
ipam:
config:
- subnet: 172.16.238.0/24
7 changes: 5 additions & 2 deletions template/py/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# all starts with .(dot), including files and directories
# all start with .(dot), including directories and files
.*
cargo-generate.toml
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
docker-compose.yml
compose.yml
LICENSE*
Makefile
README.md
45 changes: 45 additions & 0 deletions template/py/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
FROM python:{{py_version}} AS venv

LABEL author="{{authors}}"

# set environment variables
# NO Cache
ENV PIP_NO_CACHE_DIR 1
ENV PIP_INDEX_URL https://pypi.tuna.tsinghua.edu.cn/simple

WORKDIR /app

COPY requirement.txt .

RUN python -m venv venv

# TODO using pex or not?
RUN . venv/bin/activate && \
python -m ensurepip -U && \
pip install -r requirement.txt


FROM python:{{py_version}}-slim

LABEL author="{{authors}}"

COPY --from=venv /app/venv /app/venv
ENV PATH /app/venv/bin:$PATH

# set environment variables
# Prevents Python from writing pyc files to disc (equivalent to python -B option)
# https://docs.python.org/3/using/cmdline.html#cmdoption-B
ENV PYTHONDONTWRITEBYTECODE 1
# Prevents Python from buffering stdout and stderr (equivalent to python -u option)
# https://docs.python.org/3/using/cmdline.html#cmdoption-u
ENV PYTHONUNBUFFERED 1

WORKDIR /app

# if something you want ignore it, add it to .dockerignore
COPY . .

HEALTHCHECK --start-period=30s CMD python -c "import requests; requests.get('http://localhost:8000', timeout=2)"

# CMD . .venv/bin/activate && python src/app.py
CMD python src/app.py
27 changes: 27 additions & 0 deletions template/py/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.PHONY: help
.DEFAULT_GOAL := help

APP_NAME := go

# build image
image:
@docker image build -t $(APP_NAME) .

# buildx cross

# Show help
help:
@echo ""
@echo "Usage:"
@echo " make [target]"
@echo ""
@echo "Targets:"
@awk '/^[a-zA-Z\-_0-9]+:/ \
{ \
helpMessage = match(lastLine, /^# (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
} \
} { lastLine = $$0 }' $(MAKEFILE_LIST)
14 changes: 14 additions & 0 deletions template/py/cargo-generate.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
[template]
cargo_generate_version = ">=0.10.0"

[placeholders.gh_uname]
type = "string"
prompt = "GitHub username (or organization)?"
regex = "^[A-Za-z0-9][A-Za-z0-9-]{0,38}$"

[placeholders.proj_desc]
type = "string"
prompt = "Project description?"

[placeholders.py_version]
type = "string"
prompt = "Python version? e.g. 3.8, 3.11"
regex = "^[3-9](\\.[0-9]+){1,2}$"
12 changes: 12 additions & 0 deletions template/py/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: Compose specification

service:
{{project-name}}:
build: .

networks:
{{project-name}}-net:
name: {{project-name}}-net
ipam:
config:
- subnet: 172.16.238.0/24
Empty file removed template/py/docker-compose.yml
Empty file.

0 comments on commit b1de8e3

Please sign in to comment.