Gin
설치
go get -u github.com/gin-gonic/gin
Config
go get -u github.com/caarlos0/env/v6
go get -u github.com/joho/godotenv
.env
DEBUG=true
PORT=8080
POD_NAME=gin-test
POD_NAMESPACE=localhost
"internal/config/config.go
package config
import (
"fmt"
"github.com/caarlos0/env/v6"
"github.com/joho/godotenv"
)
type config struct {
Debug bool `env:"DEBUG" envDefault:"false"`
Port int `env:"PORT" envDefault:"8080"`
K8s podConfig `envPrefix:"POD_"`
}
type podConfig struct {
Name string `env:"NAME,required"`
Namespace string `env:"NAMESPACE,required"`
}
var c config
func Init() {
var err error
err = godotenv.Load()
if err != nil {
fmt.Println("failed to load .env", err)
}
err = env.Parse(&c)
if err != nil {
fmt.Println("failed to parse env", err)
}
}
func Config() config {
return c
}
Dockerfile
.dockerignore
.env
Dockerfile
FROM golang:1.19.2-alpine AS build
WORKDIR /app
COPY . /app
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags '-s' -tags=go_json,nomsgpack -buildvcs=false -o app
FROM alpine:3.16.3 AS runtime
WORKDIR /app
COPY /app/app /usr/bin/
ENV PORT 8080
EXPOSE 8080
CMD ["app"]
scratch 이미지를 사용하면 작은 이미지를 만들 수 있지만, 디버깅이 어려울 수 있습니다.