gin logging

This commit is contained in:
2022-09-12 09:52:44 +02:00
parent 3f443f52fd
commit f78d84dc8f
10 changed files with 183 additions and 71 deletions

16
internal/api/health.go Normal file
View File

@@ -0,0 +1,16 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
)
func HealthRoute(app *gin.Engine) {
healthRoute := app.Group("/health")
healthRoute.GET("/ready", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "healthy",
})
})
}

View File

@@ -0,0 +1,39 @@
package api
import (
"context"
"net/http"
"git.front.kjuulh.io/kjuulh/kraken/internal/commands"
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/jobs"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"go.uber.org/zap"
)
func CommandRoute(logger *zap.Logger, app *gin.Engine, deps *serverdeps.ServerDeps) {
commandRoute := app.Group("commands")
commandRoute.POST("processRepos", func(c *gin.Context) {
type processReposRequest struct {
RepositoryUrls []string `json:"repositoryUrls"`
}
var request processReposRequest
err := c.BindJSON(&request)
if err != nil {
logger.Info("could not bind request", zap.String("request", "processRepo"), zap.Error(err))
c.AbortWithStatus(http.StatusBadRequest)
return
}
jobId := uuid.New().String()
go func(repositoryUrls []string, jobId string) {
ctx := context.WithValue(context.Background(), jobs.JobId{}, jobId)
processRepos := commands.NewProcessRepos(logger, deps)
err = processRepos.Process(ctx, repositoryUrls)
}(request.RepositoryUrls, jobId)
c.Status(http.StatusAccepted)
})
}

12
internal/api/root.go Normal file
View File

