Skip to content

Commit

Permalink
fix(lint): some issues (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
WildEgor authored Jun 28, 2024
1 parent 85deb34 commit f89e8b1
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/linters/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ linters-settings:
gci:
custom-order: true
sections:
- standard
- default
- standard
- prefix(github.com/WildEgor/e-shop-fiber-microservice-boilerplate)
staticcheck:
checks:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
VALIDATE_ALL_CODEBASE: ${{ github.event_name != 'pull_request' }}
DEFAULT_BRANCH: develop
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FILTER_REGEX_EXCLUDE: '.*(assets/.*|\.goreleaser\.yml)'
FILTER_REGEX_EXCLUDE: '.*(assets/.*|\.goreleaser\.yml|api/.*\.(yml|json)|README\.md|test/request/.*)'
# go validator works pretty bad in super-linter, we'll use the original one
VALIDATE_GO: false
# do not validate SQL - linters are pretty useless in case of this library,
Expand Down
20 changes: 10 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ LABEL maintainer="YOUR_NAME <YOUR_EMAIL>"
#RUN git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/"
WORKDIR /app
COPY ./go.mod ./go.sum ./
RUN mkdir -p dist
RUN go mod download
RUN mkdir -p dist && \
go mod download

# Development Stage
FROM base as dev
WORKDIR /app/
COPY . .
RUN go install github.com/air-verse/air@latest
RUN go build -o dist/app cmd/main.go
RUN go install github.com/air-verse/air@latest && \
go build -o dist/app cmd/main.go
CMD ["air", "-c", ".air-unix.toml", "-d"]

# Debug stage
FROM base as debug
WORKDIR /
COPY . .
RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN go build -gcflags="all=-N -l" -o /app/app cmd/main.go
RUN mv /go/bin/dlv /
RUN go install github.com/go-delve/delve/cmd/dlv@latest && \
go build -gcflags="all=-N -l" -o /app/app cmd/main.go && \
mv /go/bin/dlv /
CMD ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/app/app"]

# Build Production Stage
FROM base as build
WORKDIR /app
COPY . .
RUN go install github.com/swaggo/swag/cmd/swag@latest
RUN $GOPATH/bin/swag init -g cmd/main.go --output docs
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o dist/app cmd/main.go
RUN go install github.com/swaggo/swag/cmd/swag@latest && \
$GOPATH/bin/swag init -g cmd/main.go --output docs && \
CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o dist/app cmd/main.go

# Production Stage
FROM cgr.dev/chainguard/busybox:latest-glibc as prod
Expand Down
6 changes: 5 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func main() {
)
defer done()

srv, _ := server.NewServer()
srv, err := server.NewServer()
if err != nil {
panic(err)
}

srv.Run(ctx)

<-ctx.Done()
Expand Down
4 changes: 2 additions & 2 deletions internal/adapters/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package adapters

import "github.com/google/wire"

// AdaptersSet contains "adapters" to 3th party systems
var AdaptersSet = wire.NewSet()
// Set contains "adapters" to 3th party systems
var Set = wire.NewSet()
23 changes: 17 additions & 6 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
// AppSet link main app deps
var AppSet = wire.NewSet(
NewApp,
configs.ConfigsSet,
routers.RouterSet,
configs.Set,
routers.Set,
)

// Server represents the main server configuration.
Expand Down Expand Up @@ -53,7 +53,7 @@ func (srv *Server) Run(ctx context.Context) {

if ac.IsDebug() && srv.Pyro == nil {
slog.Info("pyro start")
srv.Pyro, _ = pyroscope.Start(pyroscope.Config{
pyro, err := pyroscope.Start(pyroscope.Config{
ApplicationName: srv.AppConfig.Name,
ServerAddress: srv.ProfilerConfig.API,
Logger: pyroscope.StandardLogger,
Expand All @@ -65,20 +65,30 @@ func (srv *Server) Run(ctx context.Context) {
pyroscope.ProfileInuseSpace,
},
})

if err != nil {
slog.Error("cannot start pyro client", slog.Any("err", err))
}

srv.Pyro = pyro
}

if !ac.IsDebug() {
slog.Info("pyro stop")
if srv.Pyro != nil {
srv.Pyro.Stop()
err := srv.Pyro.Stop()
if err != nil {
slog.Warn("cannot stop pyro client", slog.Any("err", err))
return
}
}
}

}

srv.AppConfig.OnChanged(pr)

if err := srv.App.Listen(fmt.Sprintf(":%s", srv.AppConfig.HttpPort), fiber.ListenConfig{
if err := srv.App.Listen(fmt.Sprintf(":%s", srv.AppConfig.HTTPPort), fiber.ListenConfig{
DisableStartupMessage: false,
EnablePrintRoutes: false,
OnShutdownSuccess: func() {
Expand All @@ -102,9 +112,10 @@ func (srv *Server) Shutdown(ctx context.Context) {
}
}

// NewApp init app
func NewApp(
ac *configs.AppConfig,
lc *configs.LoggerConfig,
lc *configs.LoggerConfig, // init logger
pc *configs.ProfilerConfig,
c *configs.Configurator,
eh *eh.ErrorsHandler,
Expand Down
4 changes: 3 additions & 1 deletion internal/configs/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
type AppConfig struct {
Name string `mapstructure:"name"`
Mode string `mapstructure:"mode"`
HttpPort string `mapstructure:"http_port"`
HTTPPort string `mapstructure:"http_port"`
changer func(ac *AppConfig)
}

// NewAppConfig creates app config
func NewAppConfig(c *Configurator) *AppConfig {
cfg := &AppConfig{}

Expand All @@ -34,6 +35,7 @@ func NewAppConfig(c *Configurator) *AppConfig {
return cfg
}

// OnChanged register callback
func (ac *AppConfig) OnChanged(fn func(ac *AppConfig)) {
ac.changer = fn
}
Expand Down
6 changes: 4 additions & 2 deletions internal/configs/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Configurator struct {
watchers []func()
}

// NewConfigurator create new Configurator
func NewConfigurator() *Configurator {
c := &Configurator{
watchers: make([]func(), 0),
Expand All @@ -21,18 +22,19 @@ func NewConfigurator() *Configurator {
return c
}

// Watch config
func (c *Configurator) Watch() {
viper.WatchConfig()

viper.OnConfigChange(func(e fsnotify.Event) {
slog.Info(fmt.Sprintf("watchers len: %d", len(c.watchers)))

for _, watcher := range c.watchers {
watcher()
}
})
viper.WatchConfig()
}

// Register watcher
func (c *Configurator) Register(name string, fn func()) {
slog.Info("register watcher", slog.Any("value", name))
c.watchers = append(c.watchers, fn)
Expand Down
4 changes: 3 additions & 1 deletion internal/configs/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

var (
// LogJsonFormat specify json format
LogJsonFormat string = "json"
)

Expand All @@ -15,12 +16,13 @@ var logLevelToSlogLevel = map[string]slog.Leveler{
"info": slog.LevelInfo,
}

// ProfilerConfig holds logger configurations
// LoggerConfig holds logger configurations
type LoggerConfig struct {
Level string `mapstructure:"level"`
Format string `mapstructure:"format"`
}

// NewLoggerConfig create logger config
func NewLoggerConfig(c *Configurator) *LoggerConfig {
cfg := &LoggerConfig{}

Expand Down
1 change: 1 addition & 0 deletions internal/configs/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ProfilerConfig struct {
API string `mapstructure:"api"`
}

// NewProfilerConfig create profiler config
func NewProfilerConfig(c *Configurator) *ProfilerConfig {
cfg := &ProfilerConfig{}

Expand Down
4 changes: 2 additions & 2 deletions internal/configs/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package configs

import "github.com/google/wire"

// ConfigsSet contains project configs
var ConfigsSet = wire.NewSet(
// Set contains project configs
var Set = wire.NewSet(
NewConfigurator,
NewAppConfig,
NewLoggerConfig,
Expand Down
2 changes: 2 additions & 0 deletions internal/handlers/errors/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
type ErrorsHandler struct {
}

// NewErrorsHandler creates new handler
func NewErrorsHandler() *ErrorsHandler {
return &ErrorsHandler{}
}

// Handle errors
func (hch *ErrorsHandler) Handle(ctx fiber.Ctx, err error) error {
sc := fiber.StatusInternalServerError
var e *fiber.Error
Expand Down
3 changes: 3 additions & 0 deletions internal/handlers/health_check/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"github.com/gofiber/fiber/v3"
)

// HealthCheckHandler represent handler
type HealthCheckHandler struct {
}

// NewHealthCheckHandler creates new handler
func NewHealthCheckHandler() *HealthCheckHandler {
return &HealthCheckHandler{}
}

// Handle health
// HealthCheck godoc
// @Summary Health check service
// @Description Health check service
Expand Down
13 changes: 10 additions & 3 deletions internal/handlers/ping/handler.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package ping_handler

import (
"crypto/rand"
"errors"
"github.com/gofiber/fiber/v3"
"math/rand"
)

// PingCheckHandler represent ping handler
type PingCheckHandler struct {
}

// NewPingHandler creates new handler
func NewPingHandler() *PingCheckHandler {
return &PingCheckHandler{}
}

// Handle ping
// Ping godoc
// @Summary Dummy ping
// @Description Just random return 200 OK or error
Expand All @@ -22,8 +25,12 @@ func NewPingHandler() *PingCheckHandler {
// @Success 200
// @Router /api/v1/ping [get]
func (hch *PingCheckHandler) Handle(ctx fiber.Ctx) error {

n := rand.Int()
c := 10
b := make([]byte, c)
n, err := rand.Read(b)
if err != nil {
return errors.New("error")
}

if n%2 == 0 {
return errors.New("ooops")
Expand Down
3 changes: 3 additions & 0 deletions internal/handlers/ready_check/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"github.com/gofiber/fiber/v3"
)

// ReadyCheckHandler represent ready check
type ReadyCheckHandler struct {
}

// NewReadyCheckHandler creates new handler
func NewReadyCheckHandler() *ReadyCheckHandler {
return &ReadyCheckHandler{}
}

// Handle ready
// HealthCheck godoc
// @Summary Ready check service
// @Description Ready check service
Expand Down
20 changes: 10 additions & 10 deletions internal/handlers/wire.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package handlers

import (
error_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/errors"
health_check_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/health_check"
ping_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/ping"
ready_check_handler "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/ready_check"
eh "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/errors"
hch "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/health_check"
ph "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/ping"
rh "github.com/WildEgor/e-shop-fiber-microservice-boilerplate/internal/handlers/ready_check"
"github.com/google/wire"
)

// HandlersSet contains http/amqp/etc handlers (acts like facades)
var HandlersSet = wire.NewSet(
error_handler.NewErrorsHandler,
health_check_handler.NewHealthCheckHandler,
ready_check_handler.NewReadyCheckHandler,
ping_handler.NewPingHandler,
// Set contains http/amqp/etc handlers (acts like facades)
var Set = wire.NewSet(
eh.NewErrorsHandler,
hch.NewHealthCheckHandler,
rh.NewReadyCheckHandler,
ph.NewPingHandler,
)
1 change: 1 addition & 0 deletions internal/middlewares/not_found/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package not_found_middleware

import "github.com/gofiber/fiber/v3"

// NewNotFound create 404 handler
func NewNotFound() fiber.Handler {
return func(ctx fiber.Ctx) error {
return ctx.Render("not_found", fiber.Map{
Expand Down
5 changes: 5 additions & 0 deletions internal/routers/private_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"github.com/gofiber/fiber/v3"
)

var _ Router[*fiber.App] = (*PrivateRouter)(nil)

// PrivateRouter router
type PrivateRouter struct {
}

// NewPrivateRouter creates router
func NewPrivateRouter() *PrivateRouter {
return &PrivateRouter{}
}

// Setup router
func (r *PrivateRouter) Setup(app *fiber.App) {
}
Loading

0 comments on commit f89e8b1

Please sign in to comment.