Skip to content

Commit a539f49

Browse files
committed
refactor
1 parent 75e90e6 commit a539f49

File tree

10 files changed

+37
-40
lines changed

10 files changed

+37
-40
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ The boilerplate consists of:
2525
## Go package and file structure
2626
The project structure attempts to be on a par with [golang-standards/project-layout](https://github.com/golang-standards/project-layout).
2727

28-
> Every local package that collides with a golang std package is named with `x` postfix. For example `.../internal/httpx` or `.../internal/logx`. By doing this cheat you can avoid putting aliases when you need both of those and you can take advantage of package autocomplete/autoimport features more easily than having colliding names.
28+
> Every local package that collides with a golang std package is named with `l` postfix. For example `.../internal/zhttp` or `.../internal/zlog`. By doing this cheat you can avoid putting aliases when you need both of those and you can take advantage of package autocomplete/autoimport features more easily than having colliding names.
2929
3030
| Package | Description |
3131
|--------------------|-----------------------------------------|
32-
| `cmd/*/main.go` | main function that is intended to glue together the most core level components like: configuration, http server and router, logs initialization, db connections (if any), the `App` and finally the `Main` struct (`/internal/exec.go`) that handles the long running / signal handling / graceful shutdown of the service. |
33-
| `internal/exec.go` | the `Main` struct that wraps the long running / signal handling / graceful shutdown of the service |
32+
| `cmd/*/main.go` | main function that is intended to glue together the most core level components like: configuration, http server and router, logs initialization, db connections (if any), the `App` and finally the `Daemon` struct (`/internal/exec.go`) that handles the long running / signal handling / graceful shutdown of the service. |
3433
| `internal/config` | this package contain the initialization of `koanf` config. |
35-
| `internal/httpx` | this package contains: the setup of the `chi` router, some helpers functions for parsing/writing http request and http response. |
36-
| `internal/logx` | this package contains the setup/init function for `slog` logger. |
34+
| `internal/zhttp` | this package contains: the setup of the `chi` router, some helpers functions for parsing/writing http request and http response. |
35+
| `internal/zlog` | this package contains the setup/init function for `slog` logger. |
3736

3837
| Folder/File | Description |
3938
|--------------------|-----------------------------------------|

cmd/goboilerplate/main.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55
"fmt"
66
"log/slog"
7+
"maps"
78
"os"
89

910
"github.com/ifnotnil/daemon"
1011
"github.com/moukoublen/goboilerplate/internal/config"
11-
"github.com/moukoublen/goboilerplate/internal/httpx"
12-
"github.com/moukoublen/goboilerplate/internal/logx"
12+
"github.com/moukoublen/goboilerplate/internal/zhttp"
13+
"github.com/moukoublen/goboilerplate/internal/zlog"
1314
)
1415

1516
func defaultConfigs() map[string]any {
@@ -18,43 +19,41 @@ func defaultConfigs() map[string]any {
1819
}
1920

2021
gather := []map[string]any{
21-
httpx.DefaultConfigValues(),
22-
logx.DefaultConfigValues(),
22+
zhttp.DefaultConfigValues(),
23+
zlog.DefaultConfigValues(),
2324
}
2425

2526
for _, g := range gather {
26-
for k, v := range g {
27-
defaults[k] = v
28-
}
27+
maps.Copy(defaults, g)
2928
}
3029

3130
return defaults
3231
}
3332

