Skip to content

Commit

Permalink
Initial working impl for Uber FX
Browse files Browse the repository at this point in the history
Signed-off-by: Dusan Malusev <dusan@dusanmalusev.dev>
  • Loading branch information
CodeLieutenant committed Nov 19, 2023
1 parent 71aee20 commit 13c9ab9
Show file tree
Hide file tree
Showing 29 changed files with 507 additions and 521 deletions.
10 changes: 0 additions & 10 deletions Makefile

This file was deleted.

43 changes: 27 additions & 16 deletions app/commands/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@ package commands

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

"github.com/BrosSquad/GoFiber-Boilerplate/app/http"
"github.com/BrosSquad/GoFiber-Boilerplate/core/container"
"github.com/BrosSquad/GoFiber-Boilerplate/core/http/httpfx"
"github.com/BrosSquad/GoFiber-Boilerplate/app/config"
"github.com/BrosSquad/GoFiber-Boilerplate/app/constants"
"github.com/BrosSquad/GoFiber-Boilerplate/app/handlers"
"github.com/BrosSquad/GoFiber-Boilerplate/core/configfx"
"github.com/BrosSquad/GoFiber-Boilerplate/core/fiber/fiberfx"
"github.com/BrosSquad/GoFiber-Boilerplate/core/loggerfx"
)

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()
app := container.New(
httpfx.Module("0.0.0.0:8000", "Test", true, http.Handlers()),
)
app.Run()

// 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()
app.Run()
return nil
// return app.ShutdownWithTimeout(10 * time.Second)
},
}
}
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.

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

import (
"github.com/BrosSquad/GoFiber-Boilerplate/app/handlers/helloworld"
"github.com/BrosSquad/GoFiber-Boilerplate/core/fiber/fiberfx"
)

func Handlers() fiberfx.RoutesFx {
return fiberfx.Routes(
"/",
nil,
fiberfx.Get("/", helloworld.HelloWorld),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package helloworld

import (
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog"
)

func HelloWorld() fiber.Handler {
func HelloWorld(logger zerolog.Logger) fiber.Handler {
return func(c *fiber.Ctx) error {
// logger.Info().Msg("Hello World!")
logger.Info().Msg("Hello World!")
return c.SendString("Hello, World!")
}
}
15 changes: 0 additions & 15 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.

71 changes: 0 additions & 71 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

0 comments on commit 13c9ab9

Please sign in to comment.