Skip to content

Commit

Permalink
refactor(server): decouple service initializations
Browse files Browse the repository at this point in the history
BREAKING CHANGE: decouple service initializations
  • Loading branch information
bastean committed May 27, 2024
1 parent db66e1d commit 61961d2
Show file tree
Hide file tree
Showing 44 changed files with 584 additions and 289 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22

require (
github.com/JGLTechnologies/gin-rate-limit v1.5.4
github.com/a-h/templ v0.2.697
github.com/a-h/templ v0.2.707
github.com/brianvoe/gofakeit/v7 v7.0.3
github.com/cucumber/godog v0.14.1
github.com/gin-contrib/secure v1.1.0
Expand Down Expand Up @@ -69,7 +69,7 @@ require (
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/JGLTechnologies/gin-rate-limit v1.5.4 h1:1hIaXIdGM9MZFZlXgjWJLpxaK0WH
github.com/JGLTechnologies/gin-rate-limit v1.5.4/go.mod h1:mGEhNzlHEg/Tk+KH/mKylZLTfDjACnx7MVYaAlj07eU=
github.com/a-h/templ v0.2.697 h1:OILxtWvD0NRJaoCOiZCopRDPW8paroKlGsrAiHLykNE=
github.com/a-h/templ v0.2.697/go.mod h1:5cqsugkq9IerRNucNsI4DEamdHPsoGMQy99DzydLhM8=
github.com/a-h/templ v0.2.707 h1:T1Gkd2ugbRglZ9rYw/VBchWOSZVKmetDbBkm4YubM7U=
github.com/a-h/templ v0.2.707/go.mod h1:5cqsugkq9IerRNucNsI4DEamdHPsoGMQy99DzydLhM8=
github.com/brianvoe/gofakeit/v7 v7.0.3 h1:tGCt+eYfhTMWE1ko5G2EO1f/yE44yNpIwUb4h32O0wo=
github.com/brianvoe/gofakeit/v7 v7.0.3/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
Expand Down Expand Up @@ -182,6 +184,8 @@ golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/server/component/page/dashboard/page.dashboard.templ
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dashboard
import (
"github.com/bastean/codexgo/pkg/cmd/server/component/layout"
"github.com/bastean/codexgo/pkg/cmd/server/component/script"
"github.com/bastean/codexgo/pkg/context/user/application/read"
"github.com/bastean/codexgo/pkg/cmd/server/service/user"
)

script PageInit() {
Expand Down Expand Up @@ -39,7 +39,7 @@ script Logout() {
window.location.replace("/");
}

templ Page(user *read.Response) {
templ Page(user *user.ReadResponse) {
@layout.Index(script.Head{}, script.Body{}) {
<div class="ui centered grid container" style="height: 100%;">
<div class="sixteen wide column">
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/server/handler/form/user.create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (

"github.com/bastean/codexgo/pkg/cmd/server/service/user"
"github.com/bastean/codexgo/pkg/cmd/server/util/reply"
"github.com/bastean/codexgo/pkg/context/user/application/create"
"github.com/gin-gonic/gin"
)

func UserCreate() gin.HandlerFunc {
return func(c *gin.Context) {
command := new(create.Command)
command := new(user.CreateCommand)

c.BindJSON(command)

err := user.CreateHandler.Handle(command)
err := user.Create.Handle(command)

if err != nil {
c.Error(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/server/handler/form/user.delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/bastean/codexgo/pkg/cmd/server/util/errs"
"github.com/bastean/codexgo/pkg/cmd/server/util/key"
"github.com/bastean/codexgo/pkg/cmd/server/util/reply"
"github.com/bastean/codexgo/pkg/context/user/application/delete"
"github.com/gin-gonic/gin"
)

Expand All @@ -21,13 +20,13 @@ func UserDelete() gin.HandlerFunc {
return
}

command := new(delete.Command)
command := new(user.DeleteCommand)

c.BindJSON(command)

command.Id = id.(string)

err := user.DeleteHandler.Handle(command)
err := user.Delete.Handle(command)

if err != nil {
c.Error(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/server/handler/form/user.login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import (
"github.com/bastean/codexgo/pkg/cmd/server/service/user"
"github.com/bastean/codexgo/pkg/cmd/server/util/key"
"github.com/bastean/codexgo/pkg/cmd/server/util/reply"
"github.com/bastean/codexgo/pkg/context/user/application/login"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)

func UserLogin() gin.HandlerFunc {
return func(c *gin.Context) {
query := new(login.Query)
query := new(user.LoginQuery)

c.BindJSON(query)

user, err := user.LoginHandler.Handle(query)
user, err := user.Login.Handle(query)

if err != nil {
c.Error(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/server/handler/form/user.update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/bastean/codexgo/pkg/cmd/server/util/errs"
"github.com/bastean/codexgo/pkg/cmd/server/util/key"
"github.com/bastean/codexgo/pkg/cmd/server/util/reply"
"github.com/bastean/codexgo/pkg/context/user/application/update"
"github.com/gin-gonic/gin"
)

Expand All @@ -21,13 +20,13 @@ func UserUpdate() gin.HandlerFunc {
return
}

command := new(update.Command)
command := new(user.UpdateCommand)

c.BindJSON(command)

command.Id = id.(string)

err := user.UpdateHandler.Handle(command)
err := user.Update.Handle(command)

if err != nil {
c.Error(err)
Expand Down
9 changes: 4 additions & 5 deletions pkg/cmd/server/handler/page/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/bastean/codexgo/pkg/cmd/server/service/user"
"github.com/bastean/codexgo/pkg/cmd/server/util/errs"
"github.com/bastean/codexgo/pkg/cmd/server/util/key"
"github.com/bastean/codexgo/pkg/context/user/application/read"
"github.com/gin-gonic/gin"
)

Expand All @@ -22,11 +21,11 @@ func Dashboard() gin.HandlerFunc {
return
}

query := &read.Query{
Id: id.(string),
}
query := new(user.ReadQuery)

query.Id = id.(string)

user, err := user.ReadHandler.Handle(query)
user, err := user.Read.Handle(query)

if err != nil {
c.Error(err)
Expand Down
15 changes: 10 additions & 5 deletions pkg/cmd/server/handler/param/user.verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ import (
"net/http"

"github.com/bastean/codexgo/pkg/cmd/server/service/user"
"github.com/bastean/codexgo/pkg/context/user/application/verify"
"github.com/bastean/codexgo/pkg/cmd/server/util/errs"
"github.com/bastean/codexgo/pkg/cmd/server/util/key"
"github.com/gin-gonic/gin"
)

func UserVerify() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Param("id") == "" {
id := c.Param(key.Id)

if id == "" {
c.Error(errs.MissingKey(key.Id, "UserVerify"))
c.Redirect(http.StatusFound, "/")
c.Abort()
return
}

command := new(verify.Command)
command := new(user.VerifyCommand)

command.Id = c.Param("id")
command.Id = id

err := user.VerifyHandler.Handle(command)
err := user.Verify.Handle(command)

if err != nil {
c.Error(err)
Expand Down
7 changes: 3 additions & 4 deletions pkg/cmd/server/middleware/cookieSession.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package middleware

import (
"os"

"github.com/bastean/codexgo/pkg/cmd/server/service/env"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)

var secretKey = os.Getenv("COOKIE_SECRET_KEY")
var secretKey = env.Cookie.SecretKey

var sessionName = os.Getenv("COOKIE_SESSION_NAME")
var sessionName = env.Cookie.SessionName

func CookieSession() gin.HandlerFunc {
store := cookie.NewStore([]byte(secretKey))
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/server/middleware/errorHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package middleware
import (
"net/http"

"github.com/bastean/codexgo/pkg/cmd/server/service/errors"
"github.com/bastean/codexgo/pkg/cmd/server/service/logger"
"github.com/bastean/codexgo/pkg/cmd/server/util/reply"
"github.com/bastean/codexgo/pkg/context/shared/domain/errors"
"github.com/gin-gonic/gin"
)

Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/server/middleware/securityConfig.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package middleware

import (
"os"
"strings"

"github.com/bastean/codexgo/pkg/cmd/server/service/env"
"github.com/gin-contrib/secure"
"github.com/gin-gonic/gin"
)

var allowedHosts = env.Security.AllowedHosts

func getAllowedHosts() []string {
return strings.Split(os.Getenv("ALLOWED_HOSTS"), ", ")
return strings.Split(allowedHosts, ", ")
}

func SecurityConfig() gin.HandlerFunc {
Expand Down
22 changes: 8 additions & 14 deletions pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@ import (
"time"

"github.com/bastean/codexgo/pkg/cmd/server/router"
"github.com/bastean/codexgo/pkg/cmd/server/service/broker"
"github.com/bastean/codexgo/pkg/cmd/server/service/database"
"github.com/bastean/codexgo/pkg/cmd/server/service"
"github.com/bastean/codexgo/pkg/cmd/server/service/logger"
"github.com/bastean/codexgo/pkg/cmd/server/service/notify"
"github.com/bastean/codexgo/pkg/cmd/server/service/user"
)

//go:embed static
var Files embed.FS

func Run(port string) {
errNotify := notify.Init()
errBroker := broker.Init()
errDatabase := database.Init()
errUser := user.Init()
logger.Info("starting services")

err := errors.Join(errNotify, errBroker, errDatabase, errUser)
err := service.Start()

if err != nil {
logger.Fatal(err.Error())
Expand All @@ -54,19 +48,19 @@ func Run(port string) {

<-shutdown

logger.Info("closing server")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

defer cancel()

errBroker = broker.Close()
logger.Info("stopping services")

errService := service.Stop(ctx)

errDatabase = database.Close(ctx)
logger.Info("stopping server")

errServer := server.Shutdown(ctx)

err = errors.Join(errBroker, errDatabase, errServer)
err = errors.Join(errService, errServer)

if err != nil {
logger.Error(err.Error())
Expand Down
7 changes: 2 additions & 5 deletions pkg/cmd/server/service/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package auth

import (
"os"

"github.com/bastean/codexgo/pkg/cmd/server/service/env"
"github.com/bastean/codexgo/pkg/context/shared/infrastructure/authentications"
)

type Payload authentications.Payload

var secretKey = os.Getenv("JWT_SECRET_KEY")

var auth = authentications.NewAuthentication(secretKey)
var auth = authentications.NewAuthentication(env.JWT.SecretKey)

var GenerateJWT = auth.GenerateJWT

Expand Down
69 changes: 0 additions & 69 deletions pkg/cmd/server/service/broker/broker.go

This file was deleted.

Loading

0 comments on commit 61961d2

Please sign in to comment.