3433
func main() {
3534
// pre-init slog with default config
36-
logger := logx.InitSLog(logx.Config{LogType: logx.LogTypeText, Level: slog.LevelInfo})
35+
logger := zlog.InitSLog(zlog.Config{LogType: zlog.LogTypeText, Level: slog.LevelInfo})
3736
logger.Info("starting up...")
3837

3938
cnf, err := config.Load(context.Background(), "APP_", defaultConfigs())
4039
if err != nil {
41-
logger.Error("error during config init", logx.Error(err))
40+
logger.Error("error during config init", zlog.Error(err))
4241
os.Exit(1)
4342
}
4443

45-
logger = logx.InitSLog(logx.ParseConfig(cnf))
44+
logger = zlog.InitSLog(zlog.ParseConfig(cnf))
4645

4746
dmn := daemon.Start(
4847
context.Background(),
4948
daemon.WithLogger(logger),
5049
daemon.WithShutdownGraceDuration(cnf.Duration("shutdown_timeout")),
5150
)
5251

53-
httpConf := httpx.ParseConfig(cnf)
54-
router := httpx.NewDefaultRouter(dmn.CTX(), httpConf, logger)
52+
httpConf := zhttp.ParseConfig(cnf)
53+
router := zhttp.NewDefaultRouter(dmn.CTX(), httpConf, logger)
5554

5655
// init services / application
57-
server := httpx.StartListenAndServe(
56+
server := zhttp.StartListenAndServe(
5857
fmt.Sprintf("%s:%d", httpConf.IP, httpConf.Port),
5958
router,
6059
httpConf.ReadHeaderTimeout,
@@ -67,7 +66,7 @@ func main() {
6766
func(ctx context.Context) {
6867
logger.InfoContext(ctx, "shuting down http server")
6968
if err := server.Shutdown(ctx); err != nil {
70-
logger.Warn("error during http server shutdown", logx.Error(err))
69+
logger.Warn("error during http server shutdown", zlog.Error(err))
7170
}
7271
},
7372
)

internal/config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ import (
1111
"github.com/knadh/koanf/providers/env"
1212
"github.com/knadh/koanf/providers/file"
1313
"github.com/knadh/koanf/v2"
14-
"github.com/moukoublen/goboilerplate/internal/logx"
14+
"github.com/moukoublen/goboilerplate/internal/zlog"
1515
)
1616

1717
func Load(ctx context.Context, envVarPrefix string, defaultConfigs map[string]any) (*koanf.Koanf, error) {
18-
logger := logx.GetFromContext(ctx)
18+
logger := zlog.GetFromContext(ctx)
1919

2020
const delim = "."
2121
k := koanf.New(delim)
2222

2323
// Load default values.
2424
if err := k.Load(confmap.Provider(defaultConfigs, delim), nil); err != nil {
25-
logger.DebugContext(ctx, "error during config loading from defaults", logx.Error(err))
25+
logger.DebugContext(ctx, "error during config loading from defaults", zlog.Error(err))
2626
}
2727

2828
// Load YAML config.
2929
if err := k.Load(file.Provider("config.yaml"), yaml.Parser()); err != nil {
30-
logger.DebugContext(ctx, "error during config loading from yaml file", logx.Error(err))
30+
logger.DebugContext(ctx, "error during config loading from yaml file", zlog.Error(err))
3131
if !errors.Is(err, fs.ErrNotExist) {
3232
return k, err
3333
}
@@ -39,12 +39,12 @@ func Load(ctx context.Context, envVarPrefix string, defaultConfigs map[string]an
3939
"log": map[string]any{},
4040
}
4141
if err := k.Load(env.Provider(envVarPrefix, delim, buildEnvVarsNamesMapper(envVarsLevels, envVarPrefix)), nil); err != nil {
42-
logger.WarnContext(ctx, "error during config loading from env vars", logx.Error(err))
42+
logger.WarnContext(ctx, "error during config loading from env vars", zlog.Error(err))
4343
}
4444

4545
// Load .env file
4646
if err := k.Load(file.Provider(".env"), dotenv.ParserEnv(envVarPrefix, delim, buildEnvVarsNamesMapper(envVarsLevels, envVarPrefix))); err != nil {
47-
logger.WarnContext(ctx, "error during config loading from dot env file", logx.Error(err))
47+
logger.WarnContext(ctx, "error during config loading from dot env file", zlog.Error(err))
4848
if !errors.Is(err, fs.ErrNotExist) {
4949
return k, err
5050
}

internal/httpx/handlers.go renamed to internal/zhttp/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpx
1+
package zhttp
22

33
import (
44
"net/http"
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package httpx
1+
package zhttp
22

33
import (
44
"context"
55
"encoding/json"
66
"net/http"
77

8-
"github.com/moukoublen/goboilerplate/internal/logx"
8+
"github.com/moukoublen/goboilerplate/internal/zlog"
99
)
1010

1111
// RespondJSON renders a json response using a json encoder directly over the ResponseWriter.
@@ -17,8 +17,8 @@ func RespondJSON(ctx context.Context, w http.ResponseWriter, statusCode int, bod
1717

1818
if body != nil {
1919
if err := json.NewEncoder(w).Encode(body); err != nil {
20-
logger := logx.GetFromContext(ctx)
21-
logger.ErrorContext(ctx, "error during response encoding", logx.Error(err))
20+
logger := zlog.GetFromContext(ctx)
21+
logger.ErrorContext(ctx, "error during response encoding", zlog.Error(err))
2222
}
2323
}
2424
}

internal/httpx/mock_http_client_test.go renamed to internal/zhttp/mock_http_client_test.go

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/httpx/outbound.go renamed to internal/zhttp/outbound.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpx
1+
package zhttp
22

33
import (
44
"fmt"

internal/httpx/router.go renamed to internal/zhttp/router.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpx
1+
package zhttp
22

33
import (
44
"context"
@@ -12,7 +12,7 @@ import (
1212
"github.com/go-chi/chi/v5/middleware"
1313
xhttp "github.com/ifnotnil/x/http"
1414
"github.com/knadh/koanf/v2"
15-
"github.com/moukoublen/goboilerplate/internal/logx"
15+
"github.com/moukoublen/goboilerplate/internal/zlog"
1616
)
1717

1818
func DefaultConfigValues() map[string]any {
@@ -70,7 +70,7 @@ func NewDefaultRouter(ctx context.Context, c Config, logger *slog.Logger) *chi.M
7070
}
7171

7272
func LogRoutes(ctx context.Context, r *chi.Mux) {
73-
logger := logx.GetFromContext(ctx)
73+
logger := zlog.GetFromContext(ctx)
7474
if !logger.Enabled(ctx, slog.LevelDebug) {
7575
return
7676
}
@@ -85,7 +85,7 @@ func LogRoutes(ctx context.Context, r *chi.Mux) {
8585
}
8686

8787
if err := chi.Walk(r, walkFunc); err != nil {
88-
logger.ErrorContext(ctx, "error during chi walk", logx.Error(err))
88+
logger.ErrorContext(ctx, "error during chi walk", zlog.Error(err))
8989
} else {
9090
logger.DebugContext(ctx, "http routes", slog.Any("routes", routes))
9191
}

internal/httpx/router_test.go renamed to internal/zhttp/router_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpx
1+
package zhttp
22

33
import (
44
"encoding/json"

internal/logx/log.go renamed to internal/zlog/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package logx
1+
package zlog
22

33
import (
44
"context"

0 commit comments

Comments
 (0)