From 86283b0a91980865efc66fec230d2dd1701e62ea Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 3 Oct 2024 16:14:53 +0200 Subject: [PATCH] wip --- cmd/injector.go | 40 +++++++++++++++ injector/app/app.go | 88 +++++++++++++++++++++++++++++++++ injector/handlers/bundle.go | 26 ++++++++++ injector/routes/routes.go | 11 +++++ internal/publisher/publisher.go | 20 ++++++++ internal/types/bundle.go | 2 +- 6 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 cmd/injector.go create mode 100644 injector/app/app.go create mode 100644 injector/handlers/bundle.go create mode 100644 injector/routes/routes.go diff --git a/cmd/injector.go b/cmd/injector.go new file mode 100644 index 0000000..9edf77a --- /dev/null +++ b/cmd/injector.go @@ -0,0 +1,40 @@ +/* +Copyright © 2024 NAME HERE + +*/ +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") +} diff --git a/injector/app/app.go b/injector/app/app.go new file mode 100644 index 0000000..9ce264c --- /dev/null +++ b/injector/app/app.go @@ -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 + } + } +} diff --git a/injector/handlers/bundle.go b/injector/handlers/bundle.go new file mode 100644 index 0000000..4c650d8 --- /dev/null +++ b/injector/handlers/bundle.go @@ -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) + } + } +} diff --git a/injector/routes/routes.go b/injector/routes/routes.go new file mode 100644 index 0000000..4a8dc16 --- /dev/null +++ b/injector/routes/routes.go @@ -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)) +} diff --git a/internal/publisher/publisher.go b/internal/publisher/publisher.go index 40185a5..6b28351 100644 --- a/internal/publisher/publisher.go +++ b/internal/publisher/publisher.go @@ -6,11 +6,13 @@ import ( "encoding/json" cml04eventer "git.espin.casa/albert/cml04-eventer" + "git.espin.casa/albert/cml04-falcon-system/internal/types" "github.com/nats-io/nats.go" ) type Publisher interface { NewBarcode(ctx context.Context, barcode string) error + NewBundle(ctx context.Context, bundle *types.BundleData) 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()) } +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 { return &publisher{ nc: nc, diff --git a/internal/types/bundle.go b/internal/types/bundle.go index 7353c48..dce5ef8 100644 --- a/internal/types/bundle.go +++ b/internal/types/bundle.go @@ -9,7 +9,7 @@ import ( type BundleData struct { Grupo6 string `json:"grupo6"` - Po int `gorm:"index" json:"po"` + Po int `json:"po" gorm:"index"` Co int `json:"co"` Colada string `json:"colada"` Calidad string `json:"calidad"`