# README
rebar v2
V2 rebar introduced a few improvements
- Simpler subfolder and package structure
- Replaced Gorilla Mux with Gin
- Better Logger middleware with zap
- Improved graceful shutdown with cancelable context
- Updated time duration based option names
Getting started
go get github.com/masonhubco/rebar/v2
Examples
Let's start with a minimum example:
logger, err := rebar.NewStandardLogger()
if err != nil {
log.Fatal("ERROR:", err)
}
app := rebar.New(rebar.Options{
Environment: rebar.Development,
Port: "3000",
Logger: logger,
})
app.Router.Use(middleware.Logger(logger))
app.Router.Use(gin.Recovery())
apiGroup := app.Router.Group("/api")
{
apiGroup.Use(middleware.BasicJWT("test-system-token"))
// a simple GET request handler
apiGroup.GET("/status", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "up",
"redis": "connected",
})
})
// request can be aborted with a helper function rebar.AbortWithError()
apiGroup.DELETE("/another_example", func(c *gin.Context) {
if c.Query("required_parameter") == "" {
rebar.AbortWithError(c, http.StatusBadRequest,
errors.New("required_parameter is not provided"))
return
}
c.JSON(http.StatusOK, gin.H{
"some": "data",
})
})
}
if err := app.Run(); err != nil {
log.Fatal("ERROR:", err)
}
And more examples:
- standard: basic and simple setup
- graphql: graphQL query and playground integrated
- graceful: gracefully shut down worker or other long running goroutines
Each example app was created as separate go module. To run an example (ie. standard
):
cd examples/standard
make run
Configuration
type Options struct {
// Environment defaults to development. Possible value could be development,
// test, staging, integration, sandbox and production. When it's set to
// development, it activates Gin's debug mode, test triggers test mode, and
// everything else maps to release mode
Environment string
// Port defaults to 3000. It's the port rebar http server will listen to.
Port string
// Logger is used throughout rebar for writing logs. It accepts an instance
// of zap logger.
Logger Logger
// WriteTimeout defaults to 15 seconds. It maps to http.Server's WriteTimeout.
WriteTimeout time.Duration
// ReadTimeout defaults to 15 seconds. It maps to http.Server's ReadTimeout.
ReadTimeout time.Duration
// IdleTimeout defaults to 60 seconds. It maps to http.Server's IdleTimeout.
IdleTimeout time.Duration
// ShutDownWait defaults to 30 seconds. It tells the server how long it has
// to gracefully shutdown
ShutDownWait time.Duration
// StopOnProcessorStartFailure will prevent the server from starting if any attached processors fail to start
StopOnProcessorStartFailure bool
}
Middleware
middleware.ForceSSL
middleware.I18n
middleware.Logger
middleware.Recovery
middleware.Transaction
middleware.BaiscJWT
Examples for rebar middleware.
Upgrade from v0
to v2
v0 | v2 | |
---|---|---|
|
|
|
|
|
|
|
|
|
Another example for gracefully shutting down worker for long running goroutines.
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
I18n will construct a new internationalized ValidationError.
No description provided by the author
No description provided by the author
No description provided by the author
New creates a new Rebar instance.
No description provided by the author
No description provided by the author
No description provided by the author
Translate will return the translated key based on the accept string (i.e.
No description provided by the author
No description provided by the author
No description provided by the author
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
Options is the set of custom options you'd like to use to start up this web server.
Rebar is the MasonHub Base App.
No description provided by the author
# Interfaces
No description provided by the author
No description provided by the author
Processor interface defines the necessary functions to start and gracefully stop any sub process attached to the Rebar instance.