aboutsummaryrefslogtreecommitdiff
path: root/cmd/server/main.go
blob: b2a95c30e75bb8696dc4dd1332d387298dea5894 (plain) (blame)
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
package main

import (
	"flag"
	"fmt"
	"gopkg.in/yaml.v3"
	"log"
	"net/http"
	"os"
	"time"

	"riedstra.dev/mitch/go-website/page"
)

var VersionString = ""

func VersionPrint() {
	fmt.Println(VersionString)
	os.Exit(0)
}

func loadConf(fn string) (*App, error) {
	fh, err := os.Open(fn)
	if err != nil {
		return nil, err
	}
	dec := yaml.NewDecoder(fh)

	app := &App{}
	err = dec.Decode(app)
	return app, err
}

func main() {
	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")
	fl.StringVar(&page.TimeFormat, "T", page.TimeFormat, "Print the version then exit")
	indexPath := fl.String("i", "/reIndex",
		"Path in which, when called will rebuild the index and clear the cache")
	_ = fl.Parse(os.Args[1:])

	if *version {
		VersionPrint()
	}

	if err := os.Chdir(*directory); err != nil {
		log.Fatal(err)
	}

	app, err := loadConf("conf.yml")
	if err != nil {
		log.Println(err)
		app = &App{}
	}

	app.ReIndexPath = *indexPath
	app.StaticDirectory = "static"

	b, _ := yaml.Marshal(app)
	os.Stderr.Write(b)

	srv := &http.Server{
		Handler:      app,
		Addr:         *listen,
		WriteTimeout: 15 * time.Second,
		ReadTimeout:  15 * time.Second,
	}
	log.Fatal(srv.ListenAndServe())
}