From 9ddafa9df835bbe3818990aa00e8117fc723c22e Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Fri, 22 Mar 2024 13:55:24 +0000 Subject: [PATCH] Fixed recipe posting --- .gitignore | 1 + Makefile | 13 +++++++--- db/menu.db | Bin 425984 -> 425984 bytes dbconnection/menu.go | 2 +- dbconnection/sqlite_repository.go | 10 +++----- docker-compose.yml | 18 ++++++++++++++ docker/Dockerfile | 2 ++ jobhandler/jobhandler.go | 16 ++++++------ notes.txt | 8 ++++++ server.go | 39 ++++++++++++++++++++++++------ 10 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 docker-compose.yml create mode 100644 notes.txt diff --git a/.gitignore b/.gitignore index 7ca63e1..4fc30a3 100644 --- a/.gitignore +++ b/.gitignore @@ -198,3 +198,4 @@ go.work # Android studio 3.1+ serialized cache file +/menuserver diff --git a/Makefile b/Makefile index 0d1b509..8e52672 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,23 @@ PROJECT = menuserver -VERSION = 3.0 +VERSION=`git describe --tags` +BUILD=`date +%FT%T%z` ECR_REPO = git.caliban.io/martin -# APP_IMAGE = 482681734622.dkr.ecr.eu-west-1.amazonaws.com/$(PROJECT):$(VERSION) + APP_IMAGE = $(ECR_REPO)/$(PROJECT):$(VERSION) -# APP_IMAGE = $(PROJECT):$(VERSION) + NO_CACHE = true +LDFLAGS=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD}" + + .PHONY: build build: #CC=/usr/local/musl/bin/musl-gcc go build --ldflags '-linkmode external -extldflags "-static"' server.go - GCO_ENABLED=0 GOOS=linux go build -o ${PROJECT} server.go + # GCO_ENABLED=0 GOOS=linux go build ${LDFLAGS} -o ${PROJECT} server.go + go build ${LDFLAGS} -o ${PROJECT} server.go # docker build ./docker/. -t $(APP_IMAGE) --build-arg VERSION=$(VERSION) --no-cache=$(NO_CACHE) --compress=true docker build --platform linux/amd64 --no-cache -force-rm --tag ${APP_IMAGE} --file ./docker/Dockerfile . di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6!daA?dp8vgI5VKKScPVQu+z19uOtrv{Q2Kc7E7N?97eTIyAnM30X~ zW?ENnhdJD>Y;GN#n9U;LjRLc?$YFz?X~!!MlEoZ$E)}sUlSvaZkKYt{buuX=5bo7t zlX6I%8wgM4kjP4wfI~M=`&#CErHW5a!xgGpCVTc<#xS`oJ~GBc#$j~L#rpC zbAt@~A-bdjonOg_->oc8hy2VePZf56N}E*44pE^h#H|n0h>{rlvuSS%t*^(=X3$w) zcS81D8c9)k%zU~x4Hk_EyOa(E!Q9$`&L%3OOI7U}TAi$6+b-I#!%Kl_SoJP#3n(q8 zd_=#vm&O;M(@Ed)GIQpDhP%(va*vCG-A#+VN}rNz^!=o)3F_fsJ}?t~KhkgR0kioq z4&0$>8T7+cbo_8pN;WVTyVKz`KzxC~SQeD2^mYiQCOet_5S;VTsw3z;3c=+3#!m$= zgS)FknDZEnfZJxyENDn}cVRx{DZK0=7!J~=9&}d0_DnUK9or#yw9=rx9u6e^mmLC{ z%oJ^befLTHWOq>ElQ84T^YAu@=*sWWxdiW}xSN9wU&|6nNsK3;F19ZLAv}=)>LWkI z+HS+iy7ezC$HY(Y?ENZDuEDx>ciwh@{h}nn!F1LL%2=1hRxoCsUMTDk>m94^zMalO znKaysP9Yl!Dg;(BIU1Ky!Q72X=;-YH@2Zjgu2{o6&5V0kH0!dkkFjD3W}1T~t!$Iw z0)oA5tTjanR*yT+vNay4I-sS$&4>WSQ3>`Z4ZM}L`N>gQ#pe82 zYDKPL7_pJ-ED!fzXF=Vy&G}#3i@Kd)5=a@_9&`_~xF4)0$(lXugkvn>%y+&!$EkWP_t|JVlRXa2wYmv z8`G6E$9SnkPl6mh!S^Qj(COss40rEe>EhKA986 + - EMAIL_LOGIN=aida@caliban.io + - EMAIL_RECIPIENTS=Martin ,Jessica + - EMAIL_HOSTNAME=mail.caliban.io + - EMAIL_PASSWORD=Ultra+Topaz+6XQ + - SITE_URL=http://menu.lan \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index 54a5100..ce8fbb7 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -10,6 +10,8 @@ COPY ./menuserver /app/ COPY ./dist /app/dist +RUN apt-get update && apt-get install -y ca-certificates + # Need the following to get a go app to run inside a docker container # as per: https://www.fairlyusefulcode.co.uk/post/go-alpine-linux/ -- DEAD!!! # RUN apk upgrade musl diff --git a/jobhandler/jobhandler.go b/jobhandler/jobhandler.go index fe77272..65b3da0 100644 --- a/jobhandler/jobhandler.go +++ b/jobhandler/jobhandler.go @@ -7,15 +7,17 @@ import ( "log" "menuserver/dbconnection" "net/smtp" + "os" + "strings" "time" ) var ( - from = "Aida " - login = "aida@caliban.io" - // recipients = []string{"Martin ", "Jessica "} - recipients = []string{"Martin "} - hostname = "mail.caliban.io" + from = os.Getenv("EMAIL_FROM") + login = os.Getenv("EMAIL_LOGIN") + recipients = strings.Split(os.Getenv("EMAIL_RECIPIENTS"), ",") + // recipients = []string{"Martin "} + hostname = os.Getenv("EMAIL_HOSTNAME") ) func DoJob(r *dbconnection.SQLiteRepository) { @@ -26,7 +28,7 @@ func DoJob(r *dbconnection.SQLiteRepository) { var menuItems []dbconnection.Menu - srcURL := "http://menu.lan" + srcURL := os.Getenv("SITE_URL") var types = [7]int{0, 0, 0, 0, 0, 0, 0} limit := 2 @@ -143,7 +145,7 @@ func sendSMTP(inmsg string, subject string) { e := email.NewEmail() - auth := smtp.PlainAuth("", login, "Ultra+Topaz+6XQ", hostname) + auth := smtp.PlainAuth("", login, os.Getenv("EMAIL_PASSWORD"), hostname) e.From = from e.To = []string{from} diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..7fa95c7 --- /dev/null +++ b/notes.txt @@ -0,0 +1,8 @@ +EMAIL_FROM -- from = "Aida " +EMAIL_LOGIN -- login = "aida@caliban.io" +EMAIL_RECIPIENTS -- recipients = []string{"Martin ", "Jessica "} +// recipients = []string{"Martin "} +EMAIL_HOSTNAME -- hostname = "mail.caliban.io" +EMAIL_PASSWORD -- "Ultra+Topaz+6XQ" + +SITE_URL -- srcURL := "http://menu.lan" diff --git a/server.go b/server.go index aa6c79b..b064dd0 100644 --- a/server.go +++ b/server.go @@ -24,13 +24,26 @@ const htmlFront = "\n \n \n const htmlEnd = "\n \n \n " var ( - version string - build string + Version string + Build string + startupErrors []string ) func main() { - fmt.Printf("Menu server v%+v build %+v\n\n", version, build) + wanted := []string{"EMAIL_FROM", "EMAIL_LOGIN", "EMAIL_RECIPIENTS", "EMAIL_HOSTNAME", "EMAIL_PASSWORD", "SITE_URL"} + + for _, item := range wanted { + log.Printf("%+v -- %+v", item, os.Getenv(item)) + if len(os.Getenv(item)) == 0 { + startupErrors = append(startupErrors, item) + } + } + if len(startupErrors) != 0 { + log.Fatal("Missing: ", strings.Join(startupErrors, ",")) + } + + fmt.Printf("Menu server v%+v build %+v\n\n", Version, Build) // setup connections n stuff @@ -89,6 +102,7 @@ func main() { }) c := cron.New() + // "0 8 * * 6" var _, cronerr = c.AddFunc("0 8 * * 6", func() { jobhandler.DoJob(recipes) }) if cronerr != nil { log.Fatal(cronerr) @@ -224,17 +238,28 @@ func PostRecipeHandler(c *fiber.Ctx, r *dbconnection.SQLiteRepository) error { payload := dbconnection.MenuStrings{} if err := c.BodyParser(&payload); err != nil { + log.Println("PostRecipeHandler Bodyparser") log.Fatal(err) return err } - payload.Hash = ShortHash(payload.Name) - payload.Short = strcase.KebabCase(payload.Name) - payload.Lastused = 0 + meat, _ := strconv.ParseInt(payload.Meat, 10, 64) + mealtype, _ := strconv.ParseInt(payload.Mealtype, 10, 64) + + usePayload := dbconnection.Menu{ + Name: payload.Name, + Url: payload.Url, + Md: payload.Md, + Short: strcase.KebabCase(payload.Name), + Hash: ShortHash(payload.Name), + Meat: meat, + Mealtype: mealtype, + Lastused: 0, + } var returnVal dbconnection.InsertedType - returnVal, _ = r.InsertOne(payload) + returnVal, _ = r.InsertOne(usePayload) jsonStr, err := json.Marshal(returnVal)