You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 satır
851B

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