Compare commits

..

3 Commits

Author SHA1 Message Date
5e183f950f add dockerfile with multistage 2026-02-03 04:00:38 +03:00
25fc35eb3a lll 2026-02-02 11:45:04 +03:00
cbfe2f80bc Add dev container config 2026-02-02 02:49:54 +00:00
12 changed files with 217 additions and 2 deletions

30
.devcontainer/Dockerfile Normal file
View File

@ -0,0 +1,30 @@
FROM golang:tip-alpine3.22
ARG PROJECT_NAME=learn
ARG USERNAME=vscode
ARG USER_ID=1000
ARG USER_GID=${USER_ID}
RUN apk update --no-cache && apk add --no-cache \
git \
curl \
wget \
unzip \
zsh \
&& rm -rf /var/lib/apt/lists/* \
&& go install github.com/go-delve/delve/cmd/dlv@latest \
&& go install -v golang.org/x/tools/gopls@latest \
&& addgroup -g ${USER_GID} ${USERNAME} \
&& adduser -D -u ${USER_ID} \
-G ${USERNAME} \
-s /bin/zsh \
${USERNAME} \
&& mkdir -p /home/$USERNAME/.vscode-server \
&& chown -R ${USER_ID}:${USER_ID} /home/${USERNAME}
USER ${USERNAME}
WORKDIR /srv/${PROJECT_NAME}
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

View File

@ -0,0 +1,57 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/go .
{
"name": "Go dev container",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
// "image": "mcr.microsoft.com/devcontainers/go:2-1.25-trixie"
"build" : {
"dockerfile": "Dockerfile",
"options": [
"-t=godev:latest"
]
},
"workspaceMount": "source=${localWorkspaceFolder},target=/home/vscode/app,type=bind,consistency=cached",
"workspaceFolder": "/home/vscode/app",
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",
// Configure tool-specific properties.
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.profiles.linux": {
"zsh (login)": {
"path": "zsh",
"icon": "terminal-linux",
"args": ["-l"]
}
},
"terminal.integrated.defaultProfile.linux": "zsh (login)"
},
"extensions": [
"golang.go",
"ms-vscode-remote.remote-containers",
"aaron-bond.better-comments",
"qwtel.sqlite-viewer",
"oderwat.indent-rainbow",
"jinliming2.vscode-go-template",
"tamasfe.even-better-toml",
"mechatroner.rainbow-csv",
"Gruntfuggly.todo-tree",
"eamodio.gitlens"
]
},
"extensions": [
]
},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "vscode"
}

1
.vscode/launch.json vendored
View File

@ -5,6 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",

29
Dockerfile Normal file
View File

@ -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"]

41
Makefile Normal file
View File

@ -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

1
bin/config/config.yml Normal file
View File

@ -0,0 +1 @@
API_KEY: some_secret_api_key_sss

BIN
bin/learn Executable file

Binary file not shown.

View File

@ -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)
}

1
cmd/app/test Normal file
View File

@ -0,0 +1 @@
asdasd

1
config/config.yml Normal file
View File

@ -0,0 +1 @@
API_KEY: some_secret_api_key_sss

16
go.mod
View File

@ -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
)

27
go.sum Normal file
View File

@ -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=