~reesmichael1/chantpointer

ref: e8f306b2bbd69105afce04178a12356fcae8bc14 chantpointer/main.go -rw-r--r-- 959 bytes View raw
e8f306b2Michael Rees Update URL of personal website 7 months ago
                                                                                
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
package main

import (
	"flag"
	"fmt"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"
)

var noCache bool
var templates *template.Template
var templateFiles []string

func main() {
	var port int
	flag.IntVar(&port, "port", 7777, "the port that the server will run on")
	flag.BoolVar(&noCache, "no-cache", false, "if given, don't cache templates")
	flag.Parse()

	if !noCache {
		files, err := ioutil.ReadDir("templates/html")
		if err != nil {
			log.Fatal(err)
		}

		for _, f := range files {
			templateFiles = append(templateFiles, "templates/html/"+f.Name())
		}
		templates = template.Must(template.ParseFiles(templateFiles...))
	}

	static := http.FileServer(http.Dir("static"))
	http.Handle("/static/", http.StripPrefix("/static", static))
	http.HandleFunc("/chant/", ChantHandler)
	http.Handle("/generate", ErrorHandler(GenerateHandler))
	http.Handle("/", ErrorHandler(IndexHandler))
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}