Skip to content

Commit

Permalink
refactor: Adding a service layer
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Jun 21, 2024
1 parent e5d698a commit 8cf6f96
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
9 changes: 5 additions & 4 deletions cmd/goweb/api.go → cmd/goweb/goweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ func main() {
defer clients.Close(ctx)
go clients.Start()

appServer := server.New(config.appServer)
services := newServices(config)
port := newPort(config, clients)

go appServer.Start(clients.health.EndCtx(), httputils.Handler(newPort(config, clients), clients.health, clients.telemetry.Middleware("http"), owasp.New(config.owasp).Middleware, cors.New(config.cors).Middleware))
go services.server.Start(clients.health.EndCtx(), httputils.Handler(port, clients.health, clients.telemetry.Middleware("http"), owasp.New(config.owasp).Middleware, cors.New(config.cors).Middleware))

clients.health.WaitForTermination(appServer.Done())
clients.health.WaitForTermination(services.server.Done())

server.GracefulWait(appServer.Done())
server.GracefulWait(services.server.Done())
}
12 changes: 3 additions & 9 deletions cmd/goweb/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@ import (
"github.com/ViBiOh/goweb/pkg/hello"
)

const (
helloPath = "/hello/{name...}"
dumpPath = "/dump/"
delayPath = "/delay/"
)

func newPort(config configuration, client client) http.Handler {
mux := http.NewServeMux()

mux.Handle(helloPath, hello.Handler(config.hello))
mux.Handle(dumpPath, dump.Handler(client.telemetry.MeterProvider()))
mux.Handle(delayPath, delay.Handler())
mux.Handle("GET /hello/{name...}", hello.Handler(config.hello))
mux.Handle("/dump/", dump.Handler(client.telemetry.MeterProvider()))
mux.Handle("/delay/", delay.Handler())

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTeapot)
Expand Down
13 changes: 13 additions & 0 deletions cmd/goweb/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "github.com/ViBiOh/httputils/v4/pkg/server"

type services struct {
server server.Server
}

func newServices(config configuration) services {
return services{
server: server.New(config.appServer),
}
}
6 changes: 0 additions & 6 deletions pkg/hello/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ func Handler(config *Config) http.Handler {

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

telemetry.SetRouteTag(ctx, "/hello")

if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

name := r.PathValue("name")
if len(name) == 0 {
name = "World"
Expand Down

0 comments on commit 8cf6f96

Please sign in to comment.