/
web.go
36 lines (28 loc) · 940 Bytes
/
web.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
package web
import (
"fmt"
"github.com/gorilla/mux"
"html"
"log"
"net/http"
)
func index(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/index.html", 302)
}
func someApi(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Some Api!! %q", html.EscapeString(r.URL.Path))
}
// Start Up the HttpServer
func Start(port int) {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", index)
// all static goes to /static/ files
// NOTE: this expects that the program working directory is github.com/trasa/watchmud
// otherwise, all requests to /static will result in 404!
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./web/static/"))))
// TODO write an api...
router.HandleFunc("/api/v1", someApi)
log.Printf("http listening on port %d", port)
http.Handle("/", router)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}