From 268fcf7e6b671d4959a12111d5abf553bf0a201b Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sun, 24 Oct 2021 12:52:38 -0400 Subject: Redis caching. Linter config and cleanup. --- cmd/server/main.go | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'cmd/server/main.go') diff --git a/cmd/server/main.go b/cmd/server/main.go index 00aecbf..d76f938 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,12 +3,13 @@ package main import ( "flag" "fmt" - "gopkg.in/yaml.v3" "log" "net/http" "os" "time" + "github.com/gomodule/redigo/redis" + "gopkg.in/yaml.v3" "riedstra.dev/mitch/go-website/page" ) @@ -19,17 +20,28 @@ func VersionPrint() { os.Exit(0) } -func main() { +func main() { //nolint:funlen fl := flag.NewFlagSet("Website", flag.ExitOnError) listen := fl.String("l", "0.0.0.0:8001", "Listening address") directory := fl.String("d", ".", "Directory to serve.") version := fl.Bool("v", false, "Print the version then exit") confFn := fl.String("c", "conf.yml", "Location for the config file") verbose := fl.Bool("V", false, "Be more verbose ( dump config, etc ) ") - fl.StringVar(&page.TimeFormat, "T", page.TimeFormat, "Set the page time format, be careful with this") + fl.StringVar(&page.TimeFormat, "T", page.TimeFormat, + "Set the page time format, be careful with this") + defaultIndexPath := "/reIndex" + indexPath := fl.String("i", defaultIndexPath, "Path in which, when called will rebuild the index and clear the cache") + redisAddr := fl.String("r", "127.0.0.1:6379", + "Redis server set to \"\" to disable") + + pageTimeout := fl.Int("timeout", 15, "Seconds until page timeout for read and write") + + fl.BoolVar(&page.CacheIndex, "cache-index", true, + "If set to false do not cache index") + _ = fl.Parse(os.Args[1:]) if *version { @@ -43,6 +55,7 @@ func main() { app, err := loadConf(*confFn) if err != nil { log.Println(err) + app = &App{} } @@ -50,6 +63,21 @@ func main() { app.ReIndexPath = *indexPath } + if *redisAddr != "" { + app.redisPool = &redis.Pool{ + MaxIdle: 80, //nolint:gomnd + MaxActive: 12000, //nolint:gomnd + Dial: func() (redis.Conn, error) { + c, err := redis.Dial("tcp", *redisAddr) + if err != nil { + log.Println("Redis dial error: ", err) + } + + return c, err //nolint + }, + } + } + if *verbose { b, _ := yaml.Marshal(app) os.Stderr.Write(b) @@ -58,8 +86,8 @@ func main() { srv := &http.Server{ Handler: app, Addr: *listen, - WriteTimeout: 15 * time.Second, - ReadTimeout: 15 * time.Second, + WriteTimeout: time.Duration(*pageTimeout) * time.Second, + ReadTimeout: time.Duration(*pageTimeout) * time.Second, } log.Fatal(srv.ListenAndServe()) } -- cgit v1.2.3