-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
92 lines (74 loc) · 3.08 KB
/
api.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package api
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
render "gopkg.in/unrolled/render.v1"
"github.com/chadweimer/gomp/models"
"github.com/chadweimer/gomp/modules/conf"
"github.com/julienschmidt/httprouter"
)
// ---- Begin Standard Errors ----
var errMismatchedNoteID = errors.New("The note id in the path does not match the one specified in the request body")
var errMismatchedRecipeID = errors.New("The recipe id in the path does not match the one specified in the request body")
// ---- End Standard Errors ----
type apiHandler struct {
*render.Render
cfg *conf.Config
model *models.Model
apiMux *httprouter.Router
}
// NewHandler returns a new instance of http.Handler
func NewHandler(renderer *render.Render, cfg *conf.Config, model *models.Model) http.Handler {
h := apiHandler{
Render: renderer,
cfg: cfg,
model: model,
}
h.apiMux = httprouter.New()
h.apiMux.POST("/api/v1/auth", h.postAuthenticate)
h.apiMux.GET("/api/v1/recipes", h.requireAuthentication(h.getRecipes))
h.apiMux.POST("/api/v1/recipes", h.requireAuthentication(h.postRecipe))
h.apiMux.GET("/api/v1/recipes/:recipeID", h.requireAuthentication(h.getRecipe))
h.apiMux.PUT("/api/v1/recipes/:recipeID", h.requireAuthentication(h.putRecipe))
h.apiMux.DELETE("/api/v1/recipes/:recipeID", h.requireAuthentication(h.deleteRecipe))
h.apiMux.PUT("/api/v1/recipes/:recipeID/rating", h.requireAuthentication(h.putRecipeRating))
h.apiMux.GET("/api/v1/recipes/:recipeID/image", h.requireAuthentication(h.getRecipeMainImage))
h.apiMux.PUT("/api/v1/recipes/:recipeID/image", h.requireAuthentication(h.putRecipeMainImage))
h.apiMux.GET("/api/v1/recipes/:recipeID/images", h.requireAuthentication(h.getRecipeImages))
h.apiMux.POST("/api/v1/recipes/:recipeID/images", h.requireAuthentication(h.postImage))
h.apiMux.GET("/api/v1/recipes/:recipeID/notes", h.requireAuthentication(h.getRecipeNotes))
h.apiMux.DELETE("/api/v1/images/:imageID", h.requireAuthentication(h.deleteImage))
h.apiMux.POST("/api/v1/notes", h.requireAuthentication(h.postNote))
h.apiMux.PUT("/api/v1/notes/:noteID", h.requireAuthentication(h.putNote))
h.apiMux.DELETE("/api/v1/notes/:noteID", h.requireAuthentication(h.deleteNote))
h.apiMux.GET("/api/v1/tags", h.requireAuthentication(h.getTags))
h.apiMux.NotFound = http.HandlerFunc(h.notFound)
return &h
}
func (h apiHandler) notFound(resp http.ResponseWriter, req *http.Request) {
h.JSON(resp, http.StatusNotFound, fmt.Sprintf("%s is not a valid API endpoint", req.URL.Path))
}
func (h apiHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("Content-Type", "application/json")
h.apiMux.ServeHTTP(resp, req)
}
func getParam(values url.Values, key string) string {
val, _ := url.QueryUnescape(values.Get(key))
return val
}
func getParams(values url.Values, key string) []string {
var vals []string
var ok bool
if vals, ok = values[key]; ok {
for i, val := range vals {
vals[i], _ = url.QueryUnescape(val)
}
}
return vals
}
func readJSONFromRequest(req *http.Request, data interface{}) error {
return json.NewDecoder(req.Body).Decode(data)
}