api_routing.go 1018 B

123456789101112131415161718192021222324252627282930313233343536
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/torkelo/grafana-pro/pkg/models"
  5. )
  6. type routeHandlerRegisterFn func(self *HttpServer)
  7. type routeHandlerFn func(c *gin.Context, auth *authContext)
  8. var routeHandlers = make([]routeHandlerRegisterFn, 0)
  9. func getRouteHandlerWrapper(handler routeHandlerFn) gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. authContext := authContext{
  12. account: c.MustGet("usingAccount").(*models.UserAccount),
  13. userAccount: c.MustGet("userAccount").(*models.UserAccount),
  14. }
  15. handler(c, &authContext)
  16. }
  17. }
  18. func (self *HttpServer) addRoute(method string, path string, handler routeHandlerFn) {
  19. switch method {
  20. case "GET":
  21. self.router.GET(path, self.auth(), getRouteHandlerWrapper(handler))
  22. case "POST":
  23. self.router.POST(path, self.auth(), getRouteHandlerWrapper(handler))
  24. case "DELETE":
  25. self.router.DELETE(path, self.auth(), getRouteHandlerWrapper(handler))
  26. }
  27. }
  28. func addRoutes(fn routeHandlerRegisterFn) {
  29. routeHandlers = append(routeHandlers, fn)
  30. }