-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (51 loc) · 1.24 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
)
var addr = flag.String("addr", ":8080", "http service address")
func apidocHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
log.Println(r.URL.Path)
p := page{
Title: "API",
Template: "public/templates/apidoc.html",
}
p.renderTemplate(w)
} else {
http.NotFound(w, r)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Path[1:]) == 0 && r.Method == "GET" {
log.Println(r.URL.Path)
p := page{
Title: "What do you want me to say?",
Template: "public/templates/index.html",
Lang: "sv",
}
p.renderTemplate(w)
} else {
http.NotFound(w, r)
}
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path[1:])
http.ServeFile(w, r, "public/"+r.URL.Path[1:])
}
func main() {
flag.Parse()
go h.run()
http.HandleFunc("/", indexHandler)
http.HandleFunc("/static/", staticHandler)
http.HandleFunc("/api/v1/docs", apidocHandler)
http.HandleFunc("/api/v1/speak", speakHandler)
http.HandleFunc("/ws", wsHandler)
message := fmt.Sprintf("Starting server on %v", *addr)
log.Println(message)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}