Adding Initial action (#1)

Co-authored-by: kjuulh <contact@kjuulh.io>
Reviewed-on: kjuulh/kraken#1
This commit is contained in:
2022-09-12 22:12:02 +02:00
parent b3302bb3c6
commit 50228f0aff
23 changed files with 1155 additions and 59 deletions

View File

@@ -4,46 +4,28 @@ import (
"context"
"errors"
"net/http"
"time"
"git.front.kjuulh.io/kjuulh/curre"
"git.front.kjuulh.io/kjuulh/kraken/internal/api"
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func NewHttpServer(deps *serverdeps.ServerDeps) curre.Component {
return curre.NewFunctionalComponent(&curre.FunctionalComponent{
StartFunc: func(_ *curre.FunctionalComponent, _ context.Context) error {
handler := http.NewServeMux()
handler.HandleFunc(
"/health/ready",
func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("ready"))
w.WriteHeader(http.StatusOK)
})
http.ListenAndServe("127.0.0.1:3000", handler)
return nil
},
},
)
}
func NewGinHttpServer(_ *serverdeps.ServerDeps) curre.Component {
func NewGinHttpServer(logger *zap.Logger, deps *serverdeps.ServerDeps) curre.Component {
var app *gin.Engine
var server *http.Server
return curre.NewFunctionalComponent(&curre.FunctionalComponent{
InitFunc: func(_ *curre.FunctionalComponent, _ context.Context) error {
app = gin.Default()
app = gin.New()
app.UseH2C = true
app.Use(ginzap.Ginzap(logger, time.RFC3339, true))
app.Use(ginzap.RecoveryWithZap(logger, true))
healthRoute := app.Group("/health")
healthRoute.GET("/ready", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "healthy",
})
})
api.BuildApi(logger, app, deps)
server = &http.Server{
Addr: "127.0.0.1:3000",
@@ -62,6 +44,7 @@ func NewGinHttpServer(_ *serverdeps.ServerDeps) curre.Component {
return nil
},
StopFunc: func(_ *curre.FunctionalComponent, ctx context.Context) error {
ctx, _ = context.WithTimeout(ctx, time.Second*10)
if server != nil {
server.Shutdown(ctx)
}

View File

@@ -5,6 +5,7 @@ import (
"git.front.kjuulh.io/kjuulh/curre"
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/signer"
"go.uber.org/zap"
)
@@ -14,6 +15,8 @@ func Start(logger *zap.Logger) error {
deps := serverdeps.NewServerDeps(logger)
return curre.NewManager().
Register(NewGinHttpServer(deps)).
Register(NewGinHttpServer(logger.With(zap.Namespace("ginHttpServer")), deps)).
Register(NewStorageServer(logger.With(zap.Namespace("storageServer")), deps)).
Register(signer.NewOpenPGPApp(deps.GetOpenPGP())).
Run(ctx)
}

View File

@@ -0,0 +1,28 @@
package server
import (
"context"
"time"
"git.front.kjuulh.io/kjuulh/curre"
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
"go.uber.org/zap"
)
func NewStorageServer(logger *zap.Logger, deps *serverdeps.ServerDeps) curre.Component {
storage := deps.GetStorageService()
return curre.NewFunctionalComponent(&curre.FunctionalComponent{
InitFunc: func(_ *curre.FunctionalComponent, ctx context.Context) error {
logger.Debug("Initializing storage")
return storage.InitializeStorage(ctx)
},
StartFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
return nil
},
StopFunc: func(_ *curre.FunctionalComponent, ctx context.Context) error {
logger.Debug("Cleaning up storage")
ctx, _ = context.WithTimeout(ctx, time.Second*10)
return storage.CleanupStorage(ctx)
},
})
}