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 . diff --git a/db/menu.db b/db/menu.db index 7342ac2..b093ed5 100644 Binary files a/db/menu.db and b/db/menu.db differ diff --git a/dbconnection/menu.go b/dbconnection/menu.go index bced109..dc857c0 100644 --- a/dbconnection/menu.go +++ b/dbconnection/menu.go @@ -21,7 +21,7 @@ type MenuStrings struct { Hash string `json:"hash"` Meat string `json:"meat"` Mealtype string `json:"mealtype"` - Lastused int64 `json:"lastused"` + Lastused string `json:"lastused"` } type InsertedType struct { diff --git a/dbconnection/sqlite_repository.go b/dbconnection/sqlite_repository.go index b3b6756..3188032 100644 --- a/dbconnection/sqlite_repository.go +++ b/dbconnection/sqlite_repository.go @@ -94,7 +94,7 @@ func (r *SQLiteRepository) GetOneHash(Hash string) (Menu, error) { return item, nil } -func (r *SQLiteRepository) InsertOne(newitem MenuStrings) (InsertedType, error) { +func (r *SQLiteRepository) InsertOne(newitem Menu) (InsertedType, error) { log.Printf("Insert One: %s\n", newitem) stmt, err := r.db.Prepare("INSERT INTO menu(name, url, md, short, hash, meat, mealtype, lastused) VALUES (?,?,?,?,?,?,?,?)") @@ -104,12 +104,8 @@ func (r *SQLiteRepository) InsertOne(newitem MenuStrings) (InsertedType, error) } defer stmt.Close() - - meat, _ := strconv.ParseInt(newitem.Meat, 10, 64) - mealtype, _ := strconv.ParseInt(newitem.Meat, 10, 64) - lastused, _ := strconv.ParseInt(newitem.Meat, 10, 64) - - b, err := stmt.Exec(newitem.Name, newitem.Url, newitem.Md, newitem.Short, newitem.Hash, meat, mealtype, lastused) + + b, err := stmt.Exec(newitem.Name, newitem.Url, newitem.Md, newitem.Short, newitem.Hash, newitem.Meat, newitem.Mealtype, newitem.Lastused) if err != nil { log.Println(err) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..53af49a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.5' + +services: + menuserver: + container_name: menuserver + image: git.caliban.io/martin/menuserver:3.0.0 + restart: always + ports: + - "3000:3000" + volumes: + - ./db:/app/db + environment: + - EMAIL_FROM=Aida + - 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)