Skip to content

Commit

Permalink
Merge pull request #13 from savsgio/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Sergio Andrés Virviescas Santana authored Aug 20, 2018
2 parents a6ab2d2 + c2c3d20 commit 2866697
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 206 deletions.
38 changes: 31 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ Is based on [erikdubbelboer's fasthttp fork](https://github.com/erikdubbelboer/f
- TLSEnable *(bool)*: Enable HTTPS
- CertKey *(string)*: Path of cert.key file
- CertFile *(string)*: Path of cert.pem file
- GracefulEnable *(bool)*: Start server with graceful shutdown
- GracefulShutdown *(bool)*: Start server with graceful shutdown

## Note:
`*atreugo.RequestCtx` is equal than `*fasthttp.RequestCtx`, but adding extra funtionality, so you can use
the same functions of `*fasthttp.RequestCtx`. Don't worry :smile:

## Example:

Expand All @@ -45,38 +48,31 @@ import (
)

func main() {
// Configuration for Atreugo server
config := &atreugo.Config{
Host: "0.0.0.0",
Port: 8000,
}

// New instance of atreugo server with your config
server := atreugo.New(config)

// Middlewares
fnMiddlewareOne := func(ctx *fasthttp.RequestCtx) (int, error) {
fnMiddlewareOne := func(ctx *atreugo.RequestCtx) (int, error) {
return fasthttp.StatusOK, nil
}

fnMiddlewareTwo := func(ctx *fasthttp.RequestCtx) (int, error) {
return fasthttp.StatusBadRequest, errors.New("Error message")
fnMiddlewareTwo := func(ctx *atreugo.RequestCtx) (int, error) {
// Disable this middleware if you don't want to see this error
return fasthttp.StatusBadRequest, errors.New("Error example")
}

// Register middlewares
server.UseMiddleware(fnMiddlewareOne, fnMiddlewareTwo)


// Views
server.Path("GET", "/", func(ctx *fasthttp.RequestCtx) error {
return atreugo.HTTPResponse(ctx, []byte("<h1>Atreugo Micro-Framework</h1>"))
server.Path("GET", "/", func(ctx *atreugo.RequestCtx) error {
return ctx.HTTPResponse([]byte("<h1>Atreugo Micro-Framework</h1>"))
})

server.Path("GET", "/jsonPage", func(ctx *fasthttp.RequestCtx) error {
return atreugo.JSONResponse(ctx, atreugo.JSON{"Atreugo": true})
server.Path("GET", "/jsonPage", func(ctx *atreugo.RequestCtx) error {
return ctx.JSONResponse(atreugo.JSON{"Atreugo": true})
})

// Start server
err := server.ListenAndServe()
if err != nil {
panic(err)
Expand Down
31 changes: 21 additions & 10 deletions atreugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package atreugo

import (
"fmt"
"io"
"net"
"os"
"os/signal"
Expand Down Expand Up @@ -45,20 +46,25 @@ func New(cfg *Config) *Atreugo {

func (s *Atreugo) handler(viewFn View) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
s.log.Debugf("%s %s", ctx.Method(), ctx.URI())
actx := acquireRequestCtx(ctx)
defer releaseRequestCtx(actx)

if s.cfg.LogLevel == logger.DEBUG {
s.log.Debugf("%s %s", actx.Method(), actx.URI())
}

for _, middlewareFn := range s.middlewares {
if statusCode, err := middlewareFn(ctx); err != nil {
s.log.Errorf("Msg: %v | RequestUri: %s", err, ctx.URI().String())
if statusCode, err := middlewareFn(actx); err != nil {
s.log.Errorf("Msg: %v | RequestUri: %s", err, actx.URI().String())

ctx.Error(err.Error(), statusCode)
actx.Error(err.Error(), statusCode)
return
}
}

if err := viewFn(ctx); err != nil {
if err := viewFn(actx); err != nil {
s.log.Error(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
actx.Error(err.Error(), fasthttp.StatusInternalServerError)
}
}
}
Expand All @@ -78,12 +84,12 @@ func (s *Atreugo) getListener(addr string) net.Listener {
}

func (s *Atreugo) serve(ln net.Listener) error {
protocol := "http"
schema := "http"
if s.cfg.TLSEnable {
protocol = "https"
schema = "https"
}

s.log.Infof("Listening on: %s://%s/", protocol, ln.Addr().String())
s.log.Infof("Listening on: %s://%s/", schema, ln.Addr().String())
if s.cfg.TLSEnable {
return s.server.ServeTLS(ln, s.cfg.CertFile, s.cfg.CertKey)
}
Expand Down Expand Up @@ -136,12 +142,17 @@ func (s *Atreugo) UseMiddleware(fns ...Middleware) {
s.middlewares = append(s.middlewares, fns...)
}

// SetLogOutput set log output of server
func (s *Atreugo) SetLogOutput(output io.Writer) {
s.log.SetOutput(output)
}

// ListenAndServe start Atreugo server according to the configuration
func (s *Atreugo) ListenAndServe() error {
addr := fmt.Sprintf("%s:%d", s.cfg.Host, s.cfg.Port)
ln := s.getListener(addr)

if s.cfg.GracefulEnable {
if s.cfg.GracefulShutdown {
return s.serveGracefully(ln)
}

Expand Down
Loading

0 comments on commit 2866697

Please sign in to comment.