From f00aad15c50d55feec8da03f937144d0875e81e1 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 28 Sep 2019 15:09:47 +0200 Subject: [PATCH] Version * log on startup * display on about page * use compile flag * set with git tag value --- .ansible/deploy-draft.yml | 6 ++++++ .drone.yml | 8 +++++++- Dockerfile | 3 ++- README.md | 6 ++++++ bouquins/bouquins.go | 18 ++++++++---------- main.go | 16 +++++++++++----- templates/about.html | 1 + 7 files changed, 41 insertions(+), 17 deletions(-) diff --git a/.ansible/deploy-draft.yml b/.ansible/deploy-draft.yml index 442b00a..4c7da34 100644 --- a/.ansible/deploy-draft.yml +++ b/.ansible/deploy-draft.yml @@ -1,10 +1,16 @@ --- - hosts: docker tasks: + - name: registry login + docker_login: + username: "{{ registry_login }}" + password: "{{ registry_password }}" + - name: deploy draft docker_container: name: "bouquins_draft_{{ draft_name }}" image: "reg.meutel.net/go-bouquins:{{ draft_name }}" + pull: yes state: started recreate: yes labels: diff --git a/.drone.yml b/.drone.yml index 7f11ca7..95b9be5 100644 --- a/.drone.yml +++ b/.drone.yml @@ -30,6 +30,7 @@ steps: settings: registry: reg.meutel.net repo: reg.meutel.net/go-bouquins + build_args: "version=$DRONE_TAG" username: from_secret: registry_login password: @@ -55,6 +56,11 @@ steps: - name: deploy-branch image: plugins/ansible:1 + environment: + registry_login: + from_secret: registry_login + registry_password: + from_secret: registry_password settings: playbook: .ansible/deploy-draft.yml inventory: .ansible/inventory @@ -73,6 +79,6 @@ trigger: --- kind: signature -hmac: 402ef53d245969c2df4d6ec14d51d3d9665856c8fa0ec8248755a209fb66ff36 +hmac: 954a9dbf92147a4d7dce6805ef54ce3dd381450d97b8676de39bb5be51c963cf ... diff --git a/Dockerfile b/Dockerfile index e4d8960..0d547ca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,12 @@ FROM golang as builder +ARG version WORKDIR /go/src/meutel.net/meutel/go-bouquins COPY . . RUN GO111MODULE=on go get . -RUN GO111MODULE=on CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o app . +RUN GO111MODULE=on CGO_ENABLED=1 GOOS=linux go build -ldflags "-X main.version=$version" -a -installsuffix cgo -o app . RUN curl -X POST -s --data-urlencode 'input@assets/css/bouquins.css' https://cssminifier.com/raw > assets/css/bouquins.min.css # deployment image diff --git a/README.md b/README.md index 018f0b6..8c9ecac 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,12 @@ Bouquins in Go * JS: https://www.danstools.com/javascript-minify/ * CSS: curl -X POST -s --data-urlencode 'input@assets/css/bouquins.css' https://cssminifier.com/raw > assets/css/bouquins.min.css +## Versionning + +Build with flag to define `main.version` + + go build -ldflags "-X main.version=myVersion" + ## Deployment archive tar czf ~/tmp/go-bouquins.tar.gz go-bouquins assets/ templates/ diff --git a/bouquins/bouquins.go b/bouquins/bouquins.go index dec076f..f302857 100644 --- a/bouquins/bouquins.go +++ b/bouquins/bouquins.go @@ -19,9 +19,6 @@ import ( ) const ( - // Version defines application version - Version = "master" - tplBooks = "book.html" tplAuthors = "author.html" tplSeries = "series.html" @@ -56,11 +53,11 @@ const ( // URLAbout url of about page URLAbout = "/about/" // URLJs url of js assets - URLJs = "/" + Version + "/js/" + URLJs = "/js/" // URLCss url of css assets - URLCss = "/" + Version + "/css/" + URLCss = "/css/" // URLFonts url of fonts assets - URLFonts = "/" + Version + "/webfonts/" + URLFonts = "/webfonts/" // URLCalibre url of calibre resources (covers, ebooks files) URLCalibre = "/calibre/" ) @@ -186,7 +183,6 @@ type SeriesFull struct { type Model struct { Title string Page string - Version string Username string } @@ -195,7 +191,6 @@ func (app *Bouquins) NewModel(title, page string, req *http.Request) *Model { return &Model{ Title: title, Page: page, - Version: Version, Username: app.Username(req), } } @@ -285,18 +280,21 @@ type ReqParams struct { } // TemplatesFunc adds functions to templates -func TemplatesFunc(prod bool) *template.Template { +func TemplatesFunc(prod bool, version string) *template.Template { return template.New("").Funcs(template.FuncMap{ "assetUrl": func(name string, ext string) string { sep := "." if prod { sep = ".min." } - return "/" + Version + "/" + ext + "/" + name + sep + ext + return "/" + ext + "/" + name + sep + ext }, "humanSize": func(sz int64) string { return datasize.ByteSize(sz).HumanReadable() }, + "version": func() string { + return version + }, "bookCover": func(book *BookFull) string { fmt.Println(book.Path) return "/calibre/" + url.PathEscape(book.Path) + "/cover.jpg" diff --git a/main.go b/main.go index 65ed0b2..cdc0862 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,12 @@ package main import ( "database/sql" - "gopkg.in/yaml.v2" "log" "net/http" "os" + "gopkg.in/yaml.v2" + "golang.org/x/oauth2" "github.com/gorilla/sessions" @@ -15,6 +16,10 @@ import ( "meutel.net/meutel/go-bouquins/bouquins" ) +var ( + version string +) + // ReadConfig loads configuration file and initialize default value func ReadConfig() (*bouquins.Conf, error) { conf := new(bouquins.Conf) @@ -53,7 +58,7 @@ func initApp() *bouquins.Bouquins { log.Fatalln(err) } - tpl, err := bouquins.TemplatesFunc(conf.Prod).ParseGlob("templates/*.html") + tpl, err := bouquins.TemplatesFunc(conf.Prod, version).ParseGlob("templates/*.html") if err != nil { log.Fatalln(err) } @@ -86,9 +91,9 @@ func initApp() *bouquins.Bouquins { } func assets(calibre string) { - http.Handle(bouquins.URLJs, http.StripPrefix("/"+bouquins.Version, http.FileServer(http.Dir("assets")))) - http.Handle(bouquins.URLCss, http.StripPrefix("/"+bouquins.Version, http.FileServer(http.Dir("assets")))) - http.Handle(bouquins.URLFonts, http.StripPrefix("/"+bouquins.Version, http.FileServer(http.Dir("assets")))) + http.Handle(bouquins.URLJs, http.FileServer(http.Dir("assets"))) + http.Handle(bouquins.URLCss, http.FileServer(http.Dir("assets"))) + http.Handle(bouquins.URLFonts, http.FileServer(http.Dir("assets"))) } func handle(f func(res http.ResponseWriter, req *http.Request) error) func(res http.ResponseWriter, req *http.Request) { @@ -120,6 +125,7 @@ func router(app *bouquins.Bouquins) { } func main() { + log.Println("go-bouquins", version) app := initApp() defer app.DB.Close() defer app.UserDB.Close() diff --git a/templates/about.html b/templates/about.html index cbb76eb..520c965 100644 --- a/templates/about.html +++ b/templates/about.html @@ -1,5 +1,6 @@ {{ template "header.html" . }}
+

go-bouquins {{ version }}

bouquins est une interface web pour le gestionnaire de livres numériques calibre. Cette version utilise le langage Go, le framework bootstap et le framework Vue.js.

Ce logiciel est open-source et distribué sous license BSD. Le code source est disponible ici: https://git.meutel.net/meutel/go-bouquins.