18
18
package run
19
19
20
20
import (
21
+ "fmt"
21
22
"strings"
22
23
"time"
23
24
@@ -40,18 +41,18 @@ type BaseHttpGateway struct {
40
41
pool worker.WorkerPool
41
42
}
42
43
43
- // func apiWorkerFilter (apiName string) func(w worker.Worker) bool {
44
- // return func(w worker.Worker) bool {
45
- // if api, ok := w.(*worker.RouteWorker); ok {
46
- // return api.Api() == apiName
47
- // }
44
+ func apiWorkerFilter (apiName string ) func (w worker.Worker ) bool {
45
+ return func (w worker.Worker ) bool {
46
+ if api , ok := w .(* worker.RouteWorker ); ok {
47
+ return api .Api () == apiName
48
+ }
48
49
49
- // return false
50
- // }
51
- // }
50
+ return false
51
+ }
52
+ }
52
53
53
54
func (s * BaseHttpGateway ) api (ctx * fasthttp.RequestCtx ) {
54
- apiName := ctx .UserValue ("name" )
55
+ apiName := ctx .UserValue ("name" ).( string )
55
56
// Rewrite the URL of the request to remove the /api/{name} subroute
56
57
pathParts := utils .SplitPath (string (ctx .Path ()))
57
58
// remove first two path parts
@@ -64,57 +65,32 @@ func (s *BaseHttpGateway) api(ctx *fasthttp.RequestCtx) {
64
65
65
66
httpReq := triggers .FromHttpRequest (ctx )
66
67
67
- s .pool .GetWorker (& worker.GetWorkerOptions {
68
- Http : httpReq ,
69
- // Filter: apiWorkerFilter(apiName),
68
+ worker , err := s .pool .GetWorker (& worker.GetWorkerOptions {
69
+ Http : httpReq ,
70
+ Filter : apiWorkerFilter (apiName ),
70
71
})
71
72
72
- // Filter workers by a specific named API
73
+ if err != nil {
74
+ ctx .Error ("worker not found for api" , 404 )
75
+ return
76
+ }
73
77
74
- }
78
+ resp , err := worker . HandleHttpRequest ( httpReq )
75
79
76
- func (s * BaseHttpGateway ) schedule (ctx * fasthttp.RequestCtx ) {
77
- scheduleName := ctx .UserValue ("name" )
78
- // Filter workers by schedule workers
79
- }
80
+ if err != nil {
81
+ ctx .Error (fmt .Sprintf ("Error handling HTTP Request: %v" , err ), 500 )
82
+ return
83
+ }
84
+
85
+ if resp .Header != nil {
86
+ resp .Header .CopyTo (& ctx .Response .Header )
87
+ }
80
88
81
- //func (s *BaseHttpGateway) httpHandler(pool worker.WorkerPool) func(ctx *fasthttp.RequestCtx) {
82
- // return func(ctx *fasthttp.RequestCtx) {
83
- // if s.mw != nil {
84
- // if !s.mw(ctx, pool) {
85
- // // middleware has indicated that is has processed the request
86
- // // so we can exit here
87
- // return
88
- // }
89
- // }
90
-
91
- // httpTrigger := triggers.FromHttpRequest(ctx)
92
- // wrkr, err := pool.GetWorker(&worker.GetWorkerOptions{
93
- // Http: httpTrigger,
94
- // })
95
-
96
- // if err != nil {
97
- // ctx.Error("Unable to get worker to handle request", 500)
98
- // return
99
- // }
100
-
101
- // response, err := wrkr.HandleHttpRequest(httpTrigger)
102
-
103
- // if err != nil {
104
- // ctx.Error(fmt.Sprintf("Error handling HTTP Request: %v", err), 500)
105
- // return
106
- // }
107
-
108
- // if response.Header != nil {
109
- // response.Header.CopyTo(&ctx.Response.Header)
110
- // }
111
-
112
- // // Avoid content length header duplication
113
- // ctx.Response.Header.Del("Content-Length")
114
- // ctx.Response.SetStatusCode(response.StatusCode)
115
- // ctx.Response.SetBody(response.Body)
116
- // }
117
- //}
89
+ // Avoid content length header duplication
90
+ ctx .Response .Header .Del ("Content-Length" )
91
+ ctx .Response .SetStatusCode (resp .StatusCode )
92
+ ctx .Response .SetBody (resp .Body )
93
+ }
118
94
119
95
func (s * BaseHttpGateway ) Start (pool worker.WorkerPool ) error {
120
96
s .pool = pool
@@ -123,12 +99,9 @@ func (s *BaseHttpGateway) Start(pool worker.WorkerPool) error {
123
99
r := router .New ()
124
100
// Make a request for an API gateway
125
101
r .ANY ("/apis/{name}/{any:*}" , s .api )
126
- // TODO: Make a request to a specific registered function
127
- // r.ANY("/function/{name}/{any:*}", s.function)
128
- // Make a request to trigger a schedule
129
- r .POST ("/schedules/{name}" , s .schedule )
130
102
131
103
s .server = & fasthttp.Server {
104
+ ReadTimeout : time .Second * 1 ,
132
105
IdleTimeout : time .Second * 1 ,
133
106
CloseOnShutdown : true ,
134
107
Handler : r .Handler ,
@@ -146,7 +119,7 @@ func (s *BaseHttpGateway) Stop() error {
146
119
147
120
// Create new HTTP gateway
148
121
// XXX: No External Args for function atm (currently the plugin loader does not pass any argument information)
149
- func New ( mw HttpMiddleware ) (gateway.GatewayService , error ) {
122
+ func NewGateway ( ) (gateway.GatewayService , error ) {
150
123
address := utils .GetEnv ("GATEWAY_ADDRESS" , ":9001" )
151
124
152
125
return & BaseHttpGateway {
0 commit comments