Skip to content

Commit

Permalink
improved timeouts, add session dir, patch undocumented memory leakin …
Browse files Browse the repository at this point in the history
…gorilla/context
  • Loading branch information
lrstanley committed Aug 3, 2017
1 parent 4cab2b1 commit 5f7c421
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
20 changes: 15 additions & 5 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ import (
"github.com/timshannon/bolthold"
)

var sess *sessions.CookieStore
var sess sessions.Store

func httpServer() {
gob.Register(FlashMessage{})
setupTmpl()
updateGlobalStats(nil)

sess = sessions.NewCookieStore(securecookie.GenerateRandomKey(32))
sess.MaxAge(86400)
if conf.SessionDir != "" {
sess = sessions.NewFilesystemStore(conf.SessionDir, securecookie.GenerateRandomKey(32))
} else {
sess = sessions.NewCookieStore(securecookie.GenerateRandomKey(32))
}

r := chi.NewRouter()

Expand All @@ -55,13 +58,20 @@ func httpServer() {
r.Post("/", addForm)
r.Post("/add", addAPI)

srv := &http.Server{
Addr: conf.HTTP,
Handler: gctx.ClearHandler(r),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}

if conf.TLS.Enable {
debug.Printf("initializing https server on %s", conf.HTTP)
debug.Fatal(http.ListenAndServeTLS(conf.HTTP, conf.TLS.Cert, conf.TLS.Key, gctx.ClearHandler(r)))
debug.Fatal(srv.ListenAndServeTLS(conf.TLS.Cert, conf.TLS.Key))
}

debug.Printf("initializing http server on %s", conf.HTTP)
debug.Fatal(http.ListenAndServe(conf.HTTP, gctx.ClearHandler(r)))
debug.Fatal(srv.ListenAndServe())
}

func mustJSON(input interface{}) []byte {
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ var (
)

type Config struct {
Site string `short:"s" long:"site-name" default:"https://links.ml" description:"site url, used for url generation"`
Quiet bool `short:"q" long:"quiet" description:"don't log to stdout"`
Debug bool `long:"debug" description:"enable debugging (pprof endpoints)"`
HTTP string `short:"b" long:"http" default:":8080" description:"ip:port pair to bind to"`
Proxy bool `short:"p" long:"behind-proxy" description:"if X-Forwarded-For headers should be trusted"`
TLS struct {
Site string `short:"s" long:"site-name" default:"https://links.ml" description:"site url, used for url generation"`
SessionDir string `long:"session-dir" description:"optional location to store temporary sessions"`
Quiet bool `short:"q" long:"quiet" description:"don't log to stdout"`
Debug bool `long:"debug" description:"enable debugging (pprof endpoints)"`
HTTP string `short:"b" long:"http" default:":8080" description:"ip:port pair to bind to"`
Proxy bool `short:"p" long:"behind-proxy" description:"if X-Forwarded-For headers should be trusted"`
TLS struct {
Enable bool `long:"enable" description:"run tls server rather than standard http"`
Cert string `short:"c" long:"cert" description:"path to ssl cert file"`
Key string `short:"k" long:"key" description:"path to ssl key file"`
Expand Down
14 changes: 8 additions & 6 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

rice "github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2"
gctx "github.com/gorilla/context"
"github.com/pressly/chi"
"github.com/sharpner/pobin"
)
Expand All @@ -30,6 +31,7 @@ func setupTmpl() {

func tmpl(w http.ResponseWriter, r *http.Request, path string, ctx map[string]interface{}) {
session, _ := sess.Get(r, defaultSessionID)
defer gctx.Clear(r)
messages := session.Flashes("messages")

// We have to save the session, otherwise the flashes aren't properly
Expand All @@ -45,19 +47,19 @@ func tmpl(w http.ResponseWriter, r *http.Request, path string, ctx map[string]in
ctx = make(map[string]interface{})
}

// cachedGlobalStats.mu.RLock()
// // Note that this copies a mutex, but it should never be re-locked, as
// // it's only being used in a template.
// stats := cachedGlobalStats
// cachedGlobalStats.mu.RUnlock()
cachedGlobalStats.mu.RLock()
// Note that this copies a mutex, but it should never be re-locked, as
// it's only being used in a template.
stats := cachedGlobalStats
cachedGlobalStats.mu.RUnlock()

ctx["full_url"] = r.URL.String()
ctx["url"] = r.URL
ctx["sess"] = session.Values
ctx["messages"] = messages
ctx["commit"] = commit
ctx["version"] = version
// ctx["stats"] = &stats
ctx["stats"] = &stats

out, err := tpl.ExecuteBytes(ctx)
if err != nil {
Expand Down

0 comments on commit 5f7c421

Please sign in to comment.