Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 13 additions & 35 deletions .docker/app/.air.toml
Original file line number Diff line number Diff line change
@@ -1,46 +1,24 @@
root = "."
testdata_dir = "testdata"
root = "/app"
tmp_dir = "tmp"

[build]
bin = "./.bin/main"
cmd = "go build -o ./.bin/main cmd/app/main.go"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
cmd = "go build -o ./tmp/main ./cmd/app"
bin = "./tmp/main"
full_bin = "./tmp/main -e .env"
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
exclude_dir = ["assets", "tmp", "vendor", ".git"]
delay = 1000
stop_on_error = true
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
args_bin = ["-e", ".env"]

[log]
time = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false
build = "yellow"
runner = "green"

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
clean_on_exit = true
22 changes: 10 additions & 12 deletions .docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
FROM golang:1.23.2 AS builder
WORKDIR /build
FROM golang:1.24-alpine AS builder

COPY . .

RUN CGO_ENABLED=0 go build -o /build/main ./cmd/app/main.go

FROM gcr.io/distroless/static-debian11:nonroot
WORKDIR /app

COPY --from=builder /build/main /
COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ./cmd/app

ENV PORT=
FROM gcr.io/distroless/static-debian12

EXPOSE ${PORT}
COPY --from=builder /app/server /server

USER 1000
EXPOSE 8080

CMD ["/main"]
ENTRYPOINT ["/server"]
14 changes: 11 additions & 3 deletions .docker/app/local.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
FROM golang:1.22.2 AS app
FROM golang:1.24-alpine

WORKDIR /app

RUN go install github.com/cosmtrek/air@latest
RUN go install github.com/air-verse/air@latest

COPY go.mod go.sum ./
RUN go mod download

COPY . .

EXPOSE 8080

CMD ["air","-c",".docker/app/.air.toml"]
CMD ["air", "-c", ".docker/app/.air.toml"]
9 changes: 6 additions & 3 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# server envrionment variables
HOST=0.0.0.0
# Server
HOST=localhost
PORT=8080
SHUTDOWN_TIMEOUT=5s
SHUTDOWN_TIMEOUT=10s
READ_TIMEOUT=30s
WRITE_TIMEOUT=30s
IDLE_TIMEOUT=60s
Binary file modified README.md
Binary file not shown.
47 changes: 19 additions & 28 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"context"
"flag"
"log/slog"
"net/http"
"os"
"strings"

"github.com/murasame29/go-httpserver-template/cmd/config"
"github.com/murasame29/go-httpserver-template/internal/config"
"github.com/murasame29/go-httpserver-template/internal/container"
"github.com/murasame29/go-httpserver-template/internal/server"
)
Expand All @@ -25,29 +24,28 @@ func (e *envFlag) Set(v string) error {
}

func parseLogLevel(l string) slog.Level {
switch {
case strings.EqualFold(l, "debug") || strings.EqualFold(l, "DEBUG"):
switch strings.ToLower(l) {
case "debug":
return slog.LevelDebug
case strings.EqualFold(l, "info") || strings.EqualFold(l, "INFO"):
case "info":
return slog.LevelInfo
case strings.EqualFold(l, "warn") || strings.EqualFold(l, "WARN"):
case "warn":
return slog.LevelWarn
case strings.EqualFold(l, "error") || strings.EqualFold(l, "ERROR"):
case "error":
return slog.LevelError
default:
return slog.LevelDebug
}
}

func init() {
// Usage: eg. go run main.go -e .env -e hoge.env -e fuga.env ...
func main() {
var (
envFile envFlag
logLevel string
)

flag.Var(&envFile, "e", "path to .env file \n eg. -e .env -e another.env . ")
flag.StringVar(&logLevel, "ll", "debug", "Change the loglevel. The default is debug .")
flag.Var(&envFile, "e", "path to .env file (e.g. -e .env -e another.env)")
flag.StringVar(&logLevel, "ll", "debug", "log level (debug, info, warn, error)")
flag.Parse()

slog.SetDefault(
Expand All @@ -56,33 +54,26 @@ func init() {
})),
)

if err := config.LoadEnv(envFile...); err != nil {
slog.Error("failed to laod env", "error", err)
if err := run(envFile); err != nil {
slog.Error("failed to run application", "error", err)
os.Exit(1)
}
}

func main() {
if err := run(); err != nil {
slog.Error("failed to run application", "error", err)
func run(envFiles []string) error {
cfg, err := config.Load(envFiles...)
if err != nil {
return err
}
}

func run() error {
// サーバーの起動
if err := container.NewContainer(); err != nil {
if err := container.New(cfg); err != nil {
return err
}

handler, err := container.Invoke[http.Handler]()
srv, err := container.Invoke[*server.Server]()
if err != nil {
return err
}

server.New(
handler,
server.WithHost(config.Config.Server.Host),
server.WithPort(config.Config.Server.Port),
).RunWithGracefulShutdown(context.Background())

return nil
return srv.RunWithGracefulShutdown(context.Background())
}
27 changes: 0 additions & 27 deletions cmd/config/config.go

This file was deleted.

15 changes: 0 additions & 15 deletions cmd/config/type.go

This file was deleted.

15 changes: 9 additions & 6 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
services:
backend:
app:
build:
context: .docker/app
dockerfile: local.Dockerfile
platform: linux/amd64
context: .
dockerfile: .docker/app/local.Dockerfile
ports:
- 8080:8080
- "8080:8080"
volumes:
- ./:/app
- .:/app
env_file:
- .env.template
environment:
- HOST=0.0.0.0
15 changes: 1 addition & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
module github.com/murasame29/go-httpserver-template

go 1.22.2
go 1.24.0

require (
github.com/caarlos0/env/v11 v11.2.2
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.12.0
go.uber.org/dig v1.18.0
golang.org/x/sync v0.9.0
)

require (
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
)
43 changes: 2 additions & 41 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,55 +1,16 @@
github.com/caarlos0/env/v11 v11.0.0 h1:ZIlkOjuL3xoZS0kmUJlF74j2Qj8GMOq3CDLX/Viak8Q=
github.com/caarlos0/env/v11 v11.0.0/go.mod h1:2RC3HQu8BQqtEK3V4iHPxj0jOdWdbPpWJ6pOueeU1xM=
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
go.uber.org/dig v1.17.1 h1:Tga8Lz8PcYNsWsyHMZ1Vm0OQOUaJNDyvPImgbAu9YSc=
go.uber.org/dig v1.17.1/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw=
go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading