aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: e85649df10895d2b816a284aae5316b0d2d50df3 (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
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)
	}
}

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

	// setup routes
	setupRoutesAPI()
	setupRoutesWatch()
	setupHome()
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
	http.HandleFunc("/", rootHandler)

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