Use github.com/julienschmidt/httprouter instead of gorilla

This commit is contained in:
nemunaire 2016-12-16 00:51:56 +01:00
parent 5a0b81ba32
commit 3b320469b5
13 changed files with 226 additions and 172 deletions

View file

@ -6,27 +6,29 @@ import (
"strconv"
"srs.epita.fr/fic-server/libfic"
"github.com/julienschmidt/httprouter"
)
func init() {
router.Path("/exercices/").Methods("GET").HandlerFunc(apiHandler(listExercices))
re := router.Path("/exercices/{eid:[0-9]+}").Subrouter()
re.Methods("GET").HandlerFunc(apiHandler(exerciceHandler(showExercice)))
re.Methods("PUT").HandlerFunc(apiHandler(updateExercice))
re.Methods("DELETE").HandlerFunc(apiHandler(deleteExercice))
router.GET("/api/exercices/", apiHandler(listExercices))
router.GET("/api/exercices/:eid", apiHandler(exerciceHandler(showExercice)))
router.PUT("/api/exercices/:eid", apiHandler(updateExercice))
router.DELETE("/api/exercices/:eid", apiHandler(deleteExercice))
}
func listExercices(args map[string]string, body []byte) (interface{}, error) {
func listExercices(_ httprouter.Params, body []byte) (interface{}, error) {
// List all exercices
return fic.GetExercices()
}
func showExercice(exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
func showExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice, nil
}
func deleteExercice(args map[string]string, body []byte) (interface{}, error) {
if eid, err := strconv.Atoi(args["eid"]); err != nil {
func deleteExercice(ps httprouter.Params, body []byte) (interface{}, error) {
if eid, err := strconv.Atoi(ps.ByName("eid")); err != nil {
return nil, err
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
return nil, err
@ -43,8 +45,8 @@ type uploadedExercice struct {
VideoURI string
}
func updateExercice(args map[string]string, body []byte) (interface{}, error) {
if eid, err := strconv.Atoi(args["eid"]); err != nil {
func updateExercice(ps httprouter.Params, body []byte) (interface{}, error) {
if eid, err := strconv.Atoi(ps.ByName("eid")); err != nil {
return nil, err
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
return nil, err
@ -75,7 +77,7 @@ func updateExercice(args map[string]string, body []byte) (interface{}, error) {
}
}
func createExercice(theme fic.Theme, args map[string]string, body []byte) (interface{}, error) {
func createExercice(theme fic.Theme, body []byte) (interface{}, error) {
// Create a new exercice
var ue uploadedExercice
if err := json.Unmarshal(body, &ue); err != nil {
@ -103,7 +105,7 @@ type uploadedKey struct {
Key string
}
func createExerciceKey(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
func createExerciceKey(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
var uk uploadedKey
if err := json.Unmarshal(body, &uk); err != nil {
return nil, err
@ -122,7 +124,7 @@ type uploadedHint struct {
Cost int64
}
func createExerciceHint(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
func createExerciceHint(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
var uh uploadedHint
if err := json.Unmarshal(body, &uh); err != nil {
return nil, err