@@ -0,0 +1,12 @@
package api
import (
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func BuildApi(logger *zap.Logger, app *gin.Engine, deps *serverdeps.ServerDeps) {
HealthRoute(app)
CommandRoute(logger, app, deps)
}

View File

@@ -16,11 +16,13 @@ type (
logger *zap.Logger
storage *storage.Service
git *providers.Git
action *actions.Action
}
ProcessReposDeps interface {
GetStorageService() *storage.Service
GetGitProvider() *providers.Git
GetAction() *actions.Action
}
)
@@ -29,10 +31,11 @@ func NewProcessRepos(logger *zap.Logger, deps ProcessReposDeps) *ProcessRepos {
logger: logger,
storage: deps.GetStorageService(),
git: deps.GetGitProvider(),
action: deps.GetAction(),
}
}
func (pr *ProcessRepos) Process(ctx context.Context, repositoryUrls []string, action *actions.Action) error {
func (pr *ProcessRepos) Process(ctx context.Context, repositoryUrls []string) error {
// Clone repos
wg := sync.WaitGroup{}
wg.Add(len(repositoryUrls))
@@ -62,14 +65,32 @@ func (pr *ProcessRepos) Process(ctx context.Context, repositoryUrls []string, ac
}(ctx)
pr.logger.Debug("Cloning repo", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
ctx, _ = context.WithTimeout(ctx, time.Second*5)
_, err = pr.git.Clone(ctx, area, repoUrl)
cloneCtx, _ := context.WithTimeout(ctx, time.Second*5)
_, err = pr.git.Clone(cloneCtx, area, repoUrl)
if err != nil {
pr.logger.Error("could not clone repo", zap.Error(err))
errChan <- err
return
}
err = pr.action.Run(
ctx,
area,
func(ctx context.Context, area *storage.Area) (bool, error) {
pr.logger.Debug("checking predicate", zap.String("area", area.Path))
return true, nil
},
func(ctx context.Context, area *storage.Area) error {
pr.logger.Debug("running action", zap.String("area", area.Path))
return nil
}, false)
if err != nil {
pr.logger.Error("could not run action", zap.Error(err))
errChan <- err
return
}
pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
}(ctx, repoUrl)
}

View File

@@ -7,11 +7,10 @@ import (
"time"
"git.front.kjuulh.io/kjuulh/curre"
"git.front.kjuulh.io/kjuulh/kraken/internal/commands"
"git.front.kjuulh.io/kjuulh/kraken/internal/api"
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/jobs"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"go.uber.org/zap"
)
@@ -21,39 +20,12 @@ func NewGinHttpServer(logger *zap.Logger, deps *serverdeps.ServerDeps) curre.Com
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",
})
})
commandRoute := app.Group("commands")
commandRoute.POST("processRepos", func(c *gin.Context) {
type processReposRequest struct {
RepositoryUrls []string `json:"repositoryUrls"`
}
var request processReposRequest
err := c.BindJSON(&request)
if err != nil {
logger.Info("could not bind request", zap.String("request", "processRepo"), zap.Error(err))
c.AbortWithStatus(http.StatusBadRequest)
return
}
jobId := uuid.New().String()
go func(repositoryUrls []string, jobId string) {
ctx := context.WithValue(context.Background(), jobs.JobId{}, jobId)
processRepos := commands.NewProcessRepos(logger, deps)
err = processRepos.Process(ctx, repositoryUrls, nil)
}(request.RepositoryUrls, jobId)
c.Status(http.StatusAccepted)
})
api.BuildApi(logger, app, deps)
server = &http.Server{
Addr: "127.0.0.1:3000",

View File

@@ -14,7 +14,7 @@ func Start(logger *zap.Logger) error {
deps := serverdeps.NewServerDeps(logger)
return curre.NewManager().
Register(NewGinHttpServer(logger.With(zap.String("app", "ginHttpServer")), deps)).
Register(NewStorageServer(logger.With(zap.String("app", "storageServer")), deps)).
Register(NewGinHttpServer(logger.With(zap.Namespace("ginHttpServer")), deps)).
Register(NewStorageServer(logger.With(zap.Namespace("storageServer")), deps)).
Run(ctx)
}

View File

@@ -1,6 +1,7 @@
package serverdeps
import (
"git.front.kjuulh.io/kjuulh/kraken/internal/services/actions"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/providers"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
"go.uber.org/zap"
@@ -14,7 +15,7 @@ type ServerDeps struct {
func NewServerDeps(logger *zap.Logger) *ServerDeps {
deps := &ServerDeps{
logger: logger.With(zap.String("app", "serverdeps")),
logger: logger.With(zap.Namespace("serverdeps")),
}
if storageCfg, err := storage.NewDefaultStorageConfig(); err != nil {
@@ -36,9 +37,13 @@ func NewServerDeps(logger *zap.Logger) *ServerDeps {
}
func (deps *ServerDeps) GetStorageService() *storage.Service {
return storage.NewService(deps.logger.With(zap.String("app", "storage")), deps.storageConfig)
return storage.NewService(deps.logger.With(zap.Namespace("storage")), deps.storageConfig)
}
func (deps *ServerDeps) GetGitProvider() *providers.Git {
return providers.NewGit(deps.logger.With(zap.String("app", "gitProvider")), deps.gitCfg)
return providers.NewGit(deps.logger.With(zap.Namespace("gitProvider")), deps.gitCfg)
}
func (deps *ServerDeps) GetAction() *actions.Action {
return actions.NewAction(deps.logger.With(zap.Namespace("action")))
}

View File

@@ -1,4 +1,43 @@
package actions
import (
"context"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
"go.uber.org/zap"
)
type Predicate func(ctx context.Context, area *storage.Area) (bool, error)
type ActionFunc func(ctx context.Context, area *storage.Area) error
type Action struct {
logger *zap.Logger
}
func NewAction(logger *zap.Logger) *Action {
return &Action{logger: logger}
}
func (a *Action) Run(ctx context.Context, area *storage.Area, predicate Predicate, action ActionFunc, dryrun bool) error {
matches, err := predicate(ctx, area)
if err != nil {
return err
}
if !matches {
a.logger.Debug("repo doesn't match, skipping", zap.String("path", area.Path))
return nil
}
if dryrun {
a.logger.Panic("dryrun selected, but not implemented yet")
return nil
}
err = action(ctx, area)
if err != nil {
return err
}
return nil
}