-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
50 lines (39 loc) · 917 Bytes
/
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
package main
import (
//"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
// Register static files path
fs := http.FileServer(http.Dir("static/"))
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
// Register router
http.Handle("/", router)
// Register Handlers
router.HandleFunc("/", indexHandler)
router.HandleFunc("/about", aboutHandler)
// Start listening
listen()
}
func listen() {
log.Printf("Listening on %s%s...\n", HOST, PORT)
err := http.ListenAndServe(PORT, nil)
if err != nil {
log.Fatal(err)
}
}
// Handlers
func indexHandler(w http.ResponseWriter, r *http.Request) {
p := "." + r.URL.Path
if p == "./" { /*root directory*/
p = buildHtml(r.URL.Path + "index")
}
http.ServeFile(w, r, p)
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
p := buildHtml(r.URL.Path)
http.ServeFile(w, r, p)
}