github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/extension_points.go (about)

     1  // TODO: it would be nice to move these into the top level folder so people can use these with the "functions" package, eg: functions.ApiHandler
     2  package server
     3  
     4  import (
     5  	"net/http"
     6  
     7  	"github.com/gin-gonic/gin"
     8  	"github.com/iron-io/functions/api"
     9  	"github.com/iron-io/functions/api/models"
    10  )
    11  
    12  type ApiHandlerFunc func(w http.ResponseWriter, r *http.Request)
    13  
    14  // ServeHTTP calls f(w, r).
    15  func (f ApiHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    16  	f(w, r)
    17  }
    18  
    19  type ApiHandler interface {
    20  	// Handle(ctx context.Context)
    21  	ServeHTTP(w http.ResponseWriter, r *http.Request)
    22  }
    23  
    24  type ApiAppHandler interface {
    25  	// Handle(ctx context.Context)
    26  	ServeHTTP(w http.ResponseWriter, r *http.Request, app *models.App)
    27  }
    28  
    29  type ApiAppHandlerFunc func(w http.ResponseWriter, r *http.Request, app *models.App)
    30  
    31  // ServeHTTP calls f(w, r).
    32  func (f ApiAppHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, app *models.App) {
    33  	f(w, r, app)
    34  }
    35  
    36  func (s *Server) apiHandlerWrapperFunc(apiHandler ApiHandler) gin.HandlerFunc {
    37  	return func(c *gin.Context) {
    38  		apiHandler.ServeHTTP(c.Writer, c.Request)
    39  	}
    40  }
    41  
    42  func (s *Server) apiAppHandlerWrapperFunc(apiHandler ApiAppHandler) gin.HandlerFunc {
    43  	return func(c *gin.Context) {
    44  		// get the app
    45  		appName := c.Param(api.CApp)
    46  		app, err := s.Datastore.GetApp(c.Request.Context(), appName)
    47  		if err != nil {
    48  			c.AbortWithError(http.StatusInternalServerError, err)
    49  			return
    50  		}
    51  		if app == nil {
    52  			c.AbortWithStatus(http.StatusNotFound)
    53  			return
    54  		}
    55  
    56  		apiHandler.ServeHTTP(c.Writer, c.Request, app)
    57  	}
    58  }
    59  
    60  // AddEndpoint adds an endpoint to /v1/x
    61  func (s *Server) AddEndpoint(method, path string, handler ApiHandler) {
    62  	v1 := s.Router.Group("/v1")
    63  	// v1.GET("/apps/:app/log", logHandler(cfg))
    64  	v1.Handle(method, path, s.apiHandlerWrapperFunc(handler))
    65  }
    66  
    67  // AddEndpoint adds an endpoint to /v1/x
    68  func (s *Server) AddEndpointFunc(method, path string, handler func(w http.ResponseWriter, r *http.Request)) {
    69  	s.AddEndpoint(method, path, ApiHandlerFunc(handler))
    70  }
    71  
    72  // AddAppEndpoint adds an endpoints to /v1/apps/:app/x
    73  func (s *Server) AddAppEndpoint(method, path string, handler ApiAppHandler) {
    74  	v1 := s.Router.Group("/v1")
    75  	v1.Handle(method, "/apps/:app"+path, s.apiAppHandlerWrapperFunc(handler))
    76  }
    77  
    78  // AddAppEndpoint adds an endpoints to /v1/apps/:app/x
    79  func (s *Server) AddAppEndpointFunc(method, path string, handler func(w http.ResponseWriter, r *http.Request, app *models.App)) {
    80  	s.AddAppEndpoint(method, path, ApiAppHandlerFunc(handler))
    81  }