Skip to content

Commit 48ae508

Browse files
committed
feat: Add embedded gateway with api subrouting.
1 parent df28ea7 commit 48ae508

File tree

4 files changed

+37
-65
lines changed

4 files changed

+37
-65
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
github.com/mattn/go-runewidth v0.0.13 // indirect
2525
github.com/mitchellh/mapstructure v1.4.2
2626
github.com/nitrictech/boxygen v0.0.1-rc.7.0.20211212231606-62c668408f91
27-
github.com/nitrictech/nitric v0.13.0-rc.8
27+
github.com/nitrictech/nitric v0.13.0-rc.10
2828
github.com/pkg/errors v0.9.1
2929
github.com/spf13/cobra v1.2.1
3030
github.com/spf13/viper v1.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,8 @@ github.com/nishanths/predeclared v0.2.1 h1:1TXtjmy4f3YCFjTxRd8zcFHOmoUir+gp0ESzj
953953
github.com/nishanths/predeclared v0.2.1/go.mod h1:HvkGJcA3naj4lOwnFXFDkFxVtSqQMB9sbB1usJ+xjQE=
954954
github.com/nitrictech/boxygen v0.0.1-rc.7.0.20211212231606-62c668408f91 h1:gtZZJc7l5pML1eRsqyXe0U7NdQxSa7u/cbyEvnGLBpc=
955955
github.com/nitrictech/boxygen v0.0.1-rc.7.0.20211212231606-62c668408f91/go.mod h1:2XXi1xEwqitH4/gus1bHyG/IQe8WOniK+pybGTz2y/Y=
956-
github.com/nitrictech/nitric v0.13.0-rc.8 h1:LorbpJW2QcKHeDTnTKjBLthWc5jQOmK/KU2AiRXYyJY=
957-
github.com/nitrictech/nitric v0.13.0-rc.8/go.mod h1:XC6DG1/NrMc59Jzq/1h6SLn6L4foSS67pCqyTpauT3o=
956+
github.com/nitrictech/nitric v0.13.0-rc.10 h1:BgwCQALxoGHBFYay9ZmsGGhOmcq1QGLYC6FJcrmjNcQ=
957+
github.com/nitrictech/nitric v0.13.0-rc.10/go.mod h1:XC6DG1/NrMc59Jzq/1h6SLn6L4foSS67pCqyTpauT3o=
958958
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
959959
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
960960
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=

pkg/cmd/run/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/nitrictech/newcli/pkg/provider/run"
3131
"github.com/nitrictech/nitric/pkg/membrane"
3232
boltdb_service "github.com/nitrictech/nitric/pkg/plugins/document/boltdb"
33-
gateway_plugin "github.com/nitrictech/nitric/pkg/plugins/gateway/dev"
3433
minio "github.com/nitrictech/nitric/pkg/plugins/storage/minio"
3534
"github.com/nitrictech/nitric/pkg/worker"
3635
)
@@ -99,7 +98,7 @@ var runCmd = &cobra.Command{
9998
})
10099

101100
// Start a new gateway plugin
102-
gw, err := gateway_plugin.New()
101+
gw, err := run.NewGateway()
103102

104103
if err != nil {
105104
cobra.CheckErr(err)

pkg/provider/run/gateway.go

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package run
1919

2020
import (
21+
"fmt"
2122
"strings"
2223
"time"
2324

@@ -40,18 +41,18 @@ type BaseHttpGateway struct {
4041
pool worker.WorkerPool
4142
}
4243

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+
}
4849

49-
// return false
50-
// }
51-
//}
50+
return false
51+
}
52+
}
5253

5354
func (s *BaseHttpGateway) api(ctx *fasthttp.RequestCtx) {
54-
apiName := ctx.UserValue("name")
55+
apiName := ctx.UserValue("name").(string)
5556
// Rewrite the URL of the request to remove the /api/{name} subroute
5657
pathParts := utils.SplitPath(string(ctx.Path()))
5758
// remove first two path parts
@@ -64,57 +65,32 @@ func (s *BaseHttpGateway) api(ctx *fasthttp.RequestCtx) {
6465

6566
httpReq := triggers.FromHttpRequest(ctx)
6667

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),
7071
})
7172

72-
// Filter workers by a specific named API
73+
if err != nil {
74+
ctx.Error("worker not found for api", 404)
75+
return
76+
}
7377

74-
}
78+
resp, err := worker.HandleHttpRequest(httpReq)
7579

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+
}
8088

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+
}
11894

11995
func (s *BaseHttpGateway) Start(pool worker.WorkerPool) error {
12096
s.pool = pool
@@ -123,12 +99,9 @@ func (s *BaseHttpGateway) Start(pool worker.WorkerPool) error {
12399
r := router.New()
124100
// Make a request for an API gateway
125101
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)
130102

131103
s.server = &fasthttp.Server{
104+
ReadTimeout: time.Second * 1,
132105
IdleTimeout: time.Second * 1,
133106
CloseOnShutdown: true,
134107
Handler: r.Handler,
@@ -146,7 +119,7 @@ func (s *BaseHttpGateway) Stop() error {
146119

147120
// Create new HTTP gateway
148121
// 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) {
150123
address := utils.GetEnv("GATEWAY_ADDRESS", ":9001")
151124

152125
return &BaseHttpGateway{

0 commit comments

Comments
 (0)