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()
}