aboutsummaryrefslogtreecommitdiff
path: root/takeoff.go
blob: 7f7dd09af3388cdd13cd94b49d685adfd41b59a9 (plain)
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
package main

import (
	"flag"
	"html/template"
	"log"
	"net/http"
	"path"
)

var (
	addr = flag.String("a", "localhost:8888", "listen address")
	cdir = flag.String("c", "config", "path to configuration directory")
	sdir = flag.String("s", "static", "path to static files directory")
	tdir = flag.String("t", "templates", "path to templates directory")
)

var (
	instances *lineListConfig
	langs *langListConfig
)

var homeTemplate string

var tData struct {
	Instances []string
	Langs     []Lang
}

func home(w http.ResponseWriter, r *http.Request) {
	tData.Instances = instances.getList()
	tData.Langs = langs.getList()
	t := template.Must(template.ParseFiles(homeTemplate))
	t.Execute(w, &tData)
}

func serve() {
	files := http.FileServer(http.Dir(*sdir))
	http.HandleFunc("/", home)
	http.Handle("/static/", http.StripPrefix("/static/", files))
	log.Fatal(http.ListenAndServe(*addr, nil))
}

func main() {
	var err error

	flag.Parse()

	startConfigWatcher(*cdir)
	defer stopConfigWatcher()

	instances, err = newLineListConfig("instances")
	langs, err = newLangListConfig("langs")
	if err != nil {
		log.Fatal("FATAL: config list error: ", err)
	}

	homeTemplate = path.Join(*tdir, "index.html")

	log.Printf("takeoff v0 listening at http://%s\n", *addr)
	serve()
}