diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..43397a3 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/Kugelschieber/schnittfest + +go 1.13 + +require ( + github.com/emvi/logbuch v0.0.0-20191002134629-fd76a46de20c // indirect + github.com/gorilla/mux v1.7.3 // indirect + github.com/rs/cors v1.7.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a8bb12c --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/emvi/logbuch v0.0.0-20191002134629-fd76a46de20c h1:LsG8/aichRG7oGqTz77PQJuArY1id0kQttxNBuC/C3Q= +github.com/emvi/logbuch v0.0.0-20191002134629-fd76a46de20c/go.mod h1:J2Wgbr3BuSc1JO+D2MBVh6q3WPVSK5GzktwWz8pvkKw= +github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= +github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= diff --git a/main.go b/main.go new file mode 100644 index 0000000..8d8a47f --- /dev/null +++ b/main.go @@ -0,0 +1,113 @@ +package main + +import ( + "github.com/emvi/logbuch" + "github.com/gorilla/mux" + "github.com/rs/cors" + "net/http" + "os" + "strconv" + "strings" + "time" +) + +const ( + staticDir = "public/static" + staticDirPrefix = "/static/" + envPrefix = "SCHNITTFEST_" + pwdString = "PASSWORD" // do not log passwords! + + defaultHttpWriteTimeout = 20 + defaultHttpReadTimeout = 20 +) + +func configureLog() { + logbuch.Info("Configure logging...") + level := strings.ToLower(os.Getenv("SCHNITTFEST_LOGLEVEL")) + + if level == "debug" { + logbuch.SetLevel(logbuch.LevelDebug) + } else if level == "info" { + logbuch.SetLevel(logbuch.LevelInfo) + } else { + logbuch.SetLevel(logbuch.LevelWarning) + } +} + +func logEnvConfig() { + for _, e := range os.Environ() { + if strings.HasPrefix(e, envPrefix) && !strings.Contains(e, pwdString) { + pair := strings.Split(e, "=") + logbuch.Info(pair[0] + "=" + pair[1]) + } + } +} + +func setupRouter() *mux.Router { + router := mux.NewRouter() + router.PathPrefix(staticDirPrefix).Handler(http.StripPrefix(staticDirPrefix, http.FileServer(http.Dir(staticDir)))) + return router +} + +func configureCors(router *mux.Router) http.Handler { + logbuch.Info("Configuring CORS...") + + origins := strings.Split(os.Getenv("ACCWEB_ALLOWED_ORIGINS"), ",") + c := cors.New(cors.Options{ + AllowedOrigins: origins, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}, + AllowedHeaders: []string{"*"}, + AllowCredentials: true, + Debug: strings.ToLower(os.Getenv("ACCWEB_CORS_LOGLEVEL")) == "debug", + }) + return c.Handler(router) +} + +func start(handler http.Handler) { + logbuch.Info("Starting server...") + + writeTimeout := defaultHttpWriteTimeout + readTimeout := defaultHttpReadTimeout + var err error + + if os.Getenv("ACCWEB_HTTP_WRITE_TIMEOUT") != "" { + writeTimeout, err = strconv.Atoi(os.Getenv("ACCWEB_HTTP_WRITE_TIMEOUT")) + + if err != nil { + logbuch.Fatal(err.Error()) + } + } + + if os.Getenv("ACCWEB_HTTP_READ_TIMEOUT") != "" { + readTimeout, err = strconv.Atoi(os.Getenv("ACCWEB_HTTP_READ_TIMEOUT")) + + if err != nil { + logbuch.Fatal(err.Error()) + } + } + + logbuch.Info("Using HTTP read/write timeouts", logbuch.Fields{"write_timeout": writeTimeout, "read_timeout": readTimeout}) + + server := &http.Server{ + Handler: handler, + Addr: os.Getenv("ACCWEB_HOST"), + WriteTimeout: time.Duration(writeTimeout) * time.Second, + ReadTimeout: time.Duration(readTimeout) * time.Second, + } + + // TODO certmagic + if strings.ToLower(os.Getenv("ACCWEB_TLS_ENABLE")) == "true" { + logbuch.Info("TLS enabled") + logbuch.Fatal("Error starting server", server.ListenAndServeTLS(os.Getenv("ACCWEB_TLS_CERT"), os.Getenv("ACCWEB_TLS_PKEY"))) + } else { + logbuch.Fatal("Error starting server", server.ListenAndServe()) + } +} + +func main() { + configureLog() + logEnvConfig() + router := setupRouter() + corsConfig := configureCors(router) + start(corsConfig) +} diff --git a/pages/landing_page.go b/pages/landing_page.go new file mode 100644 index 0000000..76e382d --- /dev/null +++ b/pages/landing_page.go @@ -0,0 +1 @@ +package pages diff --git a/template/landing_page.html b/template/landing_page.html new file mode 100644 index 0000000..e69de29