-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapp.go
69 lines (60 loc) · 1.34 KB
/
webapp.go
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
63
64
65
66
67
68
69
package main
import (
"fmt"
"log"
"io/ioutil"
"net/http"
"os"
)
type Page struct {
Title string
Body []byte
}
func loadPage(title string) *Page {
filename := "static/" + title + ".html"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil
}
return &Page{Title: title, Body: body}
}
func router(request string) (*Page) {
switch request {
case "indulge":
return loadPage("indulge")
case "playr":
return loadPage("playr")
case "123_6":
return loadPage("123_6")
case "eatapp":
return loadPage("eatapp")
case "gen_tree":
return loadPage("gen_tree")
case "":
return loadPage("index")
default:
return nil
}
}
func handler(writer http.ResponseWriter, request *http.Request) {
page := router(request.URL.Path[1:])
if page != nil {
writer.Write(page.Body)
fmt.Println(request.URL.Path[1:]) // debug
}
}
func main() {
port := os.Getenv("PORT")
fs := http.FileServer(http.Dir("static"))
if len(port) == 0 {
fmt.Println("Global enviroment variable $PORT is empty. Using default port 80. ")
port = "80"
}
http.Handle("/", fs)
http.HandleFunc("/indulge", handler)
http.HandleFunc("/playr", handler)
http.HandleFunc("/123_6", handler)
http.HandleFunc("/eatapp", handler)
http.HandleFunc("/gen_tree", handler)
log.Fatal(http.ListenAndServe(":" + port, nil))
}