From 3cc63a0f50a7a8c94cb8103576821df67186af6b Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Fri, 6 Jul 2018 22:02:47 -0700 Subject: [PATCH] load from .env - view updates - enable CDN --- .travis.yml | 2 +- Dockerfile | 2 +- core/configs.go | 63 +++++++++++++++++----- core/core.go | 12 ++++- core/database.go | 3 ++ core/export.go | 2 +- handlers/handlers.go | 3 ++ handlers/routes.go | 1 + handlers/settings.go | 1 + main.go | 6 ++- source/tmpl/dashboard.html | 13 +++++ source/tmpl/error_404.html | 13 +++++ source/tmpl/help.html | 19 +++++-- source/tmpl/index.html | 18 ++++--- source/tmpl/login.html | 14 ++++- source/tmpl/service.html | 16 +++++- source/tmpl/services.html | 14 ++++- source/tmpl/settings.html | 40 +++++++++++--- source/tmpl/setup.html | 11 ++++ source/tmpl/user.html | 104 +++++++++++++++++++++++++++++++++++++ source/tmpl/users.html | 13 +++++ 21 files changed, 328 insertions(+), 42 deletions(-) create mode 100644 source/tmpl/user.html diff --git a/.travis.yml b/.travis.yml index 31fbade4..c6b9cdfc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ services: env: global: - - VERSION=0.29.6 + - VERSION=0.29.7 - DB_HOST=localhost - DB_USER=travis - DB_PASS= diff --git a/Dockerfile b/Dockerfile index 4cb9b91d..17a5dc5e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine:latest -ENV VERSION=v0.29.6 +ENV VERSION=v0.29.7 RUN apk --no-cache add libstdc++ ca-certificates RUN wget -q https://github.com/hunterlong/statup/releases/download/$VERSION/statup-linux-alpine.tar.gz && \ diff --git a/core/configs.go b/core/configs.go index 34e4dce7..8f4c5a77 100644 --- a/core/configs.go +++ b/core/configs.go @@ -1,25 +1,62 @@ package core import ( + "errors" "github.com/go-yaml/yaml" "github.com/hunterlong/statup/types" "io/ioutil" + "os" ) -type Config types.Config - -var ( - VERSION string -) - -func LoadConfig() (*Config, error) { - var config *Config +func LoadConfig() (*types.Config, error) { + if os.Getenv("DB_CONN") != "" { + return LoadUsingEnv() + } + Configs = new(types.Config) file, err := ioutil.ReadFile("config.yml") if err != nil { - return nil, err + return nil, errors.New("config.yml file not found - starting in setup mode") } - err = yaml.Unmarshal(file, &config) - Configs = config - CoreApp.DbConnection = config.Connection - return config, err + err = yaml.Unmarshal(file, &Configs) + CoreApp.DbConnection = Configs.Connection + return Configs, err +} + +func LoadUsingEnv() (*types.Config, error) { + Configs = new(types.Config) + if os.Getenv("DB_CONN") == "" { + return nil, errors.New("Missing DB_CONN environment variable") + } + if os.Getenv("DB_HOST") == "" { + return nil, errors.New("Missing DB_HOST environment variable") + } + if os.Getenv("DB_USER") == "" { + return nil, errors.New("Missing DB_USER environment variable") + } + if os.Getenv("DB_PASS") == "" { + return nil, errors.New("Missing DB_PASS environment variable") + } + if os.Getenv("DB_DATABASE") == "" { + return nil, errors.New("Missing DB_DATABASE environment variable") + } + Configs.Connection = os.Getenv("DB_CONN") + Configs.Host = os.Getenv("DB_HOST") + Configs.Port = os.Getenv("DB_PORT") + Configs.User = os.Getenv("DB_USER") + Configs.Password = os.Getenv("DB_PASS") + Configs.Database = os.Getenv("DB_DATABASE") + CoreApp.DbConnection = os.Getenv("DB_CONN") + CoreApp.Name = os.Getenv("NAME") + CoreApp.Domain = os.Getenv("DOMAIN") + if os.Getenv("USE_CDN") == "true" { + CoreApp.UseCdn = true + } + return Configs, nil +} + +func ifOr(val, def string) string { + if val == "" { + return def + } + return val } diff --git a/core/core.go b/core/core.go index a4cc807d..d6804229 100644 --- a/core/core.go +++ b/core/core.go @@ -4,6 +4,8 @@ import ( "github.com/GeertJohan/go.rice" "github.com/hunterlong/statup/plugin" "github.com/hunterlong/statup/types" + "github.com/pkg/errors" + "os" "time" ) @@ -27,13 +29,12 @@ type Core struct { Repos []PluginJSON AllPlugins []plugin.PluginActions Communications []*types.Communication - OfflineAssets bool DbConnection string started time.Time } var ( - Configs *Config + Configs *types.Config CoreApp *Core SqlBox *rice.Box CssBox *rice.Box @@ -43,6 +44,7 @@ var ( EmailBox *rice.Box SetupMode bool UsingAssets bool + VERSION string ) func init() { @@ -107,6 +109,9 @@ func (c Core) AllOnline() bool { func SelectLastMigration() (int64, error) { var c *Core + if DbSession == nil { + return 0, errors.New("Database connection has not been created yet") + } err := DbSession.Collection("core").Find().One(&c) if err != nil { return 0, err @@ -124,6 +129,9 @@ func SelectCore() (*Core, error) { CoreApp.DbConnection = Configs.Connection CoreApp.Version = VERSION CoreApp.Services, _ = SelectAllServices() + if os.Getenv("USE_CDN") == "true" { + CoreApp.UseCdn = true + } //store = sessions.NewCookieStore([]byte(core.ApiSecret)) return CoreApp, err } diff --git a/core/database.go b/core/database.go index 58f5c2de..40e0df5a 100644 --- a/core/database.go +++ b/core/database.go @@ -157,6 +157,9 @@ func reverseSlice(s []string) []string { func RunDatabaseUpgrades() error { var err error currentMigration, err = SelectLastMigration() + if err != nil { + return err + } utils.Log(1, fmt.Sprintf("Checking for Database Upgrades since #%v", currentMigration)) upgrade, _ := SqlBox.String(CoreApp.DbConnection + "_upgrade.sql") // parse db version and upgrade file diff --git a/core/export.go b/core/export.go index 3a257921..e372b476 100644 --- a/core/export.go +++ b/core/export.go @@ -8,7 +8,7 @@ import ( ) func ExportIndexHTML() string { - CoreApp.OfflineAssets = true + CoreApp.UseCdn = true //out := index{*CoreApp, CoreApp.Services} nav, _ := TmplBox.String("nav.html") footer, _ := TmplBox.String("footer.html") diff --git a/handlers/handlers.go b/handlers/handlers.go index 2856795d..3061314c 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -86,6 +86,9 @@ func ExecuteResponse(w http.ResponseWriter, r *http.Request, file string, data i "VERSION": func() string { return core.VERSION }, + "USE_CDN": func() bool { + return core.CoreApp.UseCdn + }, "underscore": func(html string) string { return utils.UnderScoreString(html) }, diff --git a/handlers/routes.go b/handlers/routes.go index 5461e889..145e55ca 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -29,6 +29,7 @@ func Router() *mux.Router { r.Handle("/service/{id}/delete_failures", http.HandlerFunc(ServicesDeleteFailuresHandler)).Methods("GET") r.Handle("/service/{id}/checkin", http.HandlerFunc(CheckinCreateUpdateHandler)).Methods("POST") r.Handle("/users", http.HandlerFunc(UsersHandler)).Methods("GET") + r.Handle("/user/{id}", http.HandlerFunc(UsersHandler)).Methods("GET") r.Handle("/users", http.HandlerFunc(CreateUserHandler)).Methods("POST") r.Handle("/users/{id}/delete", http.HandlerFunc(UsersDeleteHandler)).Methods("GET") r.Handle("/settings", http.HandlerFunc(PluginsHandler)).Methods("GET") diff --git a/handlers/settings.go b/handlers/settings.go index 633f8aae..f95d95ec 100644 --- a/handlers/settings.go +++ b/handlers/settings.go @@ -54,6 +54,7 @@ func SaveSettingsHandler(w http.ResponseWriter, r *http.Request) { if domain != core.CoreApp.Domain { core.CoreApp.Domain = domain } + core.CoreApp.UseCdn = (r.PostForm.Get("enable_cdn") == "on") core.CoreApp.Update() core.OnSettingsSaved(core.CoreApp) http.Redirect(w, r, "/settings", http.StatusSeeOther) diff --git a/main.go b/main.go index 4ef21d58..eaa305f3 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,8 @@ import ( ) var ( - VERSION string + VERSION string + usingEnv bool ) func init() { @@ -36,7 +37,7 @@ func main() { core.Configs, err = core.LoadConfig() if err != nil { - utils.Log(2, "config.yml file not found - starting in setup mode") + utils.Log(3, err) core.SetupMode = true handlers.RunHTTPServer() } @@ -56,6 +57,7 @@ func LoadDotEnvs() { err := godotenv.Load() if err == nil { utils.Log(1, "Environment file '.env' Loaded") + usingEnv = true } } diff --git a/source/tmpl/dashboard.html b/source/tmpl/dashboard.html index 6e05d55c..aff72068 100644 --- a/source/tmpl/dashboard.html +++ b/source/tmpl/dashboard.html @@ -3,8 +3,13 @@ +{{if USE_CDN}} + + +{{ else }} +{{end}} Statup | Dashboard @@ -83,8 +88,16 @@ {{template "footer"}} +{{if USE_CDN}} + + + +{{ else }} + +{{end}} + \ No newline at end of file diff --git a/source/tmpl/error_404.html b/source/tmpl/error_404.html index 5034834f..c3b6a8e1 100644 --- a/source/tmpl/error_404.html +++ b/source/tmpl/error_404.html @@ -3,8 +3,13 @@ +{{if USE_CDN}} + + +{{ else }} +{{end}} Statup | Page Not Found @@ -25,7 +30,15 @@ {{template "footer"}} +{{if USE_CDN}} + + + +{{ else }} + +{{end}} + \ No newline at end of file diff --git a/source/tmpl/help.html b/source/tmpl/help.html index ca31f788..03436fc1 100644 --- a/source/tmpl/help.html +++ b/source/tmpl/help.html @@ -3,10 +3,13 @@ - +{{if USE_CDN}} + + +{{ else }} - +{{end}} Statup | Help @@ -52,7 +55,15 @@ {{template "footer"}} - - +{{if USE_CDN}} + + + +{{ else }} + + + +{{end}} + \ No newline at end of file diff --git a/source/tmpl/index.html b/source/tmpl/index.html index 26f06a49..fb85c965 100644 --- a/source/tmpl/index.html +++ b/source/tmpl/index.html @@ -4,12 +4,13 @@ - {{if .Core.OfflineAssets}} - + {{if USE_CDN}} + + {{ else }} + {{end}} - {{.Core.Name}} Status @@ -101,16 +102,17 @@ {{template "footer"}} -{{if .Core.OfflineAssets}} - - - +{{if USE_CDN}} + + + + {{ else }} -{{end}} +{{end}} {{ if .Core.Style }} diff --git a/source/tmpl/login.html b/source/tmpl/login.html index 84db7e13..df73285e 100644 --- a/source/tmpl/login.html +++ b/source/tmpl/login.html @@ -3,9 +3,13 @@ +{{if USE_CDN}} + + +{{ else }} - +{{end}} Statup | Login @@ -47,7 +51,15 @@ {{template "footer"}} +{{if USE_CDN}} + + + +{{ else }} + +{{end}} + \ No newline at end of file diff --git a/source/tmpl/service.html b/source/tmpl/service.html index 9b1217c7..9e086755 100644 --- a/source/tmpl/service.html +++ b/source/tmpl/service.html @@ -3,9 +3,15 @@ +{{if USE_CDN}} + + + +{{ else }} +{{end}} Statup | {{.Name}} Service @@ -245,7 +251,15 @@ }); +{{if USE_CDN}} + + + +{{ else }} - + + +{{end}} + \ No newline at end of file diff --git a/source/tmpl/services.html b/source/tmpl/services.html index f9523f41..9694107d 100644 --- a/source/tmpl/services.html +++ b/source/tmpl/services.html @@ -3,9 +3,13 @@ +{{if USE_CDN}} + + +{{ else }} - +{{end}} Statup | Services @@ -120,7 +124,15 @@ {{template "footer"}} +{{if USE_CDN}} + + + +{{ else }} + +{{end}} + \ No newline at end of file diff --git a/source/tmpl/settings.html b/source/tmpl/settings.html index 4de6ebee..926eea21 100644 --- a/source/tmpl/settings.html +++ b/source/tmpl/settings.html @@ -3,10 +3,15 @@ - +{{if USE_CDN}} + + +{{ else }} + + +{{end}} - Statup | Settings @@ -55,9 +60,18 @@ -
- - +
+
+ + +
+
+ + + + +
+
@@ -226,12 +240,24 @@
{{template "footer"}} - - +{{if USE_CDN}} + + +{{ else }} + + +{{end}} + + +{{if USE_CDN}} + +{{else}} +{{end}} + \ No newline at end of file diff --git a/source/tmpl/setup.html b/source/tmpl/setup.html index 309efd79..c0b1c25d 100644 --- a/source/tmpl/setup.html +++ b/source/tmpl/setup.html @@ -3,8 +3,13 @@ +{{if USE_CDN}} + + +{{ else }} +{{end}} Statup | Setup @@ -110,9 +115,15 @@ {{template "footer"}} +{{if USE_CDN}} + + + +{{ else }} +{{end}} \ No newline at end of file diff --git a/source/tmpl/user.html b/source/tmpl/user.html new file mode 100644 index 00000000..06cce143 --- /dev/null +++ b/source/tmpl/user.html @@ -0,0 +1,104 @@ + + + + + +{{if USE_CDN}} + + +{{ else }} + + +{{end}} + + Statup | Users + + + + +
+ +{{template "nav"}} + +
+ +

Users

+ + + + + + + + + + {{range .}} + + + + + {{end}} + +
Username
{{.Username}} +
+ Delete +
+
+ +

Create User

+
+
+ +
+ +
+
+ + + + +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
+ +
+ +
+ +{{template "footer"}} + +{{if USE_CDN}} + + + +{{ else }} + + + +{{end}} + + + \ No newline at end of file diff --git a/source/tmpl/users.html b/source/tmpl/users.html index adf4b3da..364e8237 100644 --- a/source/tmpl/users.html +++ b/source/tmpl/users.html @@ -3,8 +3,13 @@ +{{if USE_CDN}} + + +{{ else }} +{{end}} Statup | Users @@ -85,7 +90,15 @@ {{template "footer"}} +{{if USE_CDN}} + + + +{{ else }} + +{{end}} + \ No newline at end of file