From 5bb93812ab2d6762dfa37390a154020b68f00053 Mon Sep 17 00:00:00 2001 From: aespin Date: Mon, 7 Oct 2024 10:25:25 +0200 Subject: [PATCH] wip --- Makefile | 34 +++++++++++++++++++ assets/js/barcode.js | 11 ++++--- celsa2048.crt | 28 ++++++++++++++++ docker/Dockerfile | 38 ++++++++++++++++++++++ handlers/barcodes.go | 72 ++++++++++++++++++++++++++++++++++++++--- routes/routes.go | 1 + storage/storage.go | 7 ++-- templates/barcodes.html | 17 ++++++++-- templates/index.html | 16 ++++----- types/barcode.go | 6 ++++ 10 files changed, 208 insertions(+), 22 deletions(-) create mode 100644 Makefile create mode 100644 celsa2048.crt create mode 100644 docker/Dockerfile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3fe47e2 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +# Variables +IMAGE_NAME = registry.espin.casa/cml04-falcon-ui +CONTAINER_NAME = cml04-falcon-ui + +# Check if Docker or Podman is available +DOCKER := $(shell command -v docker 2> /dev/null) +PODMAN := $(shell command -v podman 2> /dev/null) + +# Determine which command to use based on availability +ifdef DOCKER + DOCKER_CMD := docker +else ifdef PODMAN + DOCKER_CMD := podman +else +$(error "Neither Docker nor Podman is installed on this system.") +endif + +# Build the Docker image +build: + $(DOCKER_CMD) build -t $(IMAGE_NAME) -f docker/Dockerfile . + +# Run the container +run: + @templ generate + @$(DOCKER_CMD) run $(IMAGE_NAME) + +# Stop and remove the container +stop: + $(DOCKER_CMD) stop $(CONTAINER_NAME) + $(DOCKER_CMD) rm $(CONTAINER_NAME) + +# Remove the Docker image +clean: + $(DOCKER_CMD) rmi $(IMAGE_NAME) \ No newline at end of file diff --git a/assets/js/barcode.js b/assets/js/barcode.js index 940b18d..19148f8 100644 --- a/assets/js/barcode.js +++ b/assets/js/barcode.js @@ -1,9 +1,12 @@ $(document).ready(function () { // Initialize all input of date type. - const calendar = bulmaCalendar.attach('[type="date"]',{type:"datetime",isRange:true,color:"link",minuteSteps:1}); - - - + const calendar = bulmaCalendar.attach('[type="date"]',{ + weekStart: 1, + dateFormat: 'dd/MM/yyyy', + type:"datetime", + isRange:true, + color:"link", + minuteSteps:1}); // To access to bulmaCalendar instance of an element $('#submit').click(function() { const dateInput = $('#inicio').val(); diff --git a/celsa2048.crt b/celsa2048.crt new file mode 100644 index 0000000..523346d --- /dev/null +++ b/celsa2048.crt @@ -0,0 +1,28 @@ +-----BEGIN CERTIFICATE----- +MIIE0zCCA7ugAwIBAgIJANu+mC2Jt3uTMA0GCSqGSIb3DQEBCwUAMIGhMQswCQYD +VQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTERMA8GA1UEBxMIU2FuIEpvc2Ux +FTATBgNVBAoTDFpzY2FsZXIgSW5jLjEVMBMGA1UECxMMWnNjYWxlciBJbmMuMRgw +FgYDVQQDEw9ac2NhbGVyIFJvb3QgQ0ExIjAgBgkqhkiG9w0BCQEWE3N1cHBvcnRA +enNjYWxlci5jb20wHhcNMTQxMjE5MDAyNzU1WhcNNDIwNTA2MDAyNzU1WjCBoTEL +MAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExETAPBgNVBAcTCFNhbiBK +b3NlMRUwEwYDVQQKEwxac2NhbGVyIEluYy4xFTATBgNVBAsTDFpzY2FsZXIgSW5j +LjEYMBYGA1UEAxMPWnNjYWxlciBSb290IENBMSIwIAYJKoZIhvcNAQkBFhNzdXBw +b3J0QHpzY2FsZXIuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA +qT7STSxZRTgEFFf6doHajSc1vk5jmzmM6BWuOo044EsaTc9eVEV/HjH/1DWzZtcr +fTj+ni205apMTlKBW3UYR+lyLHQ9FoZiDXYXK8poKSV5+Tm0Vls/5Kb8mkhVVqv7 +LgYEmvEY7HPY+i1nEGZCa46ZXCOohJ0mBEtB9JVlpDIO+nN0hUMAYYdZ1KZWCMNf +5J/aTZiShsorN2A38iSOhdd+mcRM4iNL3gsLu99XhKnRqKoHeH83lVdfu1XBeoQz +z5V6gA3kbRvhDwoIlTBeMa5l4yRdJAfdpkbFzqiwSgNdhbxTHnYYorDzKfr2rEFM +dsMU0DHdeAZf711+1CunuQIDAQABo4IBCjCCAQYwHQYDVR0OBBYEFLm33UrNww4M +hp1d3+wcBGnFTpjfMIHWBgNVHSMEgc4wgcuAFLm33UrNww4Mhp1d3+wcBGnFTpjf +oYGnpIGkMIGhMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTERMA8G +A1UEBxMIU2FuIEpvc2UxFTATBgNVBAoTDFpzY2FsZXIgSW5jLjEVMBMGA1UECxMM +WnNjYWxlciBJbmMuMRgwFgYDVQQDEw9ac2NhbGVyIFJvb3QgQ0ExIjAgBgkqhkiG +9w0BCQEWE3N1cHBvcnRAenNjYWxlci5jb22CCQDbvpgtibd7kzAMBgNVHRMEBTAD +AQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAw0NdJh8w3NsJu4KHuVZUrmZgIohnTm0j+ +RTmYQ9IKA/pvxAcA6K1i/LO+Bt+tCX+C0yxqB8qzuo+4vAzoY5JEBhyhBhf1uK+P +/WVWFZN/+hTgpSbZgzUEnWQG2gOVd24msex+0Sr7hyr9vn6OueH+jj+vCMiAm5+u +kd7lLvJsBu3AO3jGWVLyPkS3i6Gf+rwAp1OsRrv3WnbkYcFf9xjuaf4z0hRCrLN2 +xFNjavxrHmsH8jPHVvgc1VD0Opja0l/BRVauTrUaoW6tE+wFG5rEcPGS80jjHK4S +pB5iDj2mUZH1T8lzYtuZy0ZPirxmtsk3135+CKNa2OCAhhFjE0xd +-----END CERTIFICATE----- \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..3192255 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,38 @@ +# Etapa de compilación +FROM golang:1.23.1-bookworm AS builder + +ENV TZ=Europe/Madrid + +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +ADD celsa2048.crt /usr/local/share/ca-certificates/celsa2048.crt +RUN chmod 644 /usr/local/share/ca-certificates/celsa2048.crt +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates + +RUN update-ca-certificates +WORKDIR /app + +COPY . . + +RUN go get -d -v ./... + +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -mod=mod -ldflags "-s -w" -o cml04-falcon-system main.go + +# Etapa de producción + +FROM debian:stable +ENV TZ=Europe/Madrid + +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +ADD celsa2048.crt /usr/local/share/ca-certificates/celsa2048.crt +RUN chmod 644 /usr/local/share/ca-certificates/celsa2048.crt +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates +ADD assets /root/assets +ADD templates /root/templates +RUN update-ca-certificates +WORKDIR /root/ + +COPY --from=builder /app/cml04-falcon-system . + +CMD ["./cml04-falcon-ui"] \ No newline at end of file diff --git a/handlers/barcodes.go b/handlers/barcodes.go index bfe636a..e0cd085 100644 --- a/handlers/barcodes.go +++ b/handlers/barcodes.go @@ -1,20 +1,82 @@ package handlers import ( + "fmt" "net/http" + "strings" "text/template" "git.espin.casa/albert/cml04-falcon-ui/storage" + "git.espin.casa/albert/cml04-falcon-ui/types" "github.com/julienschmidt/httprouter" ) func BarcodesHandler(storage storage.Storager) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - t, _ := template.ParseFiles("templates/base.html", "templates/barcodes.html") - err := t.Execute(w, nil) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + // get method + if r.Method == http.MethodGet { + // parse template files + t, _ := template.ParseFiles("templates/base.html", "templates/barcodes.html") + // execute templates + err := t.Execute(w, nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + // post method + if r.Method == http.MethodPost { + // barcodes holder + barcodes := []types.Barcode{} + // parse form data from formulary + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + // barcode + codigo := r.FormValue("codigo") + // date range + fechas := r.FormValue("fechas") + // loading bed + evacuadores := r.FormValue("evacuadores") + // check form values + if codigo != "" { + // call storager + data, err := storage.Barcode(r.Context(), codigo) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // append barcode to holder + barcodes = append(barcodes, *data) + } else { + inicio := strings.Split(fechas, "-")[0] + inicio = strings.TrimSpace(inicio) + final := strings.Split(fechas, "-")[1] + final = strings.TrimSpace(final) + // get loading bed from string + lb, ok := types.MapLoadingBed[evacuadores] + // if loading bed not found + if !ok { + http.Error(w, fmt.Errorf("loading bed value not found").Error(), http.StatusInternalServerError) + return + } + // call storager + data, err := storage.ListBarcode(r.Context(), lb, inicio, final) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + //set barcodes + barcodes = data + } + // parse template files + t, _ := template.ParseFiles("templates/base.html", "templates/barcodes.html") + // execute templates + err := t.Execute(w, barcodes) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } } } diff --git a/routes/routes.go b/routes/routes.go index 58aec29..56e4c24 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -14,6 +14,7 @@ func CreateRoutes(r *httprouter.Router, storage storage.Storager) { r.GET("/bcp", handlers.BCPHandler(storage)) r.GET("/lcp", handlers.LCPHandler(storage)) r.GET("/barcodes", handlers.BarcodesHandler(storage)) + r.POST("/barcodes", handlers.BarcodesHandler(storage)) r.GET("/labels", handlers.LabelsHandler()) r.GET("/standards", handlers.StandardsHandler(storage)) r.GET("/bundle", handlers.BundleHandler(storage)) diff --git a/storage/storage.go b/storage/storage.go index cbebb50..bc2bc90 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -2,6 +2,7 @@ package storage import ( "context" + "fmt" "git.espin.casa/albert/cml04-falcon-ui/types" ) @@ -27,12 +28,14 @@ func (s *storage) Bundle(ctx context.Context, ua string) (bundle *types.BundleDa // ListBarcode implements Storager. func (s *storage) ListBarcode(ctx context.Context, lb types.LoadingBed, inicio string, final string) (barcodes []types.Barcode, err error) { - panic("unimplemented") + fmt.Println(lb, inicio, final) + return []types.Barcode{}, nil } // Barcode implements Storager. func (s *storage) Barcode(ctx context.Context, reading string) (barcode *types.Barcode, err error) { - panic("unimplemented") + fmt.Println(reading) + return &types.Barcode{}, nil } func New() Storager { diff --git a/templates/barcodes.html b/templates/barcodes.html index 931d094..e056d8d 100644 --- a/templates/barcodes.html +++ b/templates/barcodes.html @@ -39,14 +39,25 @@