Skip to content

Commit

Permalink
database max connections, recreated sendLog middleware, StrictSlash i…
Browse files Browse the repository at this point in the history
…s now set to true (base path), removed OnSave method for Mobile notifier, increase health check time.
  • Loading branch information
hunterlong committed Jan 13, 2020
1 parent 1aca05e commit d107d3c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -28,6 +28,6 @@ WORKDIR /app
VOLUME /app
EXPOSE $PORT

HEALTHCHECK --interval=5s --timeout=5s --retries=5 CMD curl -s "http://localhost:$PORT/health" | jq -r -e ".online==true"
HEALTHCHECK --interval=60s --timeout=10s --retries=3 CMD curl -s "http://localhost:$PORT/health" | jq -r -e ".online==true"

CMD statping -port $PORT
8 changes: 5 additions & 3 deletions core/database.go
Expand Up @@ -253,9 +253,11 @@ func (c *Core) Connect(retry bool, location string) error {
}
}
log.WithFields(utils.ToFields(dbSession)).Debugln("connected to database")
if dbType == "sqlite3" {
dbSession.DB().SetMaxOpenConns(1)
}

dbSession.DB().SetMaxOpenConns(5)
dbSession.DB().SetMaxIdleConns(5)
dbSession.DB().SetConnMaxLifetime(1 * time.Minute)

