This commit is contained in:
aespin 2024-10-04 09:38:25 +02:00
parent 224d943988
commit 534a80f0d6
4 changed files with 75 additions and 30 deletions

View File

@ -2,16 +2,14 @@ package app
import ( import (
"fmt" "fmt"
"net/http"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
"git.espin.casa/albert/cml04-falcon-system/injector/routes" "git.espin.casa/albert/cml04-falcon-system/injector/logging"
"git.espin.casa/albert/cml04-falcon-system/injector/service"
"git.espin.casa/albert/cml04-falcon-system/internal/publisher" "git.espin.casa/albert/cml04-falcon-system/internal/publisher"
"git.espin.casa/albert/logger" "git.espin.casa/albert/logger"
"github.com/julienschmidt/httprouter"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -40,26 +38,9 @@ func Run(cmd *cobra.Command, args []string) {
// on close disconnects // on close disconnects
defer nc.Close() defer nc.Close()
pub := publisher.New(nc) pub := publisher.New(nc)
// create router // create service
router := httprouter.New() svc := service.New(pub)
// create routes svc = logging.New(log, svc)
routes.CreateRoutes(router, pub)
// serve static files
router.ServeFiles("/assets/*filepath", http.Dir("assets"))
// create http server
server := http.Server{
Addr: httpAddr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 5 * time.Second,
Handler: router,
}
// start the http server
go func() {
if err := server.ListenAndServeTLS("server.crt", "server.key"); err != nil {
panic(err)
}
}()
// info banner // info banner
log.Info("started cml04-falcon-handset service", logFields) log.Info("started cml04-falcon-handset service", logFields)
// wait signal to finish // wait signal to finish

View File

@ -0,0 +1,38 @@
package logging
import (
"context"
"time"
"git.espin.casa/albert/cml04-falcon-system/injector/service"
"git.espin.casa/albert/cml04-falcon-system/injector/types"
"git.espin.casa/albert/logger"
)
type LoggingService struct {
log logger.LoggerAdapter
next service.IService
}
// Bundle implements service.IService.
func (l *LoggingService) Bundle(ctx context.Context, req *types.PostBundleReq) (res *types.PostBundleRes, err error) {
defer func(start time.Time) {
logFields := logger.LogFields{
"took": time.Since(start),
"bundle_id": res.BundleData.Nromatricula,
}
if err != nil {
l.log.Error("get product tolerance data failed", err, logFields)
} else {
l.log.Info("get product tolerance data success", logFields)
}
}(time.Now())
return l.next.Bundle(ctx, req)
}
func New(log logger.LoggerAdapter, svc service.IService) service.IService {
return &LoggingService{
log: log,
next: svc,
}
}

View File

@ -10,6 +10,7 @@ import (
"git.espin.casa/albert/cml04-falcon-system/injector/types" "git.espin.casa/albert/cml04-falcon-system/injector/types"
"github.com/zc2638/swag" "github.com/zc2638/swag"
"github.com/zc2638/swag/endpoint" "github.com/zc2638/swag/endpoint"
"github.com/zc2638/swag/option"
) )
type Server struct { type Server struct {
@ -80,3 +81,23 @@ func (s *Server) Start() error {
}() }()
return nil return nil
} }
func NewServer(url string, svc service.IService) *Server {
// create new swag
api := swag.New(
option.Title("CELSA 4 Falcon Bundle Injector"),
option.Description("This is an implementation of a REST API server for mediciones"),
option.ContactEmail("aespin@gcelsa.com"),
option.Version("1.0"),
option.BasePath("/api/v1"),
option.License("MIT", "https://opensource.org/license/mit"),
)
// add end points
api.AddEndpoint(
PostBundleEndPoint(svc),
)
return &Server{
api: api,
Url: url,
}
}

View File

@ -4,19 +4,24 @@ import (
"context" "context"
"git.espin.casa/albert/cml04-falcon-system/injector/types" "git.espin.casa/albert/cml04-falcon-system/injector/types"
"git.espin.casa/albert/cml04-falcon-system/internal/publisher"
) )
type IService interface { type IService interface {
Bundle(ctx context.Context, req *types.PostBundleReq) (res *types.PostBundleRes, err *types.PostBundleRes) Bundle(ctx context.Context, req *types.PostBundleReq) (res *types.PostBundleRes, err error)
} }
type service struct{} type service struct {
pub publisher.Publisher
}
// Bundle implements IService. // Bundle implements IService.
func (s *service) Bundle(ctx context.Context, req *types.PostBundleReq) (res *types.PostBundleRes, err *types.PostBundleRes) { func (s *service) Bundle(ctx context.Context, req *types.PostBundleReq) (res *types.PostBundleRes, err error) {
panic("unimplemented") return &types.PostBundleRes{}, nil
} }
func New() IService { func New(pub publisher.Publisher) IService {
return &service{} return &service{
pub: pub,
}
} }