mirror of
https://github.com/Kugelschieber/schnittfest.git
synced 2026-01-18 10:20:27 +00:00
Basic setup.
This commit is contained in:
9
go.mod
Normal file
9
go.mod
Normal file
@@ -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
|
||||||
|
)
|
||||||
6
go.sum
Normal file
6
go.sum
Normal file
@@ -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=
|
||||||
113
main.go
Normal file
113
main.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
1
pages/landing_page.go
Normal file
1
pages/landing_page.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package pages
|
||||||
0
template/landing_page.html
Normal file
0
template/landing_page.html
Normal file
Reference in New Issue
Block a user