From 4dd5e0982050ae92a9a37cb62e606b2807a85c27 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 29 Mar 2018 20:08:41 +0200 Subject: [PATCH] Embed database migrations - switch from goose to github.com/rubenv/sql-migrate - move assets (static, templates, migrations) to boardvoting package - add generated boardvoting/assets.go - remove unused static files from static directory - add package db with db migration configuration --- .gitignore | 1 + Gopkg.lock | 54 +- Makefile | 17 +- boardvoting.go | 39 +- boardvoting/main.go | 3 + .../20170421134856_0.1.0_original.sql | 4 +- .../20170421143052_fix_duplicates.sql | 4 +- .../20170421143114_add_constraints.sql | 2 +- .../static}/js/jquery.min.js | 0 .../static}/js/jquery.min.map | 0 .../static}/semantic.min.css | 0 .../static}/semantic.min.js | 0 {static => boardvoting/static}/styles.css | 2 +- .../themes/default/assets/fonts/icons.eot | Bin .../themes/default/assets/fonts/icons.svg | 0 .../themes/default/assets/fonts/icons.ttf | Bin .../themes/default/assets/fonts/icons.woff | Bin .../themes/default/assets/fonts/icons.woff2 | Bin .../themes/default/assets/images/flags.png | Bin .../templates}/closed_motion_mail.txt | 0 .../templates}/create_motion_form.html | 8 +- .../templates}/create_motion_mail.txt | 0 .../templates}/denied.html | 0 .../templates}/direct_vote_form.html | 0 .../templates}/direct_vote_mail.txt | 0 .../templates}/edit_motion_form.html | 0 .../templates}/footer.html | 0 .../templates}/header.html | 6 +- .../templates}/motion.html | 0 .../templates}/motion_fragments.html | 0 .../templates}/motions.html | 0 .../templates}/proxy_vote_form.html | 0 .../templates}/proxy_vote_mail.txt | 0 .../templates}/remind_voter_mail.txt | 0 .../templates}/update_motion_mail.txt | 0 .../templates}/withdraw_motion_form.html | 0 .../templates}/withdraw_motion_mail.txt | 0 db/dbconf.yml | 6 - db/migrations_embedded.go | 14 + models.go | 42 +- static/js/jquery.js | 10253 ------- static/semantic.css | 24708 ---------------- static/semantic.js | 12470 -------- static/themes/basic/assets/fonts/icons.eot | Bin 40166 -> 0 bytes static/themes/basic/assets/fonts/icons.svg | 450 - static/themes/basic/assets/fonts/icons.ttf | Bin 39924 -> 0 bytes static/themes/basic/assets/fonts/icons.woff | Bin 24676 -> 0 bytes .../github/assets/fonts/octicons-local.ttf | Bin 53604 -> 0 bytes .../themes/github/assets/fonts/octicons.svg | 200 - .../themes/github/assets/fonts/octicons.ttf | Bin 31740 -> 0 bytes .../themes/github/assets/fonts/octicons.woff | Bin 17772 -> 0 bytes static/themes/material/assets/fonts/icons.eot | Bin 143258 -> 0 bytes static/themes/material/assets/fonts/icons.svg | 2373 -- static/themes/material/assets/fonts/icons.ttf | Bin 128180 -> 0 bytes .../themes/material/assets/fonts/icons.woff | Bin 57620 -> 0 bytes 55 files changed, 102 insertions(+), 50554 deletions(-) create mode 100644 boardvoting/main.go rename {db => boardvoting}/migrations/20170421134856_0.1.0_original.sql (96%) rename {db => boardvoting}/migrations/20170421143052_fix_duplicates.sql (84%) rename {db => boardvoting}/migrations/20170421143114_add_constraints.sql (99%) rename {static => boardvoting/static}/js/jquery.min.js (100%) rename {static => boardvoting/static}/js/jquery.min.map (100%) rename {static => boardvoting/static}/semantic.min.css (100%) rename {static => boardvoting/static}/semantic.min.js (100%) rename {static => boardvoting/static}/styles.css (89%) rename {static => boardvoting/static}/themes/default/assets/fonts/icons.eot (100%) rename {static => boardvoting/static}/themes/default/assets/fonts/icons.svg (100%) rename {static => boardvoting/static}/themes/default/assets/fonts/icons.ttf (100%) rename {static => boardvoting/static}/themes/default/assets/fonts/icons.woff (100%) rename {static => boardvoting/static}/themes/default/assets/fonts/icons.woff2 (100%) rename {static => boardvoting/static}/themes/default/assets/images/flags.png (100%) rename {templates => boardvoting/templates}/closed_motion_mail.txt (100%) rename {templates => boardvoting/templates}/create_motion_form.html (90%) rename {templates => boardvoting/templates}/create_motion_mail.txt (100%) rename {templates => boardvoting/templates}/denied.html (100%) rename {templates => boardvoting/templates}/direct_vote_form.html (100%) rename {templates => boardvoting/templates}/direct_vote_mail.txt (100%) rename {templates => boardvoting/templates}/edit_motion_form.html (100%) rename {templates => boardvoting/templates}/footer.html (100%) rename {templates => boardvoting/templates}/header.html (84%) rename {templates => boardvoting/templates}/motion.html (100%) rename {templates => boardvoting/templates}/motion_fragments.html (100%) rename {templates => boardvoting/templates}/motions.html (100%) rename {templates => boardvoting/templates}/proxy_vote_form.html (100%) rename {templates => boardvoting/templates}/proxy_vote_mail.txt (100%) rename {templates => boardvoting/templates}/remind_voter_mail.txt (100%) rename {templates => boardvoting/templates}/update_motion_mail.txt (100%) rename {templates => boardvoting/templates}/withdraw_motion_form.html (100%) rename {templates => boardvoting/templates}/withdraw_motion_mail.txt (100%) delete mode 100644 db/dbconf.yml create mode 100644 db/migrations_embedded.go delete mode 100644 static/js/jquery.js delete mode 100644 static/semantic.css delete mode 100644 static/semantic.js delete mode 100644 static/themes/basic/assets/fonts/icons.eot delete mode 100644 static/themes/basic/assets/fonts/icons.svg delete mode 100644 static/themes/basic/assets/fonts/icons.ttf delete mode 100644 static/themes/basic/assets/fonts/icons.woff delete mode 100644 static/themes/github/assets/fonts/octicons-local.ttf delete mode 100644 static/themes/github/assets/fonts/octicons.svg delete mode 100644 static/themes/github/assets/fonts/octicons.ttf delete mode 100644 static/themes/github/assets/fonts/octicons.woff delete mode 100644 static/themes/material/assets/fonts/icons.eot delete mode 100644 static/themes/material/assets/fonts/icons.svg delete mode 100644 static/themes/material/assets/fonts/icons.ttf delete mode 100644 static/themes/material/assets/fonts/icons.woff diff --git a/.gitignore b/.gitignore index 88d5531..beba983 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ cacert-boardvoting config.yaml vendor/ +boardvoting/assets.go diff --git a/Gopkg.lock b/Gopkg.lock index 3f15f69..f4d8a86 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1,12 +1,6 @@ # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. -[[projects]] - branch = "master" - name = "bitbucket.org/liamstask/goose" - packages = ["lib/goose"] - revision = "8488cc47d90c8a502b1c41a462a6d9cc8ee0a895" - [[projects]] name = "github.com/Masterminds/semver" packages = ["."] @@ -25,12 +19,6 @@ revision = "3391d3790d23d03408670993e957e8f408993c34" version = "v1.0.1" -[[projects]] - name = "github.com/go-sql-driver/mysql" - packages = ["."] - revision = "a0583e0143b1624142adab07e0e97fe106d99561" - version = "v1.3" - [[projects]] name = "github.com/gorilla/context" packages = ["."] @@ -70,21 +58,6 @@ ] revision = "de8647470aafe4854c976707c431dbe1eb2822c6" -[[projects]] - branch = "master" - name = "github.com/kylelemons/go-gypsy" - packages = ["yaml"] - revision = "08cad365cd28a7fba23bb1e57aa43c5e18ad8bb8" - -[[projects]] - branch = "master" - name = "github.com/lib/pq" - packages = [ - ".", - "oid" - ] - revision = "83612a56d3dd153a94a629cd64925371c9adad78" - [[projects]] name = "github.com/mattn/go-sqlite3" packages = ["."] @@ -97,22 +70,21 @@ revision = "b2cb9fa56473e98db8caba80237377e83fe44db5" version = "v1" +[[projects]] + branch = "master" + name = "github.com/rubenv/sql-migrate" + packages = [ + ".", + "sqlparse" + ] + revision = "081fe17d19ff4e2dd9f5a0c1158e6bcf74da6906" + [[projects]] name = "github.com/satori/go.uuid" packages = ["."] revision = "f58768cc1a7a7e77a3bd49e98cdd21419399b6a3" version = "v1.2.0" -[[projects]] - name = "github.com/ziutek/mymysql" - packages = [ - "godrv", - "mysql", - "native" - ] - revision = "e08c2f35356576b3c3690c252fe5dca728ae9292" - version = "v1.5.4" - [[projects]] branch = "master" name = "golang.org/x/crypto" @@ -134,6 +106,12 @@ revision = "41f3572897373c5538c50a2402db15db079fa4fd" version = "2.0.0" +[[projects]] + name = "gopkg.in/gorp.v1" + packages = ["."] + revision = "c87af80f3cc5036b55b83d77171e156791085e2e" + version = "v1.7.1" + [[projects]] branch = "v2" name = "gopkg.in/yaml.v2" @@ -143,6 +121,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "9d2b82c5d23650eb62410786c7b529264a0d15172734bbef3ffb1c8a4ac4880f" + inputs-digest = "c6097211865a7f2c761915a6166499eccdb244c65adf046866ac3baed8f6a838" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Makefile b/Makefile index 5a97685..e310918 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,22 @@ VERSION := $(shell git describe --always --dirty=-dev) BUILD := $(shell date --iso-8601=seconds --utc) -GOFILES := $(wildcard *.go) +GOFILES = $(shell find . -type f -name '*.go') +STATICFILES = $(shell find boardvoting/static/ -type f) +TEMPLATES = $(shell find boardvoting/templates/ -type f) +DBMIGRATIONS = $(shell find boardvoting/migrations/ -type f) -cacert-boardvoting: ${GOFILES} +all: cacert-boardvoting + +boardvoting/assets.go: $(DBMIGRATIONS) $(STATICFILES) $(TEMPLATES) $(DBMIGRATIONS) boardvoting/main.go + go generate -v ./boardvoting + +cacert-boardvoting: ${GOFILES} boardvoting/assets.go go build -o $@ -x -ldflags " -X 'main.version=${VERSION}' -X 'main.build=${BUILD}'" clean: rm -f cacert-boardvoting -.PHONY: clean +distclean: clean + rm -f boardvoting/assets.go + +.PHONY: clean distclean all diff --git a/boardvoting.go b/boardvoting.go index 93b1532..c1fa28f 100644 --- a/boardvoting.go +++ b/boardvoting.go @@ -5,12 +5,14 @@ import ( "context" "crypto/tls" "crypto/x509" + "database/sql" "encoding/base64" "encoding/pem" + "flag" "fmt" + "github.com/Masterminds/sprig" "github.com/gorilla/sessions" - "github.com/jmoiron/sqlx" _ "github.com/mattn/go-sqlite3" "github.com/op/go-logging" "gopkg.in/yaml.v2" @@ -24,6 +26,7 @@ import ( "time" ) +var configFile string var config *Config var store *sessions.CookieStore var version = "undefined" @@ -87,10 +90,10 @@ func authenticateRequest(w http.ResponseWriter, r *http.Request, handler func(ht needsAuth, ok := r.Context().Value(ctxNeedsAuth).(bool) if ok && needsAuth { var templateContext struct { - PageTitle string - Voter *Voter - Flashes interface{} - Emails []string + PageTitle string + Voter *Voter + Flashes interface{} + Emails []string } for k := range emailsTried { templateContext.Emails = append(templateContext.Emails, k) @@ -663,6 +666,8 @@ type Config struct { CookieSecret string `yaml:"cookie_secret"` BaseURL string `yaml:"base_url"` MigrationsPath string `yaml:"migrations_path"` + HttpAddress string `yaml:"http_address"` + HttpsAddress string `yaml:"https_address"` MailServer struct { Host string `yaml:"host"` Port int `yaml:"port"` @@ -700,7 +705,7 @@ func setupLogging(ctx context.Context) { } func readConfig() { - source, err := ioutil.ReadFile("config.yaml") + source, err := ioutil.ReadFile(configFile) if err != nil { log.Panicf("Opening configuration file failed: %v", err) } @@ -708,6 +713,13 @@ func readConfig() { log.Panicf("Loading configuration failed: %v", err) } + if config.HttpsAddress == "" { + config.HttpsAddress = ":8443" + } + if config.HttpAddress == "" { + config.HttpAddress = ":8080" + } + cookieSecret, err := base64.StdEncoding.DecodeString(config.CookieSecret) if err != nil { log.Panicf("Decoding cookie secret failed: %v", err) @@ -721,7 +733,7 @@ func readConfig() { } func setupDbConfig(ctx context.Context) { - database, err := sqlx.Open("sqlite3", config.DatabaseFile) + database, err := sql.Open("sqlite3", config.DatabaseFile) if err != nil { log.Panicf("Opening database failed: %v", err) } @@ -789,7 +801,14 @@ func setupTLSConfig() (tlsConfig *tls.Config) { return } +func init() { + flag.StringVar( + &configFile, "config", "config.yaml", "Configuration file name") +} + func main() { + flag.Parse() + var stopAll func() executionContext, stopAll := context.WithCancel(context.Background()) setupLogging(executionContext) @@ -805,15 +824,15 @@ func main() { log.Infof("CAcert Board Voting version %s, build %s", version, build) server := &http.Server{ - Addr: ":8443", + Addr: config.HttpsAddress, TLSConfig: tlsConfig, } - log.Infof("Launching application on https://localhost%s/", server.Addr) + log.Infof("Launching application on https://%s/", server.Addr) errs := make(chan error, 1) go func() { - if err := http.ListenAndServe(":8080", http.RedirectHandler(config.BaseURL, http.StatusMovedPermanently)); err != nil { + if err := http.ListenAndServe(config.HttpsAddress, http.RedirectHandler(config.BaseURL, http.StatusMovedPermanently)); err != nil { errs <- err } close(errs) diff --git a/boardvoting/main.go b/boardvoting/main.go new file mode 100644 index 0000000..5c8e6eb --- /dev/null +++ b/boardvoting/main.go @@ -0,0 +1,3 @@ +package boardvoting + +//go:generate go-bindata -pkg $GOPACKAGE -o assets.go ./migrations/... ./static/... ./templates/... diff --git a/db/migrations/20170421134856_0.1.0_original.sql b/boardvoting/migrations/20170421134856_0.1.0_original.sql similarity index 96% rename from db/migrations/20170421134856_0.1.0_original.sql rename to boardvoting/migrations/20170421134856_0.1.0_original.sql index ae32f38..fae8391 100644 --- a/db/migrations/20170421134856_0.1.0_original.sql +++ b/boardvoting/migrations/20170421134856_0.1.0_original.sql @@ -1,5 +1,5 @@ --- +goose Up +-- +migrate Up -- SQL in section 'Up' is executed when this migration is applied CREATE TABLE IF NOT EXISTS votes (decision INT4, voter INT4, vote INT4, voted DATETIME, notes text default ''); CREATE TABLE IF NOT EXISTS emails (voter INT4, address VARCHAR(255)); @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS voters (id INTEGER PRIMARY KEY, name VARCHAR(255), en CREATE TABLE IF NOT EXISTS decisions (id INTEGER PRIMARY KEY, proposed DATETIME, proponent INTEGER, title VARCHAR(255), content TEXT, quorum INTEGER, majority INTEGER, status INTEGER, due DATETIME, modified DATETIME, tag varchar(255), votetype INT4 DEFAULT 0 NOT NULL); --- +goose Down +-- +migrate Down -- SQL section 'Down' is executed when this migration is rolled back DROP TABLE votes; DROP TABLE decisions; diff --git a/db/migrations/20170421143052_fix_duplicates.sql b/boardvoting/migrations/20170421143052_fix_duplicates.sql similarity index 84% rename from db/migrations/20170421143052_fix_duplicates.sql rename to boardvoting/migrations/20170421143052_fix_duplicates.sql index a0ffbf0..80d2ff2 100644 --- a/db/migrations/20170421143052_fix_duplicates.sql +++ b/boardvoting/migrations/20170421143052_fix_duplicates.sql @@ -1,5 +1,5 @@ --- +goose Up +-- +migrate Up -- SQL in section 'Up' is executed when this migration is applied -- Peter Yuill has a duplicate entry in the voters table he has ids 17 and 31 @@ -9,7 +9,7 @@ UPDATE decisions SET proponent=17 WHERE proponent=31; DELETE FROM emails WHERE voter=31; DELETE FROM voters WHERE id=31; --- +goose Down +-- +migrate Down -- SQL section 'Down' is executed when this migration is rolled back -- There is no useful backward migration \ No newline at end of file diff --git a/db/migrations/20170421143114_add_constraints.sql b/boardvoting/migrations/20170421143114_add_constraints.sql similarity index 99% rename from db/migrations/20170421143114_add_constraints.sql rename to boardvoting/migrations/20170421143114_add_constraints.sql index c1ddfbe..2fc969e 100644 --- a/db/migrations/20170421143114_add_constraints.sql +++ b/boardvoting/migrations/20170421143114_add_constraints.sql @@ -1,4 +1,4 @@ --- +goose Up +-- +migrate Up -- SQL in section 'Up' is executed when this migration is applied CREATE TABLE voters_new ( id INTEGER PRIMARY KEY, diff --git a/static/js/jquery.min.js b/boardvoting/static/js/jquery.min.js similarity index 100% rename from static/js/jquery.min.js rename to boardvoting/static/js/jquery.min.js diff --git a/static/js/jquery.min.map b/boardvoting/static/js/jquery.min.map similarity index 100% rename from static/js/jquery.min.map rename to boardvoting/static/js/jquery.min.map diff --git a/static/semantic.min.css b/boardvoting/static/semantic.min.css similarity index 100% rename from static/semantic.min.css rename to boardvoting/static/semantic.min.css diff --git a/static/semantic.min.js b/boardvoting/static/semantic.min.js similarity index 100% rename from static/semantic.min.js rename to boardvoting/static/semantic.min.js diff --git a/static/styles.css b/boardvoting/static/styles.css similarity index 89% rename from static/styles.css rename to boardvoting/static/styles.css index 1cf3d72..18a0000 100644 --- a/static/styles.css +++ b/boardvoting/static/styles.css @@ -1,5 +1,5 @@ html, body, th, td { - font-family: Verdana, Arial, Sans-Serif; + font-family: Verdana, Arial, sans-serif; font-size:10px; } table, tr, td, th { diff --git a/static/themes/default/assets/fonts/icons.eot b/boardvoting/static/themes/default/assets/fonts/icons.eot similarity index 100% rename from static/themes/default/assets/fonts/icons.eot rename to boardvoting/static/themes/default/assets/fonts/icons.eot diff --git a/static/themes/default/assets/fonts/icons.svg b/boardvoting/static/themes/default/assets/fonts/icons.svg similarity index 100% rename from static/themes/default/assets/fonts/icons.svg rename to boardvoting/static/themes/default/assets/fonts/icons.svg diff --git a/static/themes/default/assets/fonts/icons.ttf b/boardvoting/static/themes/default/assets/fonts/icons.ttf similarity index 100% rename from static/themes/default/assets/fonts/icons.ttf rename to boardvoting/static/themes/default/assets/fonts/icons.ttf diff --git a/static/themes/default/assets/fonts/icons.woff b/boardvoting/static/themes/default/assets/fonts/icons.woff similarity index 100% rename from static/themes/default/assets/fonts/icons.woff rename to boardvoting/static/themes/default/assets/fonts/icons.woff diff --git a/static/themes/default/assets/fonts/icons.woff2 b/boardvoting/static/themes/default/assets/fonts/icons.woff2 similarity index 100% rename from static/themes/default/assets/fonts/icons.woff2 rename to boardvoting/static/themes/default/assets/fonts/icons.woff2 diff --git a/static/themes/default/assets/images/flags.png b/boardvoting/static/themes/default/assets/images/flags.png similarity index 100% rename from static/themes/default/assets/images/flags.png rename to boardvoting/static/themes/default/assets/images/flags.png diff --git a/templates/closed_motion_mail.txt b/boardvoting/templates/closed_motion_mail.txt similarity index 100% rename from templates/closed_motion_mail.txt rename to boardvoting/templates/closed_motion_mail.txt diff --git a/templates/create_motion_form.html b/boardvoting/templates/create_motion_form.html similarity index 90% rename from templates/create_motion_form.html rename to boardvoting/templates/create_motion_form.html index d87bac8..b3cbadb 100644 --- a/templates/create_motion_form.html +++ b/boardvoting/templates/create_motion_form.html @@ -26,16 +26,16 @@
- +
- +
-