wip
This commit is contained in:
parent
14d16b73f3
commit
86283b0a91
40
cmd/injector.go
Normal file
40
cmd/injector.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||||||
|
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// injectorCmd represents the injector command
|
||||||
|
var injectorCmd = &cobra.Command{
|
||||||
|
Use: "injector",
|
||||||
|
Short: "A brief description of your command",
|
||||||
|
Long: `A longer description that spans multiple lines and likely contains examples
|
||||||
|
and usage of using your command. For example:
|
||||||
|
|
||||||
|
Cobra is a CLI library for Go that empowers applications.
|
||||||
|
This application is a tool to generate the needed files
|
||||||
|
to quickly create a Cobra application.`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("injector called")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(injectorCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// injectorCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// injectorCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
}
|
88
injector/app/app.go
Normal file
88
injector/app/app.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.espin.casa/albert/cml04-falcon-system/injector/routes"
|
||||||
|
"git.espin.casa/albert/cml04-falcon-system/internal/publisher"
|
||||||
|
"git.espin.casa/albert/logger"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"github.com/nats-io/nats.go"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Run(cmd *cobra.Command, args []string) {
|
||||||
|
// read flags
|
||||||
|
logLevel, _ := cmd.Flags().GetString("log-level")
|
||||||
|
httpAddr, _ := cmd.Flags().GetString("http-addr")
|
||||||
|
natsHost, _ := cmd.Flags().GetString("nats-host")
|
||||||
|
natsPort, _ := cmd.Flags().GetInt("nats-port")
|
||||||
|
// setup logger
|
||||||
|
log := logger.New(os.Stdout, logLevel)
|
||||||
|
// log fields
|
||||||
|
logFields := logger.LogFields{
|
||||||
|
"http-bind": httpAddr,
|
||||||
|
"nats_host": natsHost,
|
||||||
|
"nats_port": natsPort,
|
||||||
|
"log_level": logLevel,
|
||||||
|
}
|
||||||
|
// NATS connect
|
||||||
|
nc, err := nats.Connect(fmt.Sprintf("nats://%s:%d", natsHost, natsPort))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("create nats connection failed", err, logFields)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// on close disconnects
|
||||||
|
defer nc.Close()
|
||||||
|
pub := publisher.New(nc)
|
||||||
|
// create router
|
||||||
|
router := httprouter.New()
|
||||||
|
// create routes
|
||||||
|
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
|
||||||
|
log.Info("started cml04-falcon-handset service", logFields)
|
||||||
|
// wait signal to finish
|
||||||
|
signal := WaitSignal()
|
||||||
|
log.Info("signal received", logFields.Add(logger.LogFields{
|
||||||
|
"signal": signal,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
injector/handlers/bundle.go
Normal file
26
injector/handlers/bundle.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.espin.casa/albert/cml04-falcon-system/internal/publisher"
|
||||||
|
"git.espin.casa/albert/cml04-falcon-system/internal/types"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BundleHandler(publisher publisher.Publisher) httprouter.Handle {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
// bundle holder
|
||||||
|
bundle := &types.BundleData{}
|
||||||
|
// decode bundle data
|
||||||
|
err := json.NewDecoder(r.Body).Decode(bundle)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
// publish event
|
||||||
|
if err := publisher.NewBundle(r.Context(), bundle); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
injector/routes/routes.go
Normal file
11
injector/routes/routes.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.espin.casa/albert/cml04-falcon-system/injector/handlers"
|
||||||
|
"git.espin.casa/albert/cml04-falcon-system/internal/publisher"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateRoutes(r *httprouter.Router, publisher publisher.Publisher) {
|
||||||
|
r.POST("/bundle/new", handlers.BundleHandler(publisher))
|
||||||
|
}
|
@ -6,11 +6,13 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
cml04eventer "git.espin.casa/albert/cml04-eventer"
|
cml04eventer "git.espin.casa/albert/cml04-eventer"
|
||||||
|
"git.espin.casa/albert/cml04-falcon-system/internal/types"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Publisher interface {
|
type Publisher interface {
|
||||||
NewBarcode(ctx context.Context, barcode string) error
|
NewBarcode(ctx context.Context, barcode string) error
|
||||||
|
NewBundle(ctx context.Context, bundle *types.BundleData) error
|
||||||
ComfirmedBundle(ctx context.Context, barcode string) error
|
ComfirmedBundle(ctx context.Context, barcode string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +46,24 @@ func (e *publisher) NewBarcode(ctx context.Context, barcode string) error {
|
|||||||
return e.nc.Publish("barcode.new", buf.Bytes())
|
return e.nc.Publish("barcode.new", buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *publisher) NewBundle(ctx context.Context, bundle *types.BundleData) error {
|
||||||
|
// marshal bundle data
|
||||||
|
data, err := json.Marshal(bundle)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// create event message
|
||||||
|
evt := cml04eventer.NewEvent("falcon-handset", "bundle.new", nil, data)
|
||||||
|
// create buffer message data
|
||||||
|
buf := bytes.Buffer{}
|
||||||
|
// encode message
|
||||||
|
if err := json.NewEncoder(&buf).Encode(evt); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// publish event message
|
||||||
|
return e.nc.Publish("bundle.new", buf.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
func New(nc *nats.Conn) Publisher {
|
func New(nc *nats.Conn) Publisher {
|
||||||
return &publisher{
|
return &publisher{
|
||||||
nc: nc,
|
nc: nc,
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type BundleData struct {
|
type BundleData struct {
|
||||||
Grupo6 string `json:"grupo6"`
|
Grupo6 string `json:"grupo6"`
|
||||||
Po int `gorm:"index" json:"po"`
|
Po int `json:"po" gorm:"index"`
|
||||||
Co int `json:"co"`
|
Co int `json:"co"`
|
||||||
Colada string `json:"colada"`
|
Colada string `json:"colada"`
|
||||||
Calidad string `json:"calidad"`
|
Calidad string `json:"calidad"`
|
||||||
|
Loading…
Reference in New Issue
Block a user