Skip to content

Commit b06bb74

Browse files
committed
Updated router
1 parent 871dffb commit b06bb74

File tree

11 files changed

+566
-317
lines changed

11 files changed

+566
-317
lines changed

pkg/handler/router/config.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package router
2+
3+
import (
4+
// Packages
5+
server "github.com/mutablelogic/go-server"
6+
)
7+
8+
////////////////////////////////////////////////////////////////////////////////
9+
// TYPES
10+
11+
type Config struct {
12+
Services ServiceConfig `hcl:"services"`
13+
}
14+
15+
type ServiceConfig map[string]struct {
16+
Service server.ServiceEndpoints `hcl:"service"`
17+
Middleware []server.Middleware `hcl:"middleware"`
18+
}
19+
20+
// Ensure interfaces is implemented
21+
var _ server.Plugin = Config{}
22+
23+
///////////////////////////////////////////////////////////////////////////////
24+
// GLOBALS
25+
26+
const (
27+
defaultName = "router"
28+
defaultCap = 10
29+
pathSep = "/"
30+
hostSep = "."
31+
envRequestPrefix = "REQUEST_PREFIX"
32+
)
33+
34+
///////////////////////////////////////////////////////////////////////////////
35+
// LIFECYCLE
36+
37+
// Name returns the name of the service
38+
func (Config) Name() string {
39+
return defaultName
40+
}
41+
42+
// Description returns the description of the service
43+
func (Config) Description() string {
44+
return "router for http requests"
45+
}
46+
47+
// Create a new router from the configuration
48+
func (c Config) New() (server.Task, error) {
49+
return New(c)
50+
}

pkg/handler/router/context.go

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ const (
2222
contextHost
2323
contextPrefix
2424
contextParams
25+
contextRequest
2526
contextMiddleware
27+
contextScope
28+
contextMethod
2629
contextTime
2730
)
2831

@@ -49,24 +52,14 @@ func WithTime(ctx context.Context, t time.Time) context.Context {
4952
return context.WithValue(ctx, contextTime, t)
5053
}
5154

52-
// WithRoute returns a context with various route parameters
53-
func WithRoute(ctx context.Context, route *Route) context.Context {
54-
if route == nil {
55-
return ctx
56-
}
57-
if route.Label != "" {
58-
ctx = provider.WithLabel(ctx, route.Label)
59-
}
60-
if route.Host != "" {
61-
ctx = WithHost(ctx, route.Host)
62-
}
63-
if route.Prefix != "" {
64-
ctx = WithPrefix(ctx, route.Prefix)
65-
}
66-
if len(route.Parameters) > 0 {
67-
ctx = context.WithValue(ctx, contextParams, route.Parameters)
68-
}
69-
return ctx
55+
// WithScope returns a context with the given scooes
56+
func WithScope(ctx context.Context, scope ...string) context.Context {
57+
return context.WithValue(ctx, contextScope, scope)
58+
}
59+
60+
// WithMethod returns a context with the given methods
61+
func WithMethod(ctx context.Context, method ...string) context.Context {
62+
return context.WithValue(ctx, contextMethod, method)
7063
}
7164

7265
// WithMiddleware returns a context with the given middleware
@@ -77,6 +70,37 @@ func WithMiddleware(ctx context.Context, middleware ...server.Middleware) contex
7770
return context.WithValue(ctx, contextMiddleware, append(Middleware(ctx), middleware...))
7871
}
7972

73+
// WithRoute returns a context with various route parameters
74+
func WithRoute(ctx context.Context, route *matchedRoute) context.Context {
75+
if route == nil {
76+
return ctx
77+
}
78+
if route.route != nil {
79+
if route.label != "" {
80+
ctx = provider.WithLabel(ctx, route.label)
81+
}
82+
if route.host != "" {
83+
ctx = WithHost(ctx, route.host)
84+
}
85+
if route.prefix != "" {
86+
ctx = WithPrefix(ctx, route.prefix)
87+
}
88+
if len(route.scopes) > 0 {
89+
ctx = WithScope(ctx, route.scopes...)
90+
}
91+
if len(route.methods) > 0 {
92+
ctx = WithMethod(ctx, route.methods...)
93+
}
94+
}
95+
if len(route.parameters) > 0 {
96+
ctx = context.WithValue(ctx, contextParams, route.parameters)
97+
}
98+
if route.request != "" {
99+
ctx = context.WithValue(ctx, contextRequest, route.request)
100+
}
101+
return ctx
102+
}
103+
80104
// Host returns the host from the context, or zero value if not defined
81105
func Host(ctx context.Context) string {
82106
return str(ctx, contextHost)
@@ -89,11 +113,7 @@ func Prefix(ctx context.Context) string {
89113

90114
// Params returns the path parameters from the context, or zero value if not defined
91115
func Params(ctx context.Context) []string {
92-
if value, ok := ctx.Value(contextParams).([]string); ok {
93-
return value
94-
} else {
95-
return nil
96-
}
116+
return strslice(ctx, contextParams)
97117
}
98118

99119
// Middleware returns a set of middleware from the context, or zero value if not defined
@@ -114,6 +134,16 @@ func Time(ctx context.Context) time.Time {
114134
}
115135
}
116136

137+
// Scope returns the stored scope or nil if not defined
138+
func Scope(ctx context.Context) []string {
139+
return strslice(ctx, contextScope)
140+
}
141+
142+
// Method returns the stored methods or nil if not defined
143+
func Method(ctx context.Context) []string {
144+
return strslice(ctx, contextMethod)
145+
}
146+
117147
////////////////////////////////////////////////////////////////////////////////
118148
// PRIVATE METHODS
119149

@@ -123,3 +153,10 @@ func str(ctx context.Context, key RouterContextKey) string {
123153
}
124154
return ""
125155
}
156+
157+
func strslice(ctx context.Context, key RouterContextKey) []string {
158+
if value, ok := ctx.Value(key).([]string); ok {
159+
return value
160+
}
161+
return nil
162+
}

pkg/handler/router/interface.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package router
2+
3+
import (
4+
// Packages
5+
server "github.com/mutablelogic/go-server"
6+
)
7+
8+
///////////////////////////////////////////////////////////////////////////////
9+
// TYPES
10+
11+
type Router interface {
12+
server.Router
13+
14+
// Match a host, method and path to a handler. Returns the appropriate
15+
// http status code, which will be 200 on success, 404 or 405 and
16+
// path parameters extracted from the path.
17+
Match(host, method, path string) (*matchedRoute, int)
18+
}
19+
20+
type Route interface {
21+
server.Route
22+
23+
// Set scope
24+
SetScope(...string) Route
25+
}

0 commit comments

Comments
 (0)