Repository navigation
Expand file tree
/
Copy pathserver.go
More file actions
52 lines (41 loc) · 1.42 KB
/
Copy pathserver.go
File metadata and controls
52 lines (41 loc) · 1.42 KB
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
package main
import (
"github.com/gorilla/mux"
"net/http"
"strings"
)
func startServer(port string) {
router := mux.NewRouter()
api := router.PathPrefix("/api/").Subrouter()
api.Path("/add").HandlerFunc(HandleAddArticle).Methods("POST")
api.Path("/clear").HandlerFunc(HandleClearArticles).Methods("POST")
api.Path("/frontpage").HandlerFunc(HandleGetFrontpage).Methods("GET")
api.Path("/article/{title}").HandlerFunc(HandleGetArticle).Methods("GET")
// otherwise go to the file server
router.PathPrefix("/{filename}").HandlerFunc(HandleServeFile).Methods("GET")
http.ListenAndServe(port, router)
}
// HandleServeFile takes care of appending html onto certain articles
func HandleServeFile(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
filename, ok := vars["filename"]
if !ok {
w.Write(asbytes("oh nose! bad bad bad!"))
return
}
path := "./src/github.com/opinionated/debugServer/debugFrontEnd/"
if strings.Contains(filename, ".") {
http.ServeFile(w, r, path+filename)
} else {
http.ServeFile(w, r, path+filename+".html")
}
}
// returns the handler for the api
func apiHandler() http.Handler {
router := mux.NewRouter()
router.HandleFunc("/add", HandleAddArticle).Methods("POST")
router.HandleFunc("/clear", HandleClearArticles).Methods("POST")
router.HandleFunc("/frontpage", HandleGetFrontpage).Methods("GET")
router.HandleFunc("/article/{title}", HandleGetArticle).Methods("GET")
return router
}