This commit is contained in:
albert 2024-04-19 17:03:32 +02:00
commit ddb4b3290d
12 changed files with 328 additions and 0 deletions

0
Makefile Normal file
View File

9
cmd/main.go Normal file
View File

@ -0,0 +1,9 @@
package main
import "git.espin.casa/albert/cml04-mediciones-service/internal/app"
func main() {
if err := app.Run(); err != nil {
panic(err)
}
}

0
docker/Dockerfile Normal file
View File

17
go.mod Normal file
View File

@ -0,0 +1,17 @@
module git.espin.casa/albert/cml04-mediciones-service
go 1.22.2
require (
git.espin.casa/albert/logger v0.0.0-20240312060442-59b35e5c6996
github.com/zc2638/swag v1.5.1
gorm.io/gorm v1.25.9
)
require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)

39
go.sum Normal file
View File

@ -0,0 +1,39 @@
git.espin.casa/albert/logger v0.0.0-20240312060442-59b35e5c6996 h1:gOY7u2z9gMOdp0Q1Rod9XvoM0gL84SBXR7n/VxrgvtE=
git.espin.casa/albert/logger v0.0.0-20240312060442-59b35e5c6996/go.mod h1:dacFMi82f5ysDOaJQm0QmQaJZ7mwGFiayJ2iS+JKAfQ=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/zc2638/swag v1.5.1 h1:T/PyvMTXOxCRIakSpD7Ed4uICW4PM16JdPsS+3dJbKU=
github.com/zc2638/swag v1.5.1/go.mod h1:AjyTDUHzZZ4mctSNLEZVD5jWQHFGDJlaCR5XjDm46aE=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8=
gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

64
internal/app/app.go Normal file
View File

@ -0,0 +1,64 @@
package app
import (
"flag"
"os"
"os/signal"
"syscall"
"git.espin.casa/albert/cml04-mediciones-service/internal/logging"
"git.espin.casa/albert/cml04-mediciones-service/internal/server"
"git.espin.casa/albert/cml04-mediciones-service/internal/service"
"git.espin.casa/albert/cml04-mediciones-service/internal/storage"
"git.espin.casa/albert/logger"
)
func Run() error {
// flags
bindAddr := flag.String("bind-addr", ":3000", "http rest api bind address")
logLevel := flag.String("level", "debug", "Log level")
flag.Parse()
// log fields
logFields := logger.LogFields{
"bind_address": *bindAddr,
"log_level": *logLevel,
}
// setup logger
log := logger.New(os.Stdout, *logLevel)
// create storage
storage := storage.NewStorage()
// create service
svc := service.NewService(storage)
// create logging service
svc = logging.NewLoggingService(log, svc)
// create server
server := server.NewServer(*bindAddr, svc)
// start server
server.Start()
// info banner
log.Info("started cml04-mediciones-service", logFields)
// wait signal to finish
signal := WaitSignal()
log.Info("signal received", logFields.Add(logger.LogFields.Add(logFields, logger.LogFields{
"signal": signal,
})))
return nil
}
// WaitSignal catching exit signal
func WaitSignal() os.Signal {
ch := make(chan os.Signal, 2)
signal.Notify(
ch,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM,
)
for {
sig := <-ch
switch sig {
case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM:
return sig
}
}
}

View File

@ -0,0 +1,26 @@
package logging
import (
"context"
"git.espin.casa/albert/cml04-mediciones-service/internal/service"
"git.espin.casa/albert/cml04-mediciones-service/internal/types"
"git.espin.casa/albert/logger"
)
type LoggingService struct {
log logger.LoggerAdapter
next service.IService
}
// GetTolerancia implements service.IService.
func (l *LoggingService) GetTolerancia(ctx context.Context, req *types.ToleranciaReq) (res *types.ToleranciaRes, err error) {
return l.next.GetTolerancia(ctx, req)
}
func NewLoggingService(log logger.LoggerAdapter, svc service.IService) service.IService {
return &LoggingService{
log: log,
next: svc,
}
}

96
internal/server/server.go Normal file
View File

@ -0,0 +1,96 @@
package server
import (
"encoding/json"
"fmt"
"net/http"
"path"
"time"
"git.espin.casa/albert/cml04-mediciones-service/internal/service"
"git.espin.casa/albert/cml04-mediciones-service/internal/types"
"github.com/zc2638/swag"
"github.com/zc2638/swag/endpoint"
"github.com/zc2638/swag/option"
)
type Server struct {
Api *swag.API
Url string
}
func GetToleranciasEndPoint(svc service.IService) *swag.Endpoint {
return endpoint.New(http.MethodGet, "/tolerancia",
endpoint.BodyR(types.ToleranciaReq{}),
endpoint.Response(http.StatusOK, "ok", endpoint.SchemaResponseOption(types.ToleranciaRes{})),
endpoint.Summary("Gets info about product tolerance"),
endpoint.Description("Gets info about product tolerance"),
endpoint.Handler(GetToleranciasHandler(svc)),
)
}
func GetToleranciasHandler(svc service.IService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// request body holder
req := &types.ToleranciaReq{}
// decode body request
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
http.Error(w, "decode request body failed", http.StatusInternalServerError)
return
}
// get tolerancia from database
res, err := svc.GetTolerancia(r.Context(), req)
if err != nil {
http.Error(w, "get tolerancia failed", http.StatusInternalServerError)
return
}
fmt.Printf("res: %+v\n", res)
})
}
func (s *Server) Start() error {
// set api handlers
for p, endpoints := range s.Api.Paths {
http.DefaultServeMux.Handle(path.Join(s.Api.BasePath, p), endpoints)
}
// set swagger handlers
http.DefaultServeMux.Handle("/swagger/json", s.Api.Handler())
patterns := swag.UIPatterns("/swagger/ui")
for _, pattern := range patterns {
http.DefaultServeMux.Handle(pattern, swag.UIHandler("/swagger/ui", "/swagger/json", true))
}
// Crear un servidor HTTP
server := http.Server{
Addr: s.Url,
ReadTimeout: 5 * time.Second, // time limit for reading
WriteTimeout: 5 * time.Second, // time limit for writting
IdleTimeout: 5 * time.Second,
}
// start api
go func() {
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}()
return nil
}
func NewServer(url string, svc service.IService) *Server {
// create new swag
api := swag.New(
option.Title("CELSA 4 Mediciones REST API Server"),
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(
GetToleranciasEndPoint(svc),
)
return &Server{
Api: api,
Url: url,
}
}

View File

@ -0,0 +1,31 @@
package service
import (
"context"
"time"
"git.espin.casa/albert/cml04-mediciones-service/internal/storage"
"git.espin.casa/albert/cml04-mediciones-service/internal/types"
)
type IService interface {
GetTolerancia(ctx context.Context, req *types.ToleranciaReq) (res *types.ToleranciaRes, err error)
}
type service struct {
storage storage.IStorage
}
// GetTolerancia implements IService.
func (s *service) GetTolerancia(ctx context.Context, req *types.ToleranciaReq) (res *types.ToleranciaRes, err error) {
return &types.ToleranciaRes{
Tolerancia: &types.Tolerancia{},
TimeStamp: time.Now().UTC().Format(time.RFC3339),
}, nil
}
func NewService(storage storage.IStorage) IService {
return &service{
storage: storage,
}
}

View File

@ -0,0 +1,9 @@
package storage
type IStorage interface{}
type storage struct{}
func NewStorage() IStorage {
return &storage{}
}

View File

@ -0,0 +1,30 @@
package types
type Tolerancia struct {
Medida string `gorm:""`
MMax float64
MMin float64
HMax float64
HMin float64
SMax float64
SMin float64
TMax float64
TMin float64
BMax float64
BMin float64
ParalMax float64
AsimMax float64
Longitud float64
FlechaMax float64
}
type ToleranciaReq struct {
Sender string
Medidad string
TimeStamp string
}
type ToleranciaRes struct {
Tolerancia *Tolerancia
TimeStamp string
}

7
internal/types/vigas.go Normal file
View File

@ -0,0 +1,7 @@
package types
import "gorm.io/gorm"
type Vigas struct {
gorm.Model
}