mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
@@ -33,7 +33,7 @@ type Server struct {
|
||||
singleflight singleflight // singleflight assists Datastore
|
||||
}
|
||||
|
||||
func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, r *runner.Runner, tasks chan task.Request, enqueue models.Enqueue) *Server {
|
||||
func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, r *runner.Runner, tasks chan task.Request, enqueue models.Enqueue, opts ...ServerOption) *Server {
|
||||
s := &Server{
|
||||
Runner: r,
|
||||
Router: gin.New(),
|
||||
@@ -45,6 +45,10 @@ func New(ctx context.Context, ds models.Datastore, mq models.MessageQueue, r *ru
|
||||
|
||||
s.Router.Use(prepareMiddleware(ctx))
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(s)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
11
api/server/server_options.go
Normal file
11
api/server/server_options.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package server
|
||||
|
||||
import "context"
|
||||
|
||||
type ServerOption func(*Server)
|
||||
|
||||
func EnableShutdownEndpoint(halt context.CancelFunc) ServerOption {
|
||||
return func(s *Server) {
|
||||
s.Router.GET("/shutdown", s.handleShutdown(halt))
|
||||
}
|
||||
}
|
||||
15
api/server/shutdown.go
Normal file
15
api/server/shutdown.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (s *Server) handleShutdown(halt context.CancelFunc) func(*gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
halt()
|
||||
c.JSON(http.StatusOK, "shutting down")
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,13 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/iron-io/functions/api/datastore"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/api/mqs"
|
||||
"github.com/iron-io/functions/api/runner"
|
||||
"github.com/iron-io/functions/api/runner/task"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testSpecialHandler struct{}
|
||||
|
||||
Reference in New Issue
Block a user