This commit is contained in:
aespin 2024-10-02 10:36:08 +02:00
parent 2dfbaef6d6
commit b652c521bd
10 changed files with 116 additions and 62 deletions

1
assets/css/bulma.css.map Normal file

File diff suppressed because one or more lines are too long

3
assets/css/bulma.min.css vendored Normal file

File diff suppressed because one or more lines are too long

1
assets/js/html5-qrcode.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -11,26 +11,16 @@ import (
// handsetCmd represents the handset command // handsetCmd represents the handset command
var handsetCmd = &cobra.Command{ var handsetCmd = &cobra.Command{
Use: "handset", Use: "handset",
Short: "A brief description of your command", Short: "Handset service for reading/process barcodes.",
Long: `A longer description that spans multiple lines and likely contains examples Long: "Handset service for reading and processing read barcodes from label on bundles.",
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: app.Run, Run: app.Run,
} }
func init() { func init() {
rootCmd.AddCommand(handsetCmd) rootCmd.AddCommand(handsetCmd)
// Here you will define your flags and configuration settings. handsetCmd.Flags().String("http-addr", ":9443", "nats.io broker host address")
handsetCmd.Flags().String("nats-host", "10.136.49.95", "nats.io broker host address")
// Cobra supports Persistent Flags which will work for this command handsetCmd.Flags().Int("nats-port", 4222, "nats.io broker tcp port")
// and all subcommands, e.g.: handsetCmd.Flags().String("log-level", "debug", "log level trace")
// handsetCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// handsetCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }

View File

@ -1,14 +1,73 @@
package app package app
import ( import (
"fmt"
"net/http"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
"git.espin.casa/albert/cml04-falcon-system/handset/publisher"
"git.espin.casa/albert/cml04-falcon-system/handset/routes"
"git.espin.casa/albert/logger"
"github.com/julienschmidt/httprouter"
"github.com/nats-io/nats.go"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func Run(cmd *cobra.Command, args []string) {} 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 // WaitSignal catching exit signal
func WaitSignal() os.Signal { func WaitSignal() os.Signal {

View File

@ -1,38 +0,0 @@
package eventer
import (
"bytes"
"context"
"encoding/json"
cml04eventer "git.espin.casa/albert/cml04-eventer"
"github.com/nats-io/nats.go"
)
type Eventer interface {
BarCode(ctx context.Context, barcode string) error
}
type eventer struct {
nc *nats.Conn
}
func (e *eventer) BarCode(ctx context.Context, barcode string) error {
// create event message
evt := cml04eventer.NewEvent("handset", "barcode.new", nil, []byte(barcode))
buf := bytes.Buffer{}
if err := json.NewEncoder(&buf).Encode(evt); err != nil {
return err
}
return e.nc.Publish("barcode.new", buf.Bytes())
}
func New() (Eventer, error) {
return &eventer{
nc: &nats.Conn{},
}
}

View File

@ -3,14 +3,14 @@ package handlers
import ( import (
"net/http" "net/http"
"git.espin.casa/albert/cml04-falcon-system/handset/eventer" "git.espin.casa/albert/cml04-falcon-system/handset/publisher"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
func BarCodeHandler(eventer eventer.Eventer) httprouter.Handle { func BarCodeHandler(publisher publisher.Publisher) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
barcode := ps.ByName("barcode") barcode := ps.ByName("barcode")
if err := eventer.BarCode(r.Context(), barcode); err != nil { if err := publisher.Barcode(r.Context(), barcode); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }

View File

@ -0,0 +1,38 @@
package publisher
import (
"bytes"
"context"
"encoding/json"
cml04eventer "git.espin.casa/albert/cml04-eventer"
"github.com/nats-io/nats.go"
)
type Publisher interface {
Barcode(ctx context.Context, barcode string) error
}
type publisher struct {
nc *nats.Conn
}
func (e *publisher) Barcode(ctx context.Context, barcode string) error {
// create event message
evt := cml04eventer.NewEvent("falcon-handset", "barcode.new", nil, []byte(barcode))
buf := bytes.Buffer{}
if err := json.NewEncoder(&buf).Encode(evt); err != nil {
return err
}
return e.nc.Publish("barcode.new", buf.Bytes())
}
func New(nc *nats.Conn) Publisher {
return &publisher{
nc: nc,
}
}

View File

@ -1,12 +1,12 @@
package routes package routes
import ( import (
"git.espin.casa/albert/cml04-falcon-system/handset/eventer"
"git.espin.casa/albert/cml04-falcon-system/handset/handlers" "git.espin.casa/albert/cml04-falcon-system/handset/handlers"
"git.espin.casa/albert/cml04-falcon-system/handset/publisher"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
func CreateRoutes(r *httprouter.Router, eventer eventer.Eventer) { func CreateRoutes(r *httprouter.Router, publisher publisher.Publisher) {
r.GET("/", handlers.IndexHandler()) r.GET("/", handlers.IndexHandler())
r.GET("/barcode/:barcode", handlers.BarCodeHandler(eventer)) r.GET("/barcode/:barcode", handlers.BarCodeHandler(publisher))
} }