Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/uber fx #5

Merged
merged 4 commits into from
Jan 15, 2024
Merged
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
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
FROM golang:1.20 as develop
ARG BASE_IMAGE="golang:1.21"
ARG PRODUCTION_IMAGE="alpine:3"

FROM $BASE_IMAGE as develop

RUN apt update && \
apt upgrade -y && \
Expand All @@ -24,7 +27,7 @@ EXPOSE 80
EXPOSE 5000
EXPOSE 3000

FROM golang:1.20 as build
FROM $BASE_IMAGE as build

ARG VERSION
ARG APP_NAME
Expand All @@ -37,7 +40,7 @@ RUN apt update && \
apt install make -y && \
make build VERSION=${VERSION} ENV=production APP_NAME=${APP_NAME}

FROM alpine:3 as production
FROM $PRODUCTION_IMAGE as production

ARG APP_NAME

Expand Down
10 changes: 0 additions & 10 deletions Makefile

This file was deleted.

43 changes: 29 additions & 14 deletions app/commands/serve.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
package commands

import (
"time"

"github.com/spf13/cobra"
"go.uber.org/fx"

"github.com/dmalusev/uberfx-common/configfx"
"github.com/dmalusev/uberfx-common/fiber/fiberfx"
"github.com/dmalusev/uberfx-common/loggerfx"

"github.com/BrosSquad/GoFiber-Boilerplate/app/container"
"github.com/BrosSquad/GoFiber-Boilerplate/app/http"
"github.com/BrosSquad/GoFiber-Boilerplate/core/constants"
corehttp "github.com/BrosSquad/GoFiber-Boilerplate/core/http"
"github.com/dmalusev/GoFiber-Boilerplate/app/config"
"github.com/dmalusev/GoFiber-Boilerplate/app/constants"
"github.com/dmalusev/GoFiber-Boilerplate/app/handlers"
)

func loggerSink(cfg *config.Logging) loggerfx.Sink {
return loggerfx.Sink{
Level: cfg.Level,
Type: loggerfx.Stdout,
PrettyPrint: cfg.PrettyPrint,
}
}

func Serve() *cobra.Command {
return &cobra.Command{
Use: "serve",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
di := ctx.Value(constants.ContainerContextKey).(*container.Container)
app := http.CreateApplication(ctx, di, true)
RunE: func(_ *cobra.Command, _ []string) error {
cfg, err := configfx.New[config.Config](constants.AppName)
if err != nil {
return err
}

cfg := di.GetConfig().HTTP
go corehttp.RunServer(cfg.Addr, cfg.Port, app)
app := fx.New(
configfx.Module(cfg),
loggerfx.Module(loggerSink(&cfg.Logging)),
fiberfx.App(constants.AppName, cfg.App.FiberInfo, handlers.Handlers()),
fiberfx.RunApp(cfg.HTTP.Addr, constants.AppName, cfg.HTTP.ShutdownTimeout),
)

<-ctx.Done()
return app.ShutdownWithTimeout(10 * time.Second)
app.Run()
return nil
},
}
}
46 changes: 4 additions & 42 deletions app/config/config.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
package config

import (
"github.com/spf13/viper"

"github.com/BrosSquad/GoFiber-Boilerplate/app/constants"
utilsconfig "github.com/nano-interactive/go-utils/config"
)

type (
App struct {
FiberInfo bool `mapstructure:"fiber_info" json:"fiber_info" yaml:"fiber_info"`
}
Logging struct {
Level string `mapstructure:"level" json:"level" yaml:"level"`
PrettyPrint bool `mapstructure:"pretty_print" json:"pretty_print" yaml:"pretty_print"`
}

HTTP struct {
Addr string `mapstructure:"addr" json:"addr" yaml:"addr"`
Port int `mapstructure:"port" json:"port" yaml:"port"`
}

Config struct {
Logging Logging `mapstructure:"logging" json:"logging" yaml:"logging"`
HTTP HTTP `mapstructure:"http" json:"http" yaml:"http"`
App App `mapstructure:"app" json:"app" yaml:"app"`
}
)

func New() (Config, error) {
cfg := utilsconfig.Config{
ProjectName: constants.AppName,
Name: "config",
Type: "yaml",
Paths: []string{
"$XDG_CONFIG_HOME/" + constants.AppName,
"/etc/" + constants.AppName,
".",
},
}

v, err := utilsconfig.NewWithModifier(cfg)
if err != nil {
return Config{}, err
}

return NewWithViper(v)
}

func NewWithViper(v *viper.Viper) (Config, error) {
c := Config{}

if err := v.Unmarshal(&c); err != nil {
return Config{}, err
}

return c, nil
}
10 changes: 10 additions & 0 deletions app/config/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

import (
"time"
)

type HTTP struct {
Addr string `mapstructure:"addr" json:"addr" yaml:"addr"`
ShutdownTimeout time.Duration `mapstructure:"shutdown_timeout" json:"shutdown_timeout" yaml:"shutdown_timeout"`
}
27 changes: 0 additions & 27 deletions app/container/container.go

This file was deleted.

25 changes: 0 additions & 25 deletions app/container/logger.go

This file was deleted.

13 changes: 13 additions & 0 deletions app/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package handlers

import (
"github.com/dmalusev/GoFiber-Boilerplate/app/handlers/helloworld"
"github.com/dmalusev/uberfx-common/fiber/fiberfx"
)

func Handlers() fiberfx.RoutesFx {
return fiberfx.Routes(
fiberfx.WithRoutes(fiberfx.Get("/", helloworld.HelloWorld)),
fiberfx.WithPrefix("/"),
)
}
File renamed without changes.
12 changes: 0 additions & 12 deletions app/http/handlers.go

This file was deleted.

15 changes: 0 additions & 15 deletions app/http/helloworld/handler_test.go

This file was deleted.

14 changes: 0 additions & 14 deletions app/http/http.go

This file was deleted.

78 changes: 0 additions & 78 deletions app/testutils/testing.go

This file was deleted.

13 changes: 11 additions & 2 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
app:
fiber_info: true

http:
addr: '0.0.0.0'
port: 8080
addr: '0.0.0.0:8080'
shutdown_timeout: 1s
profiler_addr: '0.0.0.0:5000'


logging:
level: debug
pretty_print: true
Loading