if dbSession.DB().Ping() == nil {
DbSession = dbSession
if utils.VerboseMode >= 4 {
Expand Down
1 change: 0 additions & 1 deletion handlers/handlers.go
Expand Up @@ -194,7 +194,6 @@ func loadTemplate(w http.ResponseWriter, r *http.Request) error {
return err
}
// render all templates
mainTemplate.Funcs(handlerFuncs(w, r))
for _, temp := range templates {
tmp, _ := source.TmplBox.String(temp)
mainTemplate, err = mainTemplate.Parse(tmp)
Expand Down
4 changes: 2 additions & 2 deletions handlers/middleware.go
Expand Up @@ -33,10 +33,9 @@ func basicAuthHandler(next http.Handler) http.Handler {
}

// sendLog is a http middleware that will log the duration of request and other useful fields
func sendLog(handler func(w http.ResponseWriter, r *http.Request)) http.Handler {
func sendLog(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t1 := utils.Now()
handler(w, r)
t2 := utils.Now().Sub(t1)
if r.RequestURI == "/logs/line" {
return
Expand All @@ -46,6 +45,7 @@ func sendLog(handler func(w http.ResponseWriter, r *http.Request)) http.Handler
WithField("method", r.Method).
WithField("load_micro_seconds", t2.Microseconds()).
Infoln(fmt.Sprintf("%v (%v) | IP: %v", r.RequestURI, r.Method, r.Host))
next.ServeHTTP(w, r)
})
}

Expand Down
27 changes: 14 additions & 13 deletions handlers/routes.go
Expand Up @@ -38,13 +38,14 @@ var (
func Router() *mux.Router {
dir := utils.Directory
CacheStorage = NewStorage()
r := mux.NewRouter()
r := mux.NewRouter().StrictSlash(true)
if os.Getenv("AUTH_USERNAME") != "" && os.Getenv("AUTH_PASSWORD") != "" {
authUser = os.Getenv("AUTH_USERNAME")
authPass = os.Getenv("AUTH_PASSWORD")
r.Use(basicAuthHandler)
}
r.Handle("/", sendLog(indexHandler))
r.Use(sendLog)
r.Handle("/", http.HandlerFunc(indexHandler))
if source.UsingAssets(dir) {
indexHandler := http.FileServer(http.Dir(dir + "/assets/"))
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir(dir+"/assets/css"))))
Expand All @@ -61,12 +62,12 @@ func Router() *mux.Router {
r.PathPrefix("/favicon.ico").Handler(http.FileServer(source.TmplBox.HTTPBox()))
r.PathPrefix("/banner.png").Handler(http.FileServer(source.TmplBox.HTTPBox()))
}
r.Handle("/charts.js", sendLog(renderServiceChartsHandler))
r.Handle("/setup", sendLog(setupHandler)).Methods("GET")
r.Handle("/setup", sendLog(processSetupHandler)).Methods("POST")
r.Handle("/dashboard", sendLog(dashboardHandler)).Methods("GET")
r.Handle("/dashboard", sendLog(loginHandler)).Methods("POST")
r.Handle("/logout", sendLog(logoutHandler))
r.Handle("/charts.js", http.HandlerFunc(renderServiceChartsHandler))
r.Handle("/setup", http.HandlerFunc(setupHandler)).Methods("GET")
r.Handle("/setup", http.HandlerFunc(processSetupHandler)).Methods("POST")
r.Handle("/dashboard", http.HandlerFunc(dashboardHandler)).Methods("GET")
r.Handle("/dashboard", http.HandlerFunc(loginHandler)).Methods("POST")
r.Handle("/logout", http.HandlerFunc(logoutHandler))
r.Handle("/plugins/download/{name}", authenticated(pluginsDownloadHandler, true))
r.Handle("/plugins/{name}/save", authenticated(pluginSavedHandler, true)).Methods("POST")
r.Handle("/help", authenticated(helpHandler, true))
Expand Down Expand Up @@ -97,11 +98,11 @@ func Router() *mux.Router {
// SERVICE Routes
r.Handle("/services", authenticated(servicesHandler, true)).Methods("GET")
r.Handle("/service/create", authenticated(createServiceHandler, true)).Methods("GET")
r.Handle("/service/{id}", sendLog(servicesViewHandler)).Methods("GET")
r.Handle("/service/{id}", readOnly(servicesViewHandler, true)).Methods("GET")
r.Handle("/service/{id}/edit", authenticated(servicesViewHandler, true)).Methods("GET")
r.Handle("/service/{id}/delete_failures", authenticated(servicesDeleteFailuresHandler, true)).Methods("GET")

r.Handle("/group/{id}", sendLog(groupViewHandler)).Methods("GET")
r.Handle("/group/{id}", http.HandlerFunc(groupViewHandler)).Methods("GET")

// API Routes
r.Handle("/api", authenticated(apiIndexHandler, false))
Expand Down Expand Up @@ -163,7 +164,7 @@ func Router() *mux.Router {
r.Handle("/api/checkin/{api}", authenticated(apiCheckinHandler, false)).Methods("GET")
r.Handle("/api/checkin", authenticated(checkinCreateHandler, false)).Methods("POST")
r.Handle("/api/checkin/{api}", authenticated(checkinDeleteHandler, false)).Methods("DELETE")
r.Handle("/checkin/{api}", sendLog(checkinHitHandler))
r.Handle("/checkin/{api}", http.HandlerFunc(checkinHitHandler))

// Static Files Routes
r.PathPrefix("/files/postman.json").Handler(http.StripPrefix("/files/", http.FileServer(source.TmplBox.HTTPBox())))
Expand All @@ -172,10 +173,10 @@ func Router() *mux.Router {

// API Generic Routes
r.Handle("/metrics", readOnly(prometheusHandler, false))
r.Handle("/health", sendLog(healthCheckHandler))
r.Handle("/health", http.HandlerFunc(healthCheckHandler))
r.Handle("/.well-known/", http.StripPrefix("/.well-known/", http.FileServer(http.Dir(dir+"/.well-known"))))

r.NotFoundHandler = sendLog(error404Handler)
r.NotFoundHandler = http.HandlerFunc(error404Handler)
return r
}

Expand Down
6 changes: 0 additions & 6 deletions notifiers/mobile.go
Expand Up @@ -117,12 +117,6 @@ func (u *mobilePush) OnSuccess(s *types.Service) {

// OnSave triggers when this notifier has been saved
func (u *mobilePush) OnSave() error {
msg := &pushArray{
Message: "The Mobile Notifier has been saved",
Title: "Notification Saved",
Topic: mobileIdentifier,
}
u.AddQueue("saved", msg)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion version.txt
@@ -1 +1 @@
0.80.69
0.80.70

0 comments on commit d107d3c

Please sign in to comment.