forked from goh-chunlin/GoToAzure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
60 lines (48 loc) · 1.37 KB
/
app.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
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
)
func main() {
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("static/css"))))
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("static/img"))))
http.Handle("/fonts/", http.StripPrefix("/fonts/", http.FileServer(http.Dir("static/fonts"))))
http.HandleFunc("/", home)
http.HandleFunc("/api", api)
http.ListenAndServe(getPort(), nil)
}
func getPort() string {
p := os.Getenv("HTTP_PLATFORM_PORT")
if p != "" {
return ":" + p
}
return ":80"
}
func render(response http.ResponseWriter, templateFileName string) {
templateFileName = fmt.Sprintf("static/templates/%s", templateFileName)
template, err := template.ParseFiles(templateFileName)
if err != nil {
log.Print("template parsing error: ", err)
}
err = template.Execute(response, nil)
if err != nil {
log.Print("template executing error: ", err)
}
}
func home(response http.ResponseWriter, request *http.Request) {
render(response, "index.html")
}
func api(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
apiStatus := modelAPIStatus{
Status: true,
Message: "The API method is called successfully.",
}
output, _ := json.MarshalIndent(&apiStatus, "", "\t")
response.WriteHeader(200)
response.Write(output)
}