Skip to content
This repository was archived by the owner on Mar 29, 2025. It is now read-only.

Commit 7f36c2f

Browse files
authored
Merge pull request #2 from randlabs/minor_refactor
Refactor to remove models package
2 parents 66a3a41 + a365444 commit 7f36c2f

File tree

9 files changed

+68
-89
lines changed

9 files changed

+68
-89
lines changed

http_adaptor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ package go_webserver
33
import (
44
"net/http"
55

6-
"github.com/randlabs/go-webserver/models"
76
"github.com/randlabs/go-webserver/request"
87
"github.com/valyala/fasthttp/fasthttpadaptor"
98
)
109

1110
// -----------------------------------------------------------------------------
1211

13-
func HandlerFromHttpHandler(handler http.Handler) models.HandlerFunc {
12+
func HandlerFromHttpHandler(handler http.Handler) HandlerFunc {
1413
fasthttpHandler := fasthttpadaptor.NewFastHTTPHandler(handler)
1514
return func(req *request.RequestContext) error {
1615
req.CallFastHttpHandler(fasthttpHandler)
1716
return nil
1817
}
1918
}
2019

21-
func HandlerFromHttpHandlerFunc(f http.HandlerFunc) models.HandlerFunc {
20+
func HandlerFromHttpHandlerFunc(f http.HandlerFunc) HandlerFunc {
2221
return HandlerFromHttpHandler(f)
2322
}

webserver_internal.go renamed to internal.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package go_webserver
33
import (
44
"net/http"
55

6-
"github.com/randlabs/go-webserver/middleware"
7-
"github.com/randlabs/go-webserver/models"
86
"github.com/randlabs/go-webserver/request"
97
"github.com/valyala/fasthttp"
108
)
@@ -32,8 +30,8 @@ func (srv *Server) createMasterHandler(masterHandler fasthttp.RequestHandler) fa
3230
}
3331

3432
// Build the recursive function for server-wide middlewares
35-
var f func(idx int) models.HandlerFunc
36-
f = func(idx int) models.HandlerFunc {
33+
var f func(idx int) HandlerFunc
34+
f = func(idx int) HandlerFunc {
3735
if idx < len(srv.middlewares) {
3836
return srv.middlewares[idx](f(idx + 1))
3937
}
@@ -65,12 +63,10 @@ func (srv *Server) createMasterHandler(masterHandler fasthttp.RequestHandler) fa
6563
}
6664
}
6765

68-
func (srv *Server) createEndpointHandler(
69-
epHandler models.HandlerFunc, middlewares ...middleware.MiddlewareFunc,
70-
) fasthttp.RequestHandler {
66+
func (srv *Server) createEndpointHandler(epHandler HandlerFunc, middlewares ...MiddlewareFunc) fasthttp.RequestHandler {
7167
// Build the recursive function for endpoint middlewares
72-
var f func(idx int) models.HandlerFunc
73-
f = func(idx int) models.HandlerFunc {
68+
var f func(idx int) HandlerFunc
69+
f = func(idx int) HandlerFunc {
7470
if idx < len(middlewares) {
7571
return middlewares[idx](f(idx + 1))
7672
}

middleware/cache.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,45 @@ package middleware
22

33
import (
44
"fmt"
5-
"github.com/randlabs/go-webserver/models"
6-
"github.com/randlabs/go-webserver/request"
75
"strings"
6+
7+
webserver "github.com/randlabs/go-webserver"
8+
"github.com/randlabs/go-webserver/request"
89
)
910

1011
// -----------------------------------------------------------------------------
1112

1213
type CacheControlOptions struct {
13-
Public bool
14-
Private bool
15-
NoCache bool
16-
NoStore bool
17-
NoTransform bool
18-
MustRevalidate bool
19-
ProxyRevalidate bool
20-
MaxAgeInSeconds *uint32
21-
SharedMaxAgeInSeconds *uint32
22-
StaleWhileRevalidateInSeconds *uint32
23-
StaleIfErrorInSeconds *uint32
14+
Public bool
15+
Private bool
16+
NoCache bool
17+
NoStore bool
18+
NoTransform bool
19+
MustRevalidate bool
20+
ProxyRevalidate bool
21+
MaxAgeInSeconds *uint32
22+
SharedMaxAgeInSeconds *uint32
23+
StaleWhileRevalidateInSeconds *uint32
24+
StaleIfErrorInSeconds *uint32
2425
}
2526

2627
// -----------------------------------------------------------------------------
2728

28-
func DisableCacheControl() MiddlewareFunc {
29+
func DisableCacheControl() webserver.MiddlewareFunc {
2930
var zero uint32
3031

3132
return NewCacheControl(CacheControlOptions{
32-
Private: true,
33-
NoCache: true,
34-
NoStore: true,
35-
MustRevalidate: true,
36-
ProxyRevalidate: true,
37-
MaxAgeInSeconds: &zero,
38-
SharedMaxAgeInSeconds: &zero,
33+
Private: true,
34+
NoCache: true,
35+
NoStore: true,
36+
MustRevalidate: true,
37+
ProxyRevalidate: true,
38+
MaxAgeInSeconds: &zero,
39+
SharedMaxAgeInSeconds: &zero,
3940
})
4041
}
4142

42-
func NewCacheControl(opts CacheControlOptions) MiddlewareFunc {
43+
func NewCacheControl(opts CacheControlOptions) webserver.MiddlewareFunc {
4344
cacheValue := make([]string, 0)
4445

4546
if opts.Public {
@@ -81,7 +82,7 @@ func NewCacheControl(opts CacheControlOptions) MiddlewareFunc {
8182

8283
finalCacheValue := strings.Join(cacheValue, ",")
8384

84-
return func(next models.HandlerFunc) models.HandlerFunc {
85+
return func(next webserver.HandlerFunc) webserver.HandlerFunc {
8586
return func(req *request.RequestContext) error {
8687
// Set cache control header
8788
req.SetResponseHeader("Cache-Control", finalCacheValue)

middleware/cors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strconv"
77
"strings"
88

9-
"github.com/randlabs/go-webserver/models"
9+
webserver "github.com/randlabs/go-webserver"
1010
"github.com/randlabs/go-webserver/request"
1111
)
1212

@@ -43,11 +43,11 @@ type CORSOptions struct {
4343

4444
// -----------------------------------------------------------------------------
4545

46-
func DefaultCORS() MiddlewareFunc {
46+
func DefaultCORS() webserver.MiddlewareFunc {
4747
return NewCORS(CORSOptions{})
4848
}
4949

50-
func NewCORS(opts CORSOptions) MiddlewareFunc {
50+
func NewCORS(opts CORSOptions) webserver.MiddlewareFunc {
5151
// Parse options
5252
if opts.SkipCallback == nil {
5353
opts.SkipCallback = defaultSkip
@@ -90,7 +90,7 @@ func NewCORS(opts CORSOptions) MiddlewareFunc {
9090
exposeHeaders := strings.Join(opts.ExposeHeaders, ",")
9191
maxAge := strconv.Itoa(opts.MaxAge)
9292

93-
return func(next models.HandlerFunc) models.HandlerFunc {
93+
return func(next webserver.HandlerFunc) webserver.HandlerFunc {
9494
return func(req *request.RequestContext) error {
9595
if opts.SkipCallback(req) {
9696
return next(req)

middleware/middleware.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

models/types.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

pprof.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"runtime/pprof"
77
"strings"
88

9-
"github.com/randlabs/go-webserver/middleware"
10-
"github.com/randlabs/go-webserver/models"
119
"github.com/randlabs/go-webserver/request"
1210
"github.com/valyala/fasthttp/fasthttpadaptor"
1311
)
@@ -21,7 +19,7 @@ type DebugProfilerAccessCheck func(req *request.RequestContext) bool
2119

2220
// ServeDebugProfiler adds the GO runtime profile handlers to a web server
2321
func (srv *Server) ServeDebugProfiler(
24-
basePath string, accessCheck DebugProfilerAccessCheck, middlewares ...middleware.MiddlewareFunc,
22+
basePath string, accessCheck DebugProfilerAccessCheck, middlewares ...MiddlewareFunc,
2523
) {
2624
if !strings.HasPrefix(basePath, "/") {
2725
basePath = "/" + basePath
@@ -45,7 +43,7 @@ func (srv *Server) ServeDebugProfiler(
4543
// -----------------------------------------------------------------------------
4644
// Private functions
4745

48-
func wrapProfilerHandler(handler http.Handler, accessCheck DebugProfilerAccessCheck) models.HandlerFunc {
46+
func wrapProfilerHandler(handler http.Handler, accessCheck DebugProfilerAccessCheck) HandlerFunc {
4947
fasthttpHandler := fasthttpadaptor.NewFastHTTPHandler(handler)
5048

5149
return func(req *request.RequestContext) error {
@@ -63,6 +61,6 @@ func wrapProfilerHandler(handler http.Handler, accessCheck DebugProfilerAccessCh
6361
}
6462
}
6563

66-
func wrapProfilerHandlerFunc(handler http.HandlerFunc, accessCheck DebugProfilerAccessCheck) models.HandlerFunc {
64+
func wrapProfilerHandlerFunc(handler http.HandlerFunc, accessCheck DebugProfilerAccessCheck) HandlerFunc {
6765
return wrapProfilerHandler(handler, accessCheck)
6866
}

webserver.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@ import (
1313

1414
"github.com/fasthttp/router"
1515
"github.com/randlabs/go-webserver/listener"
16-
"github.com/randlabs/go-webserver/middleware"
17-
"github.com/randlabs/go-webserver/models"
1816
"github.com/randlabs/go-webserver/request"
1917
"github.com/valyala/fasthttp"
2018
)
2119

2220
// -----------------------------------------------------------------------------
2321

22+
// ListenErrorHandler is a callback to call if an error is encountered in the network listener.
23+
type ListenErrorHandler func(srv *Server, err error)
24+
25+
// RequestErrorHandler is a callback to call if an error is encountered while processing a request.
26+
type RequestErrorHandler func(req *request.RequestContext, err error)
27+
28+
// HandlerFunc defines a function that handles a request
29+
type HandlerFunc func(req *request.RequestContext) error
30+
31+
// MiddlewareFunc defines a function that is executed when a request is received
32+
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
33+
2434
// Server is the main server object
2535
type Server struct {
2636
fastserver fasthttp.Server
@@ -29,7 +39,7 @@ type Server struct {
2939
bindPort uint16
3040
listenErrorHandler ListenErrorHandler
3141
requestErrorHandler RequestErrorHandler
32-
middlewares []middleware.MiddlewareFunc
42+
middlewares []MiddlewareFunc
3343
state int32
3444
startShutdownSignal chan struct{}
3545
shutdownCompleteSignal chan struct{}
@@ -77,18 +87,12 @@ type Options struct {
7787
RequestErrorHandler RequestErrorHandler
7888

7989
// A custom handler for 404 errors
80-
NotFoundHandler models.HandlerFunc
90+
NotFoundHandler HandlerFunc
8191

8292
// TLSConfig optionally provides a TLS configuration for use.
8393
TLSConfig *tls.Config
8494
}
8595

86-
// ListenErrorHandler is a callback to call if an error is encountered in the network listener.
87-
type ListenErrorHandler func(srv *Server, err error)
88-
89-
// RequestErrorHandler is a callback to call if an error is encountered while processing a request.
90-
type RequestErrorHandler func(req *request.RequestContext, err error)
91-
9296
// ServerFilesOptions sets the parameters to use in a ServeFiles call
9397
type ServerFilesOptions struct {
9498
// Base directory where public files are located
@@ -105,7 +109,7 @@ type ServerFilesOptions struct {
105109
AcceptByteRange bool
106110

107111
// Custom file not found handler. Defaults to the server NotFound handler.
108-
NotFoundHandler models.HandlerFunc
112+
NotFoundHandler HandlerFunc
109113
}
110114

111115
// -----------------------------------------------------------------------------
@@ -191,7 +195,7 @@ func Create(options Options) (*Server, error) {
191195
bindPort: options.Port,
192196
listenErrorHandler: options.ListenErrorHandler,
193197
requestErrorHandler: options.RequestErrorHandler,
194-
middlewares: make([]middleware.MiddlewareFunc, 0),
198+
middlewares: make([]MiddlewareFunc, 0),
195199
state: stateNotStarted,
196200
startShutdownSignal: make(chan struct{}, 1),
197201
shutdownCompleteSignal: make(chan struct{}, 1),
@@ -333,47 +337,47 @@ func (srv *Server) Stop() {
333337
}
334338

335339
// Use adds a middleware that will be executed as part of the request handler
336-
func (srv *Server) Use(middleware middleware.MiddlewareFunc) {
340+
func (srv *Server) Use(middleware MiddlewareFunc) {
337341
srv.middlewares = append(srv.middlewares, middleware)
338342
}
339343

340344
// GET adds a GET handler for the specified route
341-
func (srv *Server) GET(path string, handler models.HandlerFunc, middlewares ...middleware.MiddlewareFunc) {
345+
func (srv *Server) GET(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
342346
srv.router.Handle("GET", path, srv.createEndpointHandler(handler, middlewares...))
343347
}
344348

345349
// HEAD adds a HEAD handler for the specified route
346-
func (srv *Server) HEAD(path string, handler models.HandlerFunc, middlewares ...middleware.MiddlewareFunc) {
350+
func (srv *Server) HEAD(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
347351
srv.router.Handle("HEAD", path, srv.createEndpointHandler(handler, middlewares...))
348352
}
349353

350354
// OPTIONS adds a OPTIONS handler for the specified route
351-
func (srv *Server) OPTIONS(path string, handler models.HandlerFunc, middlewares ...middleware.MiddlewareFunc) {
355+
func (srv *Server) OPTIONS(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
352356
srv.router.Handle("OPTIONS", path, srv.createEndpointHandler(handler, middlewares...))
353357
}
354358

355359
// POST adds a POST handler for the specified route
356-
func (srv *Server) POST(path string, handler models.HandlerFunc, middlewares ...middleware.MiddlewareFunc) {
360+
func (srv *Server) POST(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
357361
srv.router.Handle("POST", path, srv.createEndpointHandler(handler, middlewares...))
358362
}
359363

360364
// PUT adds a PUT handler for the specified route
361-
func (srv *Server) PUT(path string, handler models.HandlerFunc, middlewares ...middleware.MiddlewareFunc) {
365+
func (srv *Server) PUT(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
362366
srv.router.Handle("PUT", path, srv.createEndpointHandler(handler, middlewares...))
363367
}
364368

365369
// PATCH adds a PATCH handler for the specified route
366-
func (srv *Server) PATCH(path string, handler models.HandlerFunc, middlewares ...middleware.MiddlewareFunc) {
370+
func (srv *Server) PATCH(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
367371
srv.router.Handle("PATCH", path, srv.createEndpointHandler(handler, middlewares...))
368372
}
369373

370374
// DELETE adds a DELETE handler for the specified route
371-
func (srv *Server) DELETE(path string, handler models.HandlerFunc, middlewares ...middleware.MiddlewareFunc) {
375+
func (srv *Server) DELETE(path string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
372376
srv.router.Handle("DELETE", path, srv.createEndpointHandler(handler, middlewares...))
373377
}
374378

375379
// ServeFiles adds custom filesystem handler for the specified route
376-
func (srv *Server) ServeFiles(path string, options ServerFilesOptions, middlewares ...middleware.MiddlewareFunc) error {
380+
func (srv *Server) ServeFiles(path string, options ServerFilesOptions, middlewares ...MiddlewareFunc) error {
377381

378382
// Check some options
379383
if !filepath.IsAbs(options.RootDirectory) {

webserver_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_webserver
1+
package go_webserver_test
22

33
import (
44
"fmt"
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
webserver "github.com/randlabs/go-webserver"
1516
"github.com/randlabs/go-webserver/middleware"
1617
"github.com/randlabs/go-webserver/request"
1718
)
@@ -26,11 +27,11 @@ type versionApiOutput struct {
2627

2728
func TestWebServer(t *testing.T) {
2829
//Create server
29-
srvOpts := Options{
30+
srvOpts := webserver.Options{
3031
Address: "127.0.0.1",
3132
Port: 3000,
3233
}
33-
srv, err := Create(srvOpts)
34+
srv, err := webserver.Create(srvOpts)
3435
if err != nil {
3536
t.Errorf("unable to create web server [%v]", err)
3637
return
@@ -52,7 +53,7 @@ func TestWebServer(t *testing.T) {
5253
workDir += string(os.PathSeparator)
5354
}
5455

55-
srv.ServeFiles("/", ServerFilesOptions{
56+
srv.ServeFiles("/", webserver.ServerFilesOptions{
5657
RootDirectory: workDir + "testdata/public",
5758
})
5859

0 commit comments

Comments
 (0)