aboutsummaryrefslogtreecommitdiff
path: root/cmd/server/main.go
blob: 9ec96dafbd62a3626ae6631c3168837dc9cb1015 (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
package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

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

var VersionString = ""

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

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 := &App{
		ReIndexPath:     *indexPath,
		StaticDirectory: "static",
	}

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