Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

47 lines
1020B

  1. package main
  2. import (
  3. "biukop/sfm/loan"
  4. "net/http"
  5. "net/http/httputil"
  6. )
  7. const apiV1Prefix = "/api/v1/"
  8. type apiV1HandlerMap struct {
  9. Method string
  10. Path string //regex
  11. Handler func(http.ResponseWriter, *http.Request, *loan.Session)
  12. }
  13. var apiV1Handler = []apiV1HandlerMap{
  14. {"POST", "login", apiV1Login},
  15. {"GET", "login", apiV1DumpRequest},
  16. }
  17. //apiV1Main version 1 main entry for all REST API
  18. //
  19. func apiV1Main(w http.ResponseWriter, r *http.Request) {
  20. logRequestDebug(httputil.DumpRequest(r, true))
  21. w.Header().Set("Content-Type", "application/json;charset=UTF-8")
  22. path := r.URL.Path[len(apiV1Prefix):] //strip API prefix
  23. session := loan.Session{}
  24. session.Retrieve(r) //TODO: check remote_addr changes in DB, to prevent possible Hack
  25. for _, node := range apiV1Handler {
  26. if r.Method == node.Method && path == node.Path {
  27. node.Handler(w, r, &session)
  28. return
  29. }
  30. }
  31. //Catch for all
  32. apiV1DumpRequest(w, r, &session)
  33. }
  34. func apiV1ErrorCheck(e error) {
  35. if nil != e {
  36. panic(e.Error())
  37. }
  38. }