Skip to content

Commit df28ea7

Browse files
committed
feat: WIP embedded gateway.
1 parent 8d325e3 commit df28ea7

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/davecgh/go-spew v1.1.1
99
github.com/docker/docker v20.10.11+incompatible
1010
github.com/docker/go-connections v0.4.0
11+
github.com/fasthttp/router v1.4.5
1112
github.com/fatih/color v1.13.0
1213
github.com/getkin/kin-openapi v0.88.0
1314
github.com/go-openapi/strfmt v0.21.1 // indirect
@@ -27,6 +28,7 @@ require (
2728
github.com/pkg/errors v0.9.1
2829
github.com/spf13/cobra v1.2.1
2930
github.com/spf13/viper v1.9.0
31+
github.com/valyala/fasthttp v1.32.0
3032
golang.org/x/net v0.0.0-20211105192438-b53810dc28af // indirect
3133
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
3234
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect

go.sum

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ github.com/esimonov/ifshort v1.0.3/go.mod h1:yZqNJUrNn20K8Q9n2CrjTKYyVEmX209Hgu+
400400
github.com/ettle/strcase v0.1.1 h1:htFueZyVeE1XNnMEfbqp5r67qAN/4r6ya1ysq8Q+Zcw=
401401
github.com/ettle/strcase v0.1.1/go.mod h1:hzDLsPC7/lwKyBOywSHEP89nt2pDgdy+No1NBA9o9VY=
402402
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
403+
github.com/fasthttp/router v1.4.5 h1:YZonsKCssEwEi3veDMhL6okIx550qegAiuXAK8NnM3Y=
404+
github.com/fasthttp/router v1.4.5/go.mod h1:UYExWhCy7pUmavRZ0XfjEgHwzxyKwyS8uzXhaTRDG9Y=
403405
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
404406
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
405407
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
@@ -1104,6 +1106,8 @@ github.com/sagikazarmark/crypt v0.1.0/go.mod h1:B/mN0msZuINBtQ1zZLEQcegFJJf9vnYI
11041106
github.com/sanposhiho/wastedassign/v2 v2.0.6 h1:+6/hQIHKNJAUixEj6EmOngGIisyeI+T3335lYTyxRoA=
11051107
github.com/sanposhiho/wastedassign/v2 v2.0.6/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI=
11061108
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
1109+
github.com/savsgio/gotils v0.0.0-20211223103454-d0aaa54c5899 h1:Orn7s+r1raRTBKLSc9DmbktTT04sL+vkzsbRD2Q8rOI=
1110+
github.com/savsgio/gotils v0.0.0-20211223103454-d0aaa54c5899/go.mod h1:oejLrk1Y/5zOF+c/aHtXqn3TFlzzbAgPWg8zBiAHDas=
11071111
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
11081112
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
11091113
github.com/securego/gosec/v2 v2.9.1 h1:anHKLS/ApTYU6NZkKa/5cQqqcbKZURjvc+MtR++S4EQ=
@@ -1234,8 +1238,9 @@ github.com/uudashr/gocognit v1.0.5/go.mod h1:wgYz0mitoKOTysqxTDMOUXg+Jb5Svtihkfm
12341238
github.com/uw-labs/lichen v0.1.4/go.mod h1:Fljba7ubiwHbEp1nzUjth+cWUbyQe73WUyL1++5HRNY=
12351239
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
12361240
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
1237-
github.com/valyala/fasthttp v1.30.0 h1:nBNzWrgZUUHohyLPU/jTvXdhrcaf2m5k3bWk+3Q049g=
12381241
github.com/valyala/fasthttp v1.30.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus=
1242+
github.com/valyala/fasthttp v1.32.0 h1:keswgWzyKyNIIjz2a7JmCYHOOIkRp6HMx9oTV6QrZWY=
1243+
github.com/valyala/fasthttp v1.32.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus=
12391244
github.com/valyala/quicktemplate v1.7.0/go.mod h1:sqKJnoaOF88V07vkO+9FL8fb9uZg/VPSJnLYn+LmLk8=
12401245
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
12411246
github.com/viki-org/dnscache v0.0.0-20130720023526-c70c1f23c5d8/go.mod h1:dniwbG03GafCjFohMDmz6Zc6oCuiqgH6tGNyXTkHzXE=

pkg/provider/run/gateway.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,143 @@
1313
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
16+
// Copyright 2021 Nitric Pty Ltd.
17+
1618
package run
19+
20+
import (
21+
"strings"
22+
"time"
23+
24+
"github.com/fasthttp/router"
25+
"github.com/valyala/fasthttp"
26+
27+
"github.com/nitrictech/nitric/pkg/plugins/gateway"
28+
"github.com/nitrictech/nitric/pkg/triggers"
29+
"github.com/nitrictech/nitric/pkg/utils"
30+
"github.com/nitrictech/nitric/pkg/worker"
31+
)
32+
33+
type HttpMiddleware func(*fasthttp.RequestCtx, worker.WorkerPool) bool
34+
35+
type BaseHttpGateway struct {
36+
address string
37+
server *fasthttp.Server
38+
gateway.UnimplementedGatewayPlugin
39+
40+
pool worker.WorkerPool
41+
}
42+
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+
// }
48+
49+
// return false
50+
// }
51+
//}
52+
53+
func (s *BaseHttpGateway) api(ctx *fasthttp.RequestCtx) {
54+
apiName := ctx.UserValue("name")
55+
// Rewrite the URL of the request to remove the /api/{name} subroute
56+
pathParts := utils.SplitPath(string(ctx.Path()))
57+
// remove first two path parts
58+
newPathParts := pathParts[2:]
59+
60+
newPath := strings.Join(newPathParts, "/")
61+
62+
// Rewrite the path
63+
ctx.URI().SetPath(newPath)
64+
65+
httpReq := triggers.FromHttpRequest(ctx)
66+
67+
s.pool.GetWorker(&worker.GetWorkerOptions{
68+
Http: httpReq,
69+
//Filter: apiWorkerFilter(apiName),
70+
})
71+
72+
// Filter workers by a specific named API
73+
74+
}
75+
76+
func (s *BaseHttpGateway) schedule(ctx *fasthttp.RequestCtx) {
77+
scheduleName := ctx.UserValue("name")
78+
// Filter workers by schedule workers
79+
}
80+
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+
//}
118+
119+
func (s *BaseHttpGateway) Start(pool worker.WorkerPool) error {
120+
s.pool = pool
121+
122+
// Setup routes
123+
r := router.New()
124+
// Make a request for an API gateway
125+
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+
131+
s.server = &fasthttp.Server{
132+
IdleTimeout: time.Second * 1,
133+
CloseOnShutdown: true,
134+
Handler: r.Handler,
135+
}
136+
137+
return s.server.ListenAndServe(s.address)
138+
}
139+
140+
func (s *BaseHttpGateway) Stop() error {
141+
if s.server != nil {
142+
return s.server.Shutdown()
143+
}
144+
return nil
145+
}
146+
147+
// Create new HTTP gateway
148+
// 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) {
150+
address := utils.GetEnv("GATEWAY_ADDRESS", ":9001")
151+
152+
return &BaseHttpGateway{
153+
address: address,
154+
}, nil
155+
}

0 commit comments

Comments
 (0)