diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 1e2d8b5..eb92924 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -7,7 +7,7 @@ "build" : { "dockerfile": "Dockerfile", "options": [ - "-t=godev" + "-t=godev:latest" ] }, "workspaceMount": "source=${localWorkspaceFolder},target=/home/vscode/app,type=bind,consistency=cached", diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0640904 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM golang:tip-alpine3.22 AS builder +COPY . /app +WORKDIR /app +RUN apk update --no-cache && apk add --no-cache \ + && apk add --no-cache make + +RUN make build + +FROM alpine:3.22.3 AS prod + +ARG PROJECT_NAME=learn +ARG APP_USER=${PROJECT_NAME}_user +ARG APP_UID=10001 +ARG APP_GID=10001 + + +RUN addgroup -g ${APP_GID} ${APP_USER} && \ + adduser -u ${APP_UID} \ + -G ${APP_USER} \ + -S \ + -H \ + -D ${APP_USER} + +COPY --from=builder /app/bin /srv/${PROJECT_NAME} +RUN chown -R ${APP_USER}:${APP_USER} /srv/ +USER ${APP_USER} +WORKDIR /srv/${PROJECT_NAME} + +CMD ["./learn"] \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5e8eae4 --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +# Переменные +# Имя бинарника (измените на ваше) +BINARY_NAME := learn +# Выходной каталог +BUILD_DIR := bin +# Директория с конфигами (например, config.yaml) +CONFIG_DIR := config +# Все Go-файлы +GO_SRC := $(shell find . -name '*.go') +GO_MOD := go.mod go.sum +VERSION := $(shell git describe --tags --always) +# Цель по умолчанию: сборка +all: build + +# Сборка бинарника и копирование файлов +build: $(BUILD_DIR)/$(BINARY_NAME) + @echo "Build completed" + +$(BUILD_DIR)/$(BINARY_NAME): $(GO_SRC) $(GO_MOD) + @mkdir -p $(BUILD_DIR) +# Измените ./cmd/myapp на путь к main.go + CGO_ENABLED=0 GOARCH=amd64 go build -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/app/main.go +# Копируем конфиги (добавьте другие файлы по аналогии, e.g. cp static/* $(BUILD_DIR)/) + @mkdir $(BUILD_DIR)/$(CONFIG_DIR)/ + @cp -r $(CONFIG_DIR)/*.yml $(BUILD_DIR)/$(CONFIG_DIR)/ + +# Тестирование +test: + go test -v ./... + +# Очистка +clean: + rm -rf $(BUILD_DIR) + go clean + +# Docker-сборка (пример, если нужно собрать образ) +docker-build: build + docker build -t $(BINARY_NAME):latest -f Dockerfile . + docker build -t $(BINARY_NAME):$(VERSION) -f Dockerfile . + +.PHONY: all build test clean docker-build \ No newline at end of file diff --git a/bin/config/config.yml b/bin/config/config.yml new file mode 100644 index 0000000..01229fc --- /dev/null +++ b/bin/config/config.yml @@ -0,0 +1 @@ +API_KEY: some_secret_api_key_sss \ No newline at end of file diff --git a/bin/learn b/bin/learn new file mode 100755 index 0000000..a342738 Binary files /dev/null and b/bin/learn differ diff --git a/cmd/app/main.go b/cmd/app/main.go index 6d0bfc8..b26818c 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -1,9 +1,20 @@ package main -import "fmt" +import ( + "fmt" + "log" + + "github.com/spf13/viper" +) func main() { - fmt.Println("Hello world") + viper.AddConfigPath("config") + err := viper.ReadInConfig() + if err != nil { + log.Fatalf("Error loading file: %v", err.Error()) + } + api_key := viper.GetString("API_KEY") + fmt.Println("Hello world, and API_KEY:", api_key) c := fmt.Sprintf("Test text: %s", "formatted") fmt.Printf("Some text: %s", c) } diff --git a/config/config.yml b/config/config.yml new file mode 100644 index 0000000..01229fc --- /dev/null +++ b/config/config.yml @@ -0,0 +1 @@ +API_KEY: some_secret_api_key_sss \ No newline at end of file diff --git a/go.mod b/go.mod index 24d3044..8659af7 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,19 @@ module learn go 1.25.4 + +require ( + github.com/fsnotify/fsnotify v1.9.0 // indirect + github.com/go-viper/mapstructure/v2 v2.4.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/sagikazarmark/locafero v0.11.0 // indirect + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect + github.com/spf13/afero v1.15.0 // indirect + github.com/spf13/cast v1.10.0 // indirect + github.com/spf13/pflag v1.0.10 // indirect + github.com/spf13/viper v1.21.0 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.yaml.in/yaml/v3 v3.0.4 // indirect + golang.org/x/sys v0.29.0 // indirect + golang.org/x/text v0.28.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..58a7da3 --- /dev/null +++ b/go.sum @@ -0,0 +1,27 @@ +github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= +github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= +github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc= +github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik= +github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= +github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U= +github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I= +github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg= +github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= +github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= +github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= +golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=