From fb48a0fa04684224453035b82c97f8f8a2d4c233 Mon Sep 17 00:00:00 2001 From: maix0 Date: Thu, 12 Jun 2025 18:35:04 +0200 Subject: [PATCH] update: scaffolding for nginx --- Docker.mk | 23 ++++++++++++++++-- Makefile | 43 ++++++++++++++++----------------- docker-compose.yml | 36 +++++++++++++++++++++++++++ nginx/17-add-template-prefix.sh | 30 +++++++++++++++++++++++ nginx/Dockerfile | 25 +++++++++++++++++++ nginx/conf/default.conf | 26 ++++++++++++++++++++ nginx/conf/locations/db.conf | 4 +++ 7 files changed, 163 insertions(+), 24 deletions(-) create mode 100644 docker-compose.yml create mode 100755 nginx/17-add-template-prefix.sh create mode 100644 nginx/Dockerfile create mode 100644 nginx/conf/default.conf create mode 100644 nginx/conf/locations/db.conf diff --git a/Docker.mk b/Docker.mk index 1db0334..5f29c24 100644 --- a/Docker.mk +++ b/Docker.mk @@ -6,9 +6,28 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/06/11 18:10:26 by maiboyer #+# #+# # -# Updated: 2025/06/11 18:10:38 by maiboyer ### ########.fr # +# Updated: 2025/06/12 18:29:29 by maiboyer ### ########.fr # # # # **************************************************************************** # all: - echo "yes" + docker compose build + docker compose up + +re: + $(MAKE) -f ./Docker.mk clean + $(MAKE) -f ./Docker.mk all + +clean: + docker compose down + +prune: clean + -if ! [ -z $(shell docker ps -a -q) ] ; then \ + docker stop $(shell docker ps -a -q); \ + docker rm $(shell docker ps -a -q); \ + fi + -docker image prune -a + -docker volume prune + -docker network prune + -docker system prune -a + diff --git a/Makefile b/Makefile index 3400338..63bd3ea 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2025/06/11 18:46:40 by maiboyer ### ########.fr # +# Updated: 2025/06/12 18:34:44 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -18,11 +18,13 @@ NAME=webserv # Colors GREEN = \033[32m +BLACK = \033[30m CYAN = \033[36m GREY = \033[0;90m RED = \033[0;31m GOLD = \033[38;5;220m BROWN = \033[38;2;100;65;23m +WHITE = \033[38;2;255;255;255m END = \033[0m BOLD = \033[1m ITALIC = \033[3m @@ -31,19 +33,24 @@ UNDERLINE = \033[4m BASE_PATH=$(shell realpath .) ECHO = /usr/bin/env echo -PMAKE = -ifndef PMAKE_DISABLE -ifeq ($(shell uname), Linux) - PMAKE = -j$(shell grep -c ^processor /proc/cpuinfo) -endif -ifeq ($(shell uname), Darwin) - PMAKE = -j$(shell sysctl -n hw.ncpu) -endif -endif - all: @$(MAKE) --no-print-directory header - @$(MAKE) --no-print-directory -f ./Docker.mk $(PMAKE) + @$(MAKE) --no-print-directory -f ./Docker.mk + @$(MAKE) --no-print-directory footer + +down: + @$(MAKE) --no-print-directory header + @$(MAKE) --no-print-directory -f ./Docker.mk down + @$(MAKE) --no-print-directory footer + +clean: + @$(MAKE) --no-print-directory header + @$(MAKE) --no-print-directory -f ./Docker.mk clean + @$(MAKE) --no-print-directory footer + +prune: + @$(MAKE) --no-print-directory header + @$(MAKE) --no-print-directory -f ./Docker.mk prune @$(MAKE) --no-print-directory footer $(NAME): all @@ -72,7 +79,7 @@ footer: @$(ECHO) -e '$(GREEN) , \\ / , $(END)' @$(ECHO) -e '$(GREEN) / \\ )\\__/( / \\ $(END)' @$(ECHO) -e '$(GREEN) / \\ (_\\ /_) / \\ $(END)' - @$(ECHO) -e '$(BROWN) ____$(GREEN)/$(BROWN)_____$(GREEN)\\$(BROWN)__$(GREEN)\\@ @/$(BROWN)___$(GREEN)/$(BROWN)_____$(GREEN)\\$(BROWN)____ $(END)' + @$(ECHO) -e '$(BROWN) ____$(GREEN)/$(BROWN)_____$(GREEN)\\$(BROWN)__$(GREEN)\\$(WHITE)@ @$(GREEN)/$(BROWN)___$(GREEN)/$(BROWN)_____$(GREEN)\\$(BROWN)____ $(END)' @$(ECHO) -e '$(BROWN) | $(GREEN)|\\../| $(BROWN) | $(END)' @$(ECHO) -e '$(BROWN) | $(GREEN) \\$(RED)vv$(GREEN)/ $(BROWN) | $(END)' @$(ECHO) -e '$(BROWN) | $(PROJECT__NAME)$(BROWN) | $(END)' @@ -85,17 +92,9 @@ footer: @$(ECHO) -e '$(GREY) The compilation is $(END)$(GOLD)finished$(END)' @$(ECHO) -e '$(GREY) Have a good $(END)$(GOLD)correction$(END)' -# Clean (make clean) -clean: - @$(ECHO) -e '$(GREY) Removing $(END)$(RED)Data dir$(END)' - -# Clean (make fclean) -fclean: clean - @$(ECHO) -e '$(GREY) Removing $(END)$(RED)Program$(END)' - # Restart (make re) re: - @$(MAKE) --no-print-directory fclean + @$(MAKE) --no-print-directory clean @$(MAKE) --no-print-directory all tokei: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f86a02c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +networks: + transcendance-network: + driver: bridge +services: +# service_name: +# build: ./service_name +# container_name: service_name +# image: service_name +# restart: always +# env_file: +# - ${SECRET_DIR}/service_name.env +# networks: +# - transcendance-network +# volumes: +# - service_name-volume:/some_path +# depends_on: +# ports: +# - 'OUTSIDE_PORT:IN_CONTAINER_PORT' + nginx: + build: ./nginx + container_name: nginx + restart: always + networks: + - transcendance-network + ports: + - '8888:443' + enviroment: + - NGINX_DOMAIN=local.maix.me +volumes: +# service_name-vol: +# driver: local +# driver_opts: +# type: 'none' +# o: 'bind' +# device: ${DATA_DIR:-/tmp/ft_deadge}/service_name +# name: "service_name-vol" diff --git a/nginx/17-add-template-prefix.sh b/nginx/17-add-template-prefix.sh new file mode 100755 index 0000000..168d0fe --- /dev/null +++ b/nginx/17-add-template-prefix.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +set -e + +ME=$(basename "$0") + +entrypoint_log() { + if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then + echo "$@" + fi +} + +auto_template() { + local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}" + local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}" + + [ -d "$template_dir" ] || return 0 + if [ ! -w "$template_dir" ]; then + entrypoint_log "$ME: ERROR: $template_dir exists, but is not writable" + return 0 + fi + find "$template_dir" -follow -type f -print | while read -r template; do + entrypoint_log "$ME: adding $suffix to $template" + mv "$template" "$template$suffix" + done +} + +auto_template + +exit 0 diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..a0a0105 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,25 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Dockerfile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: maiboyer +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2025/06/12 16:42:38 by maiboyer #+# #+# # +# Updated: 2025/06/12 18:15:38 by maiboyer ### ########.fr # +# # +# **************************************************************************** # + +FROM nginx:stable-alpine + +RUN mkdir -p /etc/nginx/ && \ + mkdir -p /etc/ssl/certs && \ + mkdir -p /etc/ssl/private && \ + apk add openssl && \ + openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ + -keyout /etc/ssl/private/nginx-selfsigned.key \ + -out /etc/ssl/certs/nginx-selfsigned.crt \ + -subj "/C=FR/OU=student/CN=${NGINX_DOMAIN}" + +COPY ./17-add-template-prefix.sh /docker-entrypoint.d/ +COPY ./conf /etc/nginx/templates diff --git a/nginx/conf/default.conf b/nginx/conf/default.conf new file mode 100644 index 0000000..a09d394 --- /dev/null +++ b/nginx/conf/default.conf @@ -0,0 +1,26 @@ +# this allows the redirection of `http://domain/URL` to `https://domain/URL` +server { + charset UTF-8; + listen 80; + listen [::]:80; + + server_name $NGINX_DOMAIN; + + if ($host = example.com) { + return 301 https://$host$request_uri; + } + return 404; +} + +server { + charset UTF-8; + listen [::]:443 ssl; + listen 443 ssl; + server_name $NGINX_DOMAIN; + + ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt; + ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key; + ssl_protocols TLSv1.3; + + include conf.d/locations/*.conf; +} diff --git a/nginx/conf/locations/db.conf b/nginx/conf/locations/db.conf new file mode 100644 index 0000000..9c93b52 --- /dev/null +++ b/nginx/conf/locations/db.conf @@ -0,0 +1,4 @@ +location /api/db { + add_header Content-Type text/plain; + return 200 'db api yay'; +}