with pull into storage

This commit is contained in:
2022-09-11 22:56:54 +02:00
parent 3643d4a467
commit 226e47e5f4
15 changed files with 485 additions and 53 deletions

View File

@@ -4,32 +4,18 @@ import (
"context"
"errors"
"net/http"
"time"
"git.front.kjuulh.io/kjuulh/curre"
"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 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
@@ -45,6 +31,30 @@ func NewGinHttpServer(_ *serverdeps.ServerDeps) curre.Component {
})
})
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)
})
server = &http.Server{
Addr: "127.0.0.1:3000",
Handler: app,
@@ -62,6 +72,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

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

View File

@@ -2,6 +2,7 @@ package server
import (
"context"
"time"
"git.front.kjuulh.io/kjuulh/curre"
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
@@ -11,15 +12,16 @@ import (
func NewStorageServer(logger *zap.Logger, deps *serverdeps.ServerDeps) curre.Component {
storage := deps.GetStorageService()
return curre.NewFunctionalComponent(&curre.FunctionalComponent{
InitFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
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(fc *curre.FunctionalComponent, ctx context.Context) error {
StopFunc: func(_ *curre.FunctionalComponent, ctx context.Context) error {
logger.Debug("Cleaning up storage")
ctx, _ = context.WithTimeout(ctx, time.Second*10)
return storage.CleanupStorage(ctx)
},
})