1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"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["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())
}
|