Use github.com/julienschmidt/httprouter instead of gorilla
This commit is contained in:
parent
0d6e36798c
commit
1bb978a9c6
13 changed files with 226 additions and 172 deletions
|
@ -10,14 +10,14 @@ import (
|
|||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
|
||||
type DispatchFunction func([]string, []byte) (interface{}, error)
|
||||
type DispatchFunction func(httprouter.Params, []byte) (interface{}, error)
|
||||
|
||||
func apiHandler(f func (map[string]string,[]byte) (interface{}, error)) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
func apiHandler(f DispatchFunction) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
log.Printf("Handling %s request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
@ -44,7 +44,7 @@ func apiHandler(f func (map[string]string,[]byte) (interface{}, error)) func(htt
|
|||
}
|
||||
}
|
||||
|
||||
ret, err = f(mux.Vars(r), body)
|
||||
ret, err = f(ps, body)
|
||||
|
||||
// Format response
|
||||
resStatus := http.StatusOK
|
||||
|
@ -74,58 +74,83 @@ func apiHandler(f func (map[string]string,[]byte) (interface{}, error)) func(htt
|
|||
}
|
||||
}
|
||||
|
||||
func teamHandler(f func(fic.Team,map[string]string,[]byte) (interface{}, error)) func (map[string]string,[]byte) (interface{}, error) {
|
||||
return func (args map[string]string, body []byte) (interface{}, error) {
|
||||
if tid, err := strconv.Atoi(string(args["tid"])); err != nil {
|
||||
if team, err := fic.GetTeamByInitialName(args["tid"]); err != nil {
|
||||
func teamPublicHandler(f func(*fic.Team,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) {
|
||||
return func (ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if tid, err := strconv.Atoi(string(ps.ByName("tid"))); err != nil {
|
||||
if team, err := fic.GetTeamByInitialName(ps.ByName("tid")); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(team, args, body)
|
||||
return f(&team, body)
|
||||
}
|
||||
} else if tid == 0 {
|
||||
return f(nil, body)
|
||||
} else if team, err := fic.GetTeam(tid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(&team, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func teamHandler(f func(fic.Team,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) {
|
||||
return func (ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if tid, err := strconv.Atoi(string(ps.ByName("tid"))); err != nil {
|
||||
if team, err := fic.GetTeamByInitialName(ps.ByName("tid")); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(team, body)
|
||||
}
|
||||
} else if team, err := fic.GetTeam(tid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(team, args, body)
|
||||
return f(team, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func themeHandler(f func(fic.Theme,map[string]string,[]byte) (interface{}, error)) func (map[string]string,[]byte) (interface{}, error) {
|
||||
return func (args map[string]string, body []byte) (interface{}, error) {
|
||||
if tid, err := strconv.Atoi(string(args["tid"])); err != nil {
|
||||
func themeHandler(f func(fic.Theme,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) {
|
||||
return func (ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if thid, err := strconv.Atoi(string(ps.ByName("thid"))); err != nil {
|
||||
return nil, err
|
||||
} else if theme, err := fic.GetTheme(tid); err != nil {
|
||||
} else if theme, err := fic.GetTheme(thid); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(theme, args, body)
|
||||
return f(theme, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func exerciceHandler(f func(fic.Exercice,map[string]string,[]byte) (interface{}, error)) func (map[string]string,[]byte) (interface{}, error) {
|
||||
return func (args map[string]string, body []byte) (interface{}, error) {
|
||||
if eid, err := strconv.Atoi(string(args["eid"])); err != nil {
|
||||
func exerciceHandler(f func(fic.Exercice,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) {
|
||||
return func (ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if eid, err := strconv.Atoi(string(ps.ByName("eid"))); err != nil {
|
||||
return nil, err
|
||||
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(exercice, args, body)
|
||||
return f(exercice, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func themedExerciceHandler(f func(fic.Theme,fic.Exercice,map[string]string,[]byte) (interface{}, error)) func (fic.Theme,map[string]string,[]byte) (interface{}, error) {
|
||||
return func (theme fic.Theme, args map[string]string, body []byte) (interface{}, error) {
|
||||
if eid, err := strconv.Atoi(string(args["eid"])); err != nil {
|
||||
return nil, err
|
||||
} else if exercice, err := fic.GetExercice(int64(eid)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return f(theme, exercice, args, body)
|
||||
}
|
||||
func themedExerciceHandler(f func(fic.Theme,fic.Exercice,[]byte) (interface{}, error)) func (httprouter.Params,[]byte) (interface{}, error) {
|
||||
return func (ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
var theme fic.Theme
|
||||
var exercice fic.Exercice
|
||||
|
||||
themeHandler(func (th fic.Theme, _[]byte) (interface{}, error) {
|
||||
theme = th
|
||||
return nil,nil
|
||||
})(ps, body)
|
||||
|
||||
exerciceHandler(func (ex fic.Exercice, _[]byte) (interface{}, error) {
|
||||
exercice = ex
|
||||
return nil,nil
|
||||
})(ps, body)
|
||||
|
||||
return f(theme, exercice, body)
|
||||
}
|
||||
}
|
||||
|
||||
func notFound(args map[string]string, body []byte) (interface{}, error) {
|
||||
func notFound(ps httprouter.Params, _ []byte) (interface{}, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
Reference in a new issue