aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: ff1349972ab49a66aaeeca92702b5238105dc39f (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
package main

import (
	"log"
	"net/http"
	"os"
	"strings"
	"github.com/joho/godotenv"
)

var debug = false
var apiKey = ""

/* root handler */
func rootHandler(w http.ResponseWriter, r *http.Request) {
	path := strings.Trim(r.URL.Path, "/")
	if path == "" {
		//http.ServeFile(w, r, "static/index.html")
		renderIndexTemplate(w)
	} else {
		renderWatchTemplate(w, path, r.URL.RawQuery) // FIXME just redirect this one to /watch for simplicity
	}
}

/* main */
func main() {
	// load .env file if it exists
	godotenv.Load()
	debug = os.Getenv("DEBUG") != ""
	apiKey = os.Getenv("API_KEY")

	// setup routes
	mux := http.NewServeMux()
	registerRoutesAPI(mux, &APISourceOfficial{})
	registerRoutesWatch(mux)
	setupHome()
	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	mux.HandleFunc("/", rootHandler)

	// start http server
	log.Println("Listening on http://localhost:8080")
	err := http.ListenAndServe(":8080", mux)
	if err != nil {
		log.Fatal(err)
	}
}