wip
This commit is contained in:
parent
2dfbaef6d6
commit
b652c521bd
1
assets/css/bulma.css.map
Normal file
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
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
1
assets/js/html5-qrcode.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -11,26 +11,16 @@ import (
|
||||
// handsetCmd represents the handset command
|
||||
var handsetCmd = &cobra.Command{
|
||||
Use: "handset",
|
||||
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.`,
|
||||
Short: "Handset service for reading/process barcodes.",
|
||||
Long: "Handset service for reading and processing read barcodes from label on bundles.",
|
||||
Run: app.Run,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(handsetCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// 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")
|
||||
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")
|
||||
handsetCmd.Flags().Int("nats-port", 4222, "nats.io broker tcp port")
|
||||
handsetCmd.Flags().String("log-level", "debug", "log level trace")
|
||||
}
|
||||
|
@ -1,14 +1,73 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"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"
|
||||
)
|
||||
|
||||
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
|
||||
func WaitSignal() os.Signal {
|
||||
|
@ -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{},
|
||||
}
|
||||
}
|
@ -3,14 +3,14 @@ package handlers
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.espin.casa/albert/cml04-falcon-system/handset/eventer"
|
||||
"git.espin.casa/albert/cml04-falcon-system/handset/publisher"
|
||||
"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) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
38
handset/publisher/publisher.go
Normal file
38
handset/publisher/publisher.go
Normal 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,
|
||||
}
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
package routes
|
||||
|
||||
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/publisher"
|
||||
"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("/barcode/:barcode", handlers.BarCodeHandler(eventer))
|
||||
r.GET("/barcode/:barcode", handlers.BarCodeHandler(publisher))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user