Use github.com/julienschmidt/httprouter instead of gorilla
This commit is contained in:
parent
0f9c3510cd
commit
5837e0e594
13 changed files with 223 additions and 169 deletions
|
|
@ -7,42 +7,39 @@ import (
|
|||
"strconv"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.Path("/themes/").Methods("GET").HandlerFunc(apiHandler(listThemes))
|
||||
router.Path("/themes/").Methods("POST").HandlerFunc(apiHandler(createTheme))
|
||||
router.Path("/themes.json").Methods("GET").HandlerFunc(apiHandler(exportThemes))
|
||||
router.Path("/themes/files-bindings").Methods("GET").HandlerFunc(apiHandler(bindingFiles))
|
||||
router.GET("/api/themes", apiHandler(listThemes))
|
||||
router.POST("/api/themes", apiHandler(createTheme))
|
||||
router.GET("/api/themes.json", apiHandler(exportThemes))
|
||||
router.GET("/api/files-bindings", apiHandler(bindingFiles))
|
||||
|
||||
rt := router.PathPrefix("/themes/{tid:[0-9]+}").Subrouter()
|
||||
rt.Path("/").Methods("GET").HandlerFunc(apiHandler(themeHandler(showTheme)))
|
||||
rt.Path("/").Methods("PUT").HandlerFunc(apiHandler(themeHandler(updateTheme)))
|
||||
rt.Path("/").Methods("DELETE").HandlerFunc(apiHandler(themeHandler(deleteTheme)))
|
||||
router.GET("/api/themes/:thid", apiHandler(themeHandler(showTheme)))
|
||||
router.PUT("/api/themes/:thid", apiHandler(themeHandler(updateTheme)))
|
||||
router.DELETE("/api/themes/:thid", apiHandler(themeHandler(deleteTheme)))
|
||||
|
||||
rtes := rt.PathPrefix("/exercices").Subrouter()
|
||||
rtes.Path("/").Methods("GET").HandlerFunc(apiHandler(themeHandler(listThemedExercices)))
|
||||
rtes.Path("/").Methods("POST").HandlerFunc(apiHandler(themeHandler(createExercice)))
|
||||
router.GET("/api/themes/:thid/exercices", apiHandler(themeHandler(listThemedExercices)))
|
||||
router.POST("/api/themes/:thid/exercices", apiHandler(themeHandler(createExercice)))
|
||||
|
||||
rte := rtes.PathPrefix("/{eid:[0-9]+}").Subrouter()
|
||||
rte.Path("/").Methods("GET").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(showThemedExercice))))
|
||||
rte.Path("/").Methods("PUT").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(updateThemedExercice))))
|
||||
rte.Path("/").Methods("DELETE").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(deleteThemedExercice))))
|
||||
router.GET("/api/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler(showThemedExercice)))
|
||||
router.PUT("/api/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler(updateThemedExercice)))
|
||||
router.DELETE("/api/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler(deleteThemedExercice)))
|
||||
|
||||
rtef := rte.Path("/files").Subrouter()
|
||||
rtef.Methods("GET").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(listThemedExerciceFiles))))
|
||||
rtef.Methods("POST").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(createExerciceFile))))
|
||||
|
||||
rteh := rte.Path("/hints").Subrouter()
|
||||
rteh.Methods("GET").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(listThemedExerciceHints))))
|
||||
rteh.Methods("POST").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(createExerciceHint))))
|
||||
router.GET("/api/themes/:thid/exercices/:eid/files", apiHandler(themedExerciceHandler(listThemedExerciceFiles)))
|
||||
router.POST("/api/themes/:thid/exercices/:eid/files", apiHandler(themedExerciceHandler(createExerciceFile)))
|
||||
|
||||
rtek := rte.Path("/keys").Subrouter()
|
||||
rtek.Methods("GET").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(listThemedExerciceKeys))))
|
||||
rtek.Methods("POST").HandlerFunc(apiHandler(themeHandler(themedExerciceHandler(createExerciceKey))))
|
||||
router.GET("/api/themes/:thid/exercices/:eid/hints", apiHandler(themedExerciceHandler(listThemedExerciceHints)))
|
||||
router.POST("/api/themes/:thid/exercices/:eid/hints", apiHandler(themedExerciceHandler(createExerciceHint)))
|
||||
|
||||
router.GET("/api/themes/:thid/exercices/:eid/keys", apiHandler(themedExerciceHandler(listThemedExerciceKeys)))
|
||||
router.POST("/api/themes/:thid/exercices/:eid/keys", apiHandler(themedExerciceHandler(createExerciceKey)))
|
||||
}
|
||||
|
||||
func bindingFiles(args map[string]string, body []byte) (interface{}, error) {
|
||||
func bindingFiles(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
if files, err := fic.GetFiles(); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
|
|
@ -66,35 +63,35 @@ func getExercice(args []string) (fic.Exercice, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func listThemes(args map[string]string, body []byte) (interface{}, error) {
|
||||
func listThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return fic.GetThemes()
|
||||
}
|
||||
|
||||
func exportThemes(args map[string]string, body []byte) (interface{}, error) {
|
||||
func exportThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return fic.ExportThemes()
|
||||
}
|
||||
|
||||
func showTheme(theme fic.Theme, args map[string]string, body []byte) (interface{}, error) {
|
||||
func showTheme(theme fic.Theme, _ []byte) (interface{}, error) {
|
||||
return theme, nil
|
||||
}
|
||||
|
||||
func listThemedExercices(theme fic.Theme, args map[string]string, body []byte) (interface{}, error) {
|
||||
func listThemedExercices(theme fic.Theme, _ []byte) (interface{}, error) {
|
||||
return theme.GetExercices()
|
||||
}
|
||||
|
||||
func showThemedExercice(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
|
||||
func showThemedExercice(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
|
||||
return exercice, nil
|
||||
}
|
||||
|
||||
func listThemedExerciceFiles(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
|
||||
func listThemedExerciceFiles(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
|
||||
return exercice.GetFiles()
|
||||
}
|
||||
|
||||
func listThemedExerciceHints(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
|
||||
func listThemedExerciceHints(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
|
||||
return exercice.GetHints()
|
||||
}
|
||||
|
||||
func listThemedExerciceKeys(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
|
||||
func listThemedExerciceKeys(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
|
||||
return exercice.GetKeys()
|
||||
}
|
||||
|
||||
|
|
@ -104,7 +101,7 @@ type uploadedTheme struct {
|
|||
Authors string
|
||||
}
|
||||
|
||||
func createTheme(args map[string]string, body []byte) (interface{}, error) {
|
||||
func createTheme(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ut uploadedTheme
|
||||
if err := json.Unmarshal(body, &ut); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -117,7 +114,7 @@ func createTheme(args map[string]string, body []byte) (interface{}, error) {
|
|||
return fic.CreateTheme(ut.Name, ut.Authors)
|
||||
}
|
||||
|
||||
func updateTheme(theme fic.Theme, args map[string]string, body []byte) (interface{}, error) {
|
||||
func updateTheme(theme fic.Theme, body []byte) (interface{}, error) {
|
||||
var ut fic.Theme
|
||||
if err := json.Unmarshal(body, &ut); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -132,7 +129,7 @@ func updateTheme(theme fic.Theme, args map[string]string, body []byte) (interfac
|
|||
return ut.Update()
|
||||
}
|
||||
|
||||
func updateThemedExercice(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
|
||||
func updateThemedExercice(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
|
||||
// Update an exercice
|
||||
var ue fic.Exercice
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
|
|
@ -152,10 +149,10 @@ func updateThemedExercice(theme fic.Theme, exercice fic.Exercice, args map[strin
|
|||
return ue, nil
|
||||
}
|
||||
|
||||
func deleteTheme(theme fic.Theme, args map[string]string, body []byte) (interface{}, error) {
|
||||
func deleteTheme(theme fic.Theme, _ []byte) (interface{}, error) {
|
||||
return theme.Delete()
|
||||
}
|
||||
|
||||
func deleteThemedExercice(theme fic.Theme, exercice fic.Exercice, args map[string]string, body []byte) (interface{}, error) {
|
||||
func deleteThemedExercice(_ fic.Theme, exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||
return exercice.Delete()
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue