/
main.go
91 lines (81 loc) · 2.06 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"log"
"net/http"
"os"
"html/template"
"broker/app"
//for extracting service credentials from VCAP_SERVICES
//"github.com/cloudfoundry-community/go-cfenv"
)
const (
DEFAULT_PORT = "8080"
DEFAULT_HOST = "localhost"
)
var index = template.Must(template.ParseFiles(
"templates/_base.html",
"templates/index.html",
))
func helloworld(w http.ResponseWriter, req *http.Request) {
index.Execute(w, nil)
}
// /user/new
func userNewHandler(w http.ResponseWriter, req *http.Request) {
log.Println("newUser")
template.Must(template.ParseFiles(
"templates/_base.html",
"templates/user/new.html",
)).Execute(w, nil)
}
// /user
func userEntityHandler(w http.ResponseWriter, req *http.Request) {
log.Println("handle")
switch req.Method {
case "POST":
userName := req.FormValue("userName")
password := req.FormValue("password")
app.NewUser(userName, password)
template.Must(template.ParseFiles(
"templates/_base.html",
"templates/user/index.html",
)).Execute(w, app.GetUserList())
case "GET":
template.Must(template.ParseFiles(
"templates/_base.html",
"templates/user/index.html",
)).Execute(w, app.GetUserList())
case "PUT":
case "DELETE":
}
}
// /user/*
func userItemHandler(w http.ResponseWriter, req *http.Request) {
log.Println("handle")
switch req.Method {
case "POST":
case "GET":
template.Must(template.ParseFiles(
"templates/_base.html",
"templates/user/index.html",
)).Execute(w, app.GetUserList())
case "PUT":
case "DELETE":
}
}
func main() {
var port string
if port = os.Getenv("VCAP_APP_PORT"); len(port) == 0 {
port = DEFAULT_PORT
}
var host string
if host = os.Getenv("VCAP_APP_HOST"); len(host) == 0 {
host = DEFAULT_HOST
}
http.HandleFunc("/", helloworld)
http.HandleFunc("/user", userEntityHandler)
http.HandleFunc("/user/new", userNewHandler)
http.HandleFunc("/user/", userItemHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
log.Printf("Starting app on %+v:%+v\n", host, port)
http.ListenAndServe(host+":"+port, nil)
}