Skip to content

Commit

Permalink
feat: some changes for 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vizee committed May 9, 2023
1 parent 4f7085a commit 07e4ed0
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 179 deletions.
3 changes: 2 additions & 1 deletion engine/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/vizee/gapi/internal/slices"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type GrpcDialer struct {
Expand All @@ -32,7 +33,7 @@ func NewBuilder() *Builder {
middlewares: make(map[string]HandleFunc),
handlers: make(map[string]CallHandler),
dialer: &GrpcDialer{
Opts: []grpc.DialOption{grpc.WithInsecure()},
Opts: []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())},
},
notFound: func(ctx *Context) error {
http.NotFound(ctx.resp, ctx.req)
Expand Down
3 changes: 2 additions & 1 deletion engine/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func TestBuildEngine(t *testing.T) {
Expand All @@ -24,7 +25,7 @@ func TestBuildEngine(t *testing.T) {
defer recover()
return ctx.Next()
})
builder.Dialer(&GrpcDialer{Opts: []grpc.DialOption{grpc.WithInsecure()}})
builder.Dialer(&GrpcDialer{Opts: []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}})
builder.NotFound(func(ctx *Context) error {
ctx.Response().Write([]byte(`404`))
return nil
Expand Down
4 changes: 4 additions & 0 deletions engine/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (c *Context) Response() http.ResponseWriter {
return c.resp
}

func (c *Context) SetResponse(w http.ResponseWriter) {
c.resp = w
}

func (ctx *Context) Set(name string, value string) {
if ctx.values == nil {
ctx.values = make(map[string]string)
Expand Down
38 changes: 27 additions & 11 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ func (e *Engine) generateMiddlewareChain(cache map[string][]HandleFunc, middlewa
return mws, nil
}

func (e *Engine) ClearRouter() {
e.routeLock.Lock()
clients := e.clients
e.clients = nil
e.router.Store(nil)
e.routeLock.Unlock()
for _, cc := range clients {
cc.Close()
}
}

func (e *Engine) RebuildRouter(routes []metadata.Route, ignoreError bool) error {
e.routeLock.Lock()
defer e.routeLock.Unlock()
Expand Down Expand Up @@ -169,22 +180,27 @@ func (e *Engine) Execute(w http.ResponseWriter, req *http.Request, params Params
e.ctxpool.Put(ctx)
}

func (e *Engine) handleNotFound(w http.ResponseWriter, req *http.Request) {
func (e *Engine) NotFound(w http.ResponseWriter, req *http.Request) {
e.Execute(w, req, nil, e.uses, e.notFound)
}

func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.Debugf("Route %s %s", req.Method, req.URL.Path)

path := req.URL.Path
handle, ps, tsr := e.router.Load().Lookup(req.Method, path)
if handle != nil {
handle(w, req, ps)
} else if tsr && path != "/" {
log.Debugf("Trailing slash redirect %s", req.URL.Path)
req.URL.Path = path + "/"
http.Redirect(w, req, req.URL.String(), http.StatusMovedPermanently)
} else {
e.handleNotFound(w, req)
router := e.router.Load()
if router != nil {
path := req.URL.Path
handle, ps, tsr := router.Lookup(req.Method, path)
if handle != nil {
handle(w, req, ps)
return
} else if tsr && path != "/" {
log.Debugf("Trailing slash redirect %s", req.URL.Path)
req.URL.Path = path + "/"
http.Redirect(w, req, req.URL.String(), http.StatusMovedPermanently)
return
}
}

e.NotFound(w, req)
}
3 changes: 2 additions & 1 deletion engine/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"testing"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func Test_grpcRoute_handleRoute(t *testing.T) {
cc, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
cc, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/vizee/gapi/handlers/httpview"
"github.com/vizee/gapi/handlers/jsonapi"
"github.com/vizee/gapi/metadata"
"github.com/vizee/gapi/metadata/descs"
"github.com/vizee/gapi/proto/descriptor"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -106,7 +107,7 @@ func loadRoutes(fname string) ([]metadata.Route, error) {
}
}

return metadata.ResolveRoutes(&metadata.MessageCache{}, p.Services(), false)
return descs.ToRoutes(&descs.MessageCache{}, p.Services(), false)
}

func main() {
Expand Down
25 changes: 13 additions & 12 deletions metadata/descriptor.go → metadata/descs/descriptor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metadata
package descs

import (
"errors"
Expand All @@ -7,14 +7,15 @@ import (

annotation "github.com/vizee/gapi-proto-go/gapi"
"github.com/vizee/gapi/internal/slices"
"github.com/vizee/gapi/metadata"
"github.com/vizee/gapi/proto/descriptor"
"github.com/vizee/jsonpb"
"google.golang.org/protobuf/types/descriptorpb"
)

type messageDesc struct {
*jsonpb.Message
bindings []FieldBinding
bindings []metadata.FieldBinding
}

type MessageCache struct {
Expand Down Expand Up @@ -73,18 +74,18 @@ func (mc *MessageCache) Resolve(md *descriptor.MessageDesc) *messageDesc {
Omit: omit,
})
} else {
var bind BindSource
var bind metadata.BindSource
switch fd.Bind {
case annotation.FIELD_BIND_FROM_QUERY:
bind = BindQuery
bind = metadata.BindQuery
case annotation.FIELD_BIND_FROM_PARAMS:
bind = BindParams
bind = metadata.BindParams
case annotation.FIELD_BIND_FROM_HEADER:
bind = BindHeader
bind = metadata.BindHeader
case annotation.FIELD_BIND_FROM_CONTEXT:
bind = BindContext
bind = metadata.BindContext
}
msg.bindings = append(msg.bindings, FieldBinding{
msg.bindings = append(msg.bindings, metadata.FieldBinding{
Name: name,
Kind: kind,
Tag: uint32(fd.Tag),
Expand All @@ -103,7 +104,7 @@ func (mc *MessageCache) Resolve(md *descriptor.MessageDesc) *messageDesc {
return msg
}

func ResolveRoutes(mc *MessageCache, sds []*descriptor.ServiceDesc, ignoreError bool) ([]Route, error) {
func ToRoutes(mc *MessageCache, sds []*descriptor.ServiceDesc, ignoreError bool) ([]metadata.Route, error) {
routesNum := 0
for _, sd := range sds {
if sd.Opts.Server == "" {
Expand All @@ -116,7 +117,7 @@ func ResolveRoutes(mc *MessageCache, sds []*descriptor.ServiceDesc, ignoreError
routesNum++
}
}
routes := make([]Route, 0, routesNum)
routes := make([]metadata.Route, 0, routesNum)
walksd:
for _, sd := range sds {
server := sd.Opts.Server
Expand Down Expand Up @@ -163,11 +164,11 @@ walksd:
}

inMsg := mc.Resolve(md.In)
routes = append(routes, Route{
routes = append(routes, metadata.Route{
Method: md.Opts.Method,
Path: sd.Opts.PathPrefix + md.Opts.Path,
Use: slices.Merge(sd.Opts.Use, md.Opts.Use),
Call: &Call{
Call: &metadata.Call{
Server: server,
Handler: handler,
Method: concatFullMethodName(sd.FullName, md.Name),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metadata
package descs

import (
"encoding/json"
Expand All @@ -10,7 +10,7 @@ import (
"google.golang.org/protobuf/types/descriptorpb"
)

func TestResolveRoutes(t *testing.T) {
func TestToRoutes(t *testing.T) {
data, err := os.ReadFile("../testdata/pdtest/pdtest.pd")
if err != nil {
t.Fatal(err)
Expand All @@ -28,7 +28,7 @@ func TestResolveRoutes(t *testing.T) {
t.Fatal(err)
}
}
routes, err := ResolveRoutes(&MessageCache{}, p.Services(), false)
routes, err := ToRoutes(&MessageCache{}, p.Services(), false)
if err != nil {
t.Fatal(err)
}
Expand Down
149 changes: 0 additions & 149 deletions proto/reflection/descriptor.go

This file was deleted.

0 comments on commit 07e4ed0

Please sign in to comment.