This repository was archived by the owner on Oct 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.go
More file actions
54 lines (39 loc) · 1.36 KB
/
routes.go
File metadata and controls
54 lines (39 loc) · 1.36 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
53
54
package server
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type param string
const (
// alphanum20 is a regular expression matching an sequence of
// 1 to 20 alphanumeric characters.
alphanum20 = "[a-zA-Z0-9]{1,20}"
// idParam is the parameter name for the path variable to identify
// one resource of a collection of resources, typically in RESTful
// APIs: "api/resources/{idParam}".
idParam param = "id"
)
// pathParam returns the value of the path parameter p from the request
// context. Returns an error if the parameter is not found in the context.
func pathParam(r *http.Request, p param) (string, error) {
v, ok := mux.Vars(r)[string(p)]
if !ok {
return "", fmt.Errorf("missing path parameter \"%s\"", p)
}
return v, nil
}
func (s *Server) registerRoutes() {
idPathVar := fmt.Sprintf("{%s:%s}", idParam, alphanum20)
s.router.HandleFunc("/", handleRoot)
v1 := s.router.PathPrefix("/v1").Subrouter()
v1.HandleFunc("/reports", s.createReport).Methods("POST")
v1.HandleFunc("/reports/"+idPathVar, s.retrieveReport).Methods("GET")
v1.HandleFunc("/stats", s.retrieveAllStats).Methods("GET")
v1.HandleFunc("/stats/"+idPathVar, s.retrieveStatsByID).Methods("GET")
}
func handleRoot(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
rw.WriteHeader(200)
rw.Write([]byte("⚡")) //nolint
}