package main import ( "flag" "fmt" "log" "net/http" "os" "strings" "text/template" "time" "github.com/gomodule/redigo/redis" "gopkg.in/yaml.v3" "riedstra.dev/mitch/go-website/page" ) var VersionString = "" func VersionPrint() { fmt.Println(VersionString) os.Exit(0) } 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") authConfFn := fl.String("ac", "auth.json", "Location for authorization configuration 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") 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") redisKey := fl.String("rk", "go-website", "Redis key to use for storing cached pages") 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 { VersionPrint() } if err := os.Chdir(*directory); err != nil { log.Fatal(err) } app, err := loadConf(*confFn) if err != nil { log.Println(err) app = &App{} } err = app.ReadAuth(*authConfFn) if err != nil { log.Println(err) } if app.ReIndexPath == "" || *indexPath != defaultIndexPath { app.ReIndexPath = *indexPath } if app.RedisKey == "" { app.RedisKey = *redisKey } 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 strings.HasPrefix(*redisAddr, "unix:") { c, err = redis.Dial("unix", strings.TrimPrefix(*redisAddr, "unix:")) } if err != nil { log.Println("Redis dial error: ", err) } return c, err //nolint }, } } if *verbose { b, _ := yaml.Marshal(app) os.Stderr.Write(b) } page.Funcs = template.FuncMap{ "ClearRedis": app.ClearRedis, } srv := &http.Server{ Handler: app, Addr: *listen, WriteTimeout: time.Duration(*pageTimeout) * time.Second, ReadTimeout: time.Duration(*pageTimeout) * time.Second, } log.Fatal(srv.ListenAndServe()) }