From ed623c3236da4d705248b225acdb4db4eba9a5ca Mon Sep 17 00:00:00 2001 From: Michael Makarochkin Date: Thu, 27 Feb 2025 01:40:30 +0300 Subject: [PATCH] init --- .gitignore | 1 + .storage/.gitignore | 2 + application/.gitignore | 1 + application/go.mod | 3 ++ application/main.go | 23 +++++++++++ dist/.gitignore | 2 + docker-compose.yml | 30 ++++++++++++++ environment/.gitignore | 3 ++ environment/example.config.yml | 0 log/.gitignore | 2 + services/postgres/data/.gitignore | 2 + services/postgres/init/01-databases.sql | 1 + services/site/Dockerfile | 20 ++++++++++ services/site/build.sh | 42 ++++++++++++++++++++ services/site/rerun.py | 52 +++++++++++++++++++++++++ 15 files changed, 184 insertions(+) create mode 100644 .gitignore create mode 100644 .storage/.gitignore create mode 100644 application/.gitignore create mode 100644 application/go.mod create mode 100644 application/main.go create mode 100644 dist/.gitignore create mode 100644 docker-compose.yml create mode 100644 environment/.gitignore create mode 100644 environment/example.config.yml create mode 100644 log/.gitignore create mode 100644 services/postgres/data/.gitignore create mode 100644 services/postgres/init/01-databases.sql create mode 100644 services/site/Dockerfile create mode 100755 services/site/build.sh create mode 100755 services/site/rerun.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/.storage/.gitignore b/.storage/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/.storage/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/application/.gitignore b/application/.gitignore new file mode 100644 index 0000000..a52d155 --- /dev/null +++ b/application/.gitignore @@ -0,0 +1 @@ +out.log \ No newline at end of file diff --git a/application/go.mod b/application/go.mod new file mode 100644 index 0000000..7e570ea --- /dev/null +++ b/application/go.mod @@ -0,0 +1,3 @@ +module pink_fox + +go 1.24.0 diff --git a/application/main.go b/application/main.go new file mode 100644 index 0000000..5e2ce32 --- /dev/null +++ b/application/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "net/http" + "os" +) + +func main() { + http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprintf(w, "ok") + }) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprintf(w, "Hello world!") + }) + + fmt.Println("Starting server at port 12001...") + if err := http.ListenAndServe(":12001", nil); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/dist/.gitignore b/dist/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/dist/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f780eac --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +services: + + site: + container_name: pink_fox-site + build: + context: ./services/site + dockerfile: Dockerfile + ports: + - "12001:12001" + - "12002:2345" + volumes: + - ./application:/app + - ./dist:/app/dist + - ./environment:/var/environment/pink_fox + - ./log:/var/log/pink_fox + - ./.storage/go:/go + environment: + - ARG1 + + postgres: + image: postgres:16 + environment: + - POSTGRES_USER=pink_fox + - POSTGRES_PASSWORD=pink_fox_pass + volumes: + - ./.storage/postgres:/var/lib/postgresql/data + - ./services/postgres/data:/var/backups/postgres + - ./services/postgres/init:/docker-entrypoint-initdb.d + ports: + - "12003:5432" diff --git a/environment/.gitignore b/environment/.gitignore new file mode 100644 index 0000000..b8d4df0 --- /dev/null +++ b/environment/.gitignore @@ -0,0 +1,3 @@ +* +!.gitignore +!example.config.yml \ No newline at end of file diff --git a/environment/example.config.yml b/environment/example.config.yml new file mode 100644 index 0000000..e69de29 diff --git a/log/.gitignore b/log/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/log/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/services/postgres/data/.gitignore b/services/postgres/data/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/services/postgres/data/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/services/postgres/init/01-databases.sql b/services/postgres/init/01-databases.sql new file mode 100644 index 0000000..9c10394 --- /dev/null +++ b/services/postgres/init/01-databases.sql @@ -0,0 +1 @@ +SELECT 'CREATE DATABASE pink_fox_db with owner pink_fox' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'pink_fox_db')\gexec diff --git a/services/site/Dockerfile b/services/site/Dockerfile new file mode 100644 index 0000000..396d818 --- /dev/null +++ b/services/site/Dockerfile @@ -0,0 +1,20 @@ +FROM ubuntu:22.04 + +RUN apt update && apt upgrade -y +RUN apt install -y wget + +ENV PATH=$PATH:/go/go/bin +ENV PATH=$PATH:/go/bin +ENV GOPATH=/go +ENV GOROOT=/go/go +ENV GOCACHE=/go/.cache/ + +WORKDIR /app + +COPY build.sh / +RUN tr -d '\r' < /build.sh > /build.Unix.sh +RUN rm /build.sh +RUN mv /build.Unix.sh /build.sh +RUN chmod +x /build.sh + +CMD [ "/build.sh" ] diff --git a/services/site/build.sh b/services/site/build.sh new file mode 100755 index 0000000..af020aa --- /dev/null +++ b/services/site/build.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +if [[ ! -d "/go/bin" ]]; then + # если папка bin не существует устанавливаем в систему go + cd / + echo "Собираем golang" >> /app/out.log + wget https://go.dev/dl/go1.24.0.linux-amd64.tar.gz + tar -C /go -xzf go1.24.0.linux-amd64.tar.gz + rm -f go1.24.0.linux-amd64.tar.gz + + CGO_ENABLED=0 go install -ldflags "-s -w -extldflags '-static'" github.com/go-delve/delve/cmd/dlv@latest + + echo "Golang собран" >> /app/out.log +fi + +if [ "$ARG1" = "debug" ]; then + # Приложение готовое для дебага + cd /app || exit 1 + rm -f "./dist/local_app_name" + echo "" > out.log + + pkill -f /app/dist/local_app_name + + go build -gcflags "all=-N -l" -o ./dist/local_app_name 2> /app/out.log + + if [ ! -s "/app/out.log" ]; then + dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec /app/dist/local_app_name > /app/out.log 2>&1 + fi +else + # Простое приложение + cd /app || exit 1 + rm -f "./dist/local_app_name" + echo "" > out.log + + pkill -f /app/dist/local_app_name + + go build -o "./dist/local_app_name" 2> /app/out.log + + if [ ! -s "/app/out.log" ]; then + /app/dist/local_app_name > /app/out.log 2>&1 + fi +fi diff --git a/services/site/rerun.py b/services/site/rerun.py new file mode 100755 index 0000000..91d0d10 --- /dev/null +++ b/services/site/rerun.py @@ -0,0 +1,52 @@ +#!/bin/python3 + +import requests +import time +import subprocess +import sys +import os + +# Останавливаем контейнер без задержки +subprocess.run(["docker", "compose", "stop", "site", "-t", "0"]) + +# Устанавливаем переменные окружения в зависимости от передачи аргумента +env = os.environ.copy() +if len(sys.argv) > 1 and sys.argv[1] == "debug": + env["ARG1"] = "debug" + +# Запускаем сервис через Docker compose +subprocess.run(["docker", "compose", "up", "site", "-d", "--build"], env=env) + +attempt_count = 0 + +while True: + try: + # Отправляем запрос для проверки поднялся ли HTTP сервер + response = requests.get("http://localhost:12001/ping", timeout=5) + + # Проверяем ответ + if response.status_code == 200: + print("Сервер запущен") + break + except requests.ConnectionError: + try: + with open("out.log", 'r') as file: + content = file.read().strip() + if content: + # Лог файл не пуст, возможно, есть ошибки сборки + if content.startswith("API server listening at: [::]:2345"): + # Лог файл начинается с сообщения о старте дебагера, ошибок нет + break + else: + print("Имеется ошибка компиляции, смотри файл out.log") + exit(1) + + except FileNotFoundError: + print("Лог файл не найден, проверьте путь") + print("Текущая директория" + os.getcwd()) + + # Если не удалось подключиться, увеличиваем счетчик попыток + attempt_count += 1 + print(f"Ждем... ({attempt_count})") + + time.sleep(1)