Skip to content

Commit

Permalink
Add lab259/cors and tests for middlewares (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemfp authored Jul 29, 2019
1 parent 3c1fb89 commit 0df3165
Show file tree
Hide file tree
Showing 10 changed files with 598 additions and 29 deletions.
11 changes: 3 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ $(EXAMPLES): %:
@:

run:
@if [ ! -z "$(EXAMPLE)" ]; then \
go run ./examples/$(EXAMPLE); \
else \
echo "Usage: make [$(EXAMPLES)] run"; \
echo "The environment variable \`EXAMPLE\` is not defined."; \
fi
@test ! -z "$(EXAMPLE)" && go run ./examples/$(EXAMPLE) || echo "Usage: make [$(EXAMPLES)] run"

build:
@test -d ./examples && $(foreach example,$(EXAMPLES),go build "-ldflags=$(LDFLAGS)" -o ./bin/$(example) -v ./examples/$(example) &&) :
Expand Down Expand Up @@ -42,9 +37,9 @@ plot-mem:

coverage-ci:
@mkdir -p $(COVERDIR)
@ginkgo -r -covermode=count --cover --trace ./
@ginkgo -r -covermode=count --cover --trace ./...
@echo "mode: count" > "${COVERAGEFILE}"
@find . -type f -name *.coverprofile -exec grep -h -v "^mode:" {} >> "${COVERAGEFILE}" \; -exec rm -f {} \;
@find . -type f -name '*.coverprofile' -exec cat {} \; -exec rm -f {} \; | grep -h -v "^mode:" >> ${COVERAGEFILE}

coverage: coverage-ci
@sed -i -e "s|_$(CURDIR)/|./|g" "${COVERAGEFILE}"
Expand Down
8 changes: 0 additions & 8 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ func (app *Application) Name() string {
return app.Configuration.Name
}

func (app *Application) LoadConfiguration() (interface{}, error) {
return nil, nil
}

func (app *Application) ApplyConfiguration(interface{}) error {
return nil
}

func (app *Application) Restart() error {
if err := app.Stop(); err != nil {
return err
Expand Down
72 changes: 72 additions & 0 deletions application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"sync"
"time"

"github.com/lab259/go-rscsrv"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -34,6 +36,43 @@ var _ = Describe("Hermes", func() {
done <- true
}, 1)

It("should start and stop a app with services", func(done Done) {
var serviceA service
var serviceB service

serviceStarter := rscsrv.QuietServiceStarter(&serviceA, &serviceB)
Expect(serviceStarter.Start()).To(Succeed())

Expect(serviceA.running).To(BeTrue())
Expect(serviceB.running).To(BeTrue())

app := NewApplication(ApplicationConfig{
Name: "Testing",
ServiceStarter: serviceStarter,
HTTP: FasthttpServiceConfiguration{
Bind: ":0",
},
}, NewRouter(RouterConfig{}))
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer GinkgoRecover()

wg.Done()
Expect(app.Start()).To(BeNil())
wg.Done()
}()
wg.Wait()
time.Sleep(time.Millisecond * 500)
wg.Add(1)
Expect(app.Stop()).To(BeNil())
wg.Wait()

Expect(serviceA.running).To(BeFalse())
Expect(serviceB.running).To(BeFalse())
done <- true
}, 1)

It("should restart a app", func(done Done) {
app := NewApplication(ApplicationConfig{
Name: "Testing",
Expand Down Expand Up @@ -98,3 +137,36 @@ var _ = Describe("Hermes", func() {
})
})
})

type service struct {
running bool
}

func (service *service) Name() string {
return "Service A"
}

func (service *service) LoadConfiguration() (interface{}, error) {
return nil, nil
}

func (service *service) ApplyConfiguration(interface{}) error {
return nil
}

func (service *service) Restart() error {
if err := service.Stop(); err != nil {
return err
}
return service.Start()
}

func (service *service) Start() error {
service.running = true
return nil
}

func (service *service) Stop() error {
service.running = false
return nil
}
6 changes: 1 addition & 5 deletions fasthttp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ func (service *FasthttpService) Start() error {
return service.Server.ListenAndServe(service.Configuration.Bind)
}

return service.Server.ListenAndServeTLS(
service.Configuration.Bind,
service.Configuration.TLS.CertFile,
service.Configuration.TLS.KeyFile,
)
return service.Server.ListenAndServeTLS(service.Configuration.Bind, service.Configuration.TLS.CertFile, service.Configuration.TLS.KeyFile)
}

// Stop closes the listener and waits the `Start` to stop.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/lab259/hermes
go 1.12

require (
github.com/AdhityaRamadhanus/fasthttpcors v0.0.0-20170121111917-d4c07198763a
github.com/jamillosantos/macchiato v0.0.0-20171220130318-3be045cc5033
github.com/klauspost/compress v1.5.0 // indirect
github.com/klauspost/cpuid v1.2.1 // indirect
github.com/lab259/cors v0.1.0
github.com/lab259/errors/v2 v2.2.0
github.com/lab259/go-rscsrv v0.2.1
github.com/lab259/rlog v2.0.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/AdhityaRamadhanus/fasthttpcors v0.0.0-20170121111917-d4c07198763a h1:XVdatQFSP2YhJGjqLLIfW8QBk4loz/SCe/PxkXDiW+s=
github.com/AdhityaRamadhanus/fasthttpcors v0.0.0-20170121111917-d4c07198763a/go.mod h1:C0A1KeiVHs+trY6gUTPhhGammbrZ30ZfXRW/nuT7HLw=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -31,6 +29,8 @@ github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM52
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/lab259/cors v0.1.0 h1:0806vcp/WBZBqwPRBA6sowKxE6/Mf6IekdWR+qC5tSQ=
github.com/lab259/cors v0.1.0/go.mod h1:irvlJlQvQX/3L0ouMuvV4XNMSKP7a1+45aexLgqnojQ=
github.com/lab259/errors/v2 v2.2.0 h1:I2YKNMygf9LiBsyt0RkRRRFaVFUqSGHBzsG7Pe21zKk=
github.com/lab259/errors/v2 v2.2.0/go.mod h1:bcuh1ha3APn7gzh8k1QMzuFXpqfBplbVKyClNpZ+H6U=
github.com/lab259/go-rscsrv v0.2.1 h1:/crfSH3rZXUTfqNBw+B1qMbkiuRN5Swucb10nmIV1Os=
Expand Down
10 changes: 5 additions & 5 deletions middlewares/cors_middleware.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package middlewares

import (
cors "github.com/AdhityaRamadhanus/fasthttpcors"
"github.com/lab259/cors"
"github.com/lab259/hermes"
"github.com/valyala/fasthttp"
)

func DefaultCorsMiddleware() hermes.Middleware {
return wrapCorsMiddleware(cors.DefaultHandler())
return wrapCorsMiddleware(cors.Default())
}

func NewCorsMiddleware(options cors.Options) hermes.Middleware {
return wrapCorsMiddleware(cors.NewCorsHandler(options))
return wrapCorsMiddleware(cors.New(options))
}

func wrapCorsMiddleware(withCors *cors.CorsHandler) hermes.Middleware {
func wrapCorsMiddleware(withCors *cors.Cors) hermes.Middleware {
return func(req hermes.Request, res hermes.Response, next hermes.Handler) hermes.Result {
canContinue := false
withCors.CorsMiddleware(func(ctx *fasthttp.RequestCtx) {
withCors.Handler(func(ctx *fasthttp.RequestCtx) {
canContinue = true
})(req.Raw())
if canContinue {
Expand Down
Loading

0 comments on commit 0df3165

Please sign in to comment.