Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danteay/gin-extended
Implementation of usefull middlewares for gin framework
https://github.com/danteay/gin-extended
Last synced: about 1 month ago
JSON representation
Implementation of usefull middlewares for gin framework
- Host: GitHub
- URL: https://github.com/danteay/gin-extended
- Owner: danteay
- Created: 2019-11-21T19:20:40.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-10T16:52:17.000Z (almost 2 years ago)
- Last Synced: 2024-06-20T15:55:16.909Z (7 months ago)
- Language: Go
- Size: 36.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Gin-Gonic Extended
This package stores usefully middlewares and libraries that could be implemented
on a Gin project## Installation
```bash
go get -u -v github.com/danteay/gin-extended
```## Content
* [Middlewares](#middlewares)
* [Authentication](#authentication)
* [SwaggerValidator](#swagger-validator)
* [Zerolog](#zerolog)
* [Libraries](#libraries)
* [Parse](#parse)
* [ParseYmlFile](#parse-yml-file)
* [ShouldParseYmlFile](#should-parse-yml-file)
* [Extras](#extras)
* [Ginrest](#ginrest)
## Middlewares
### Authentication
This middleware can authenticate request with by two different strategies; as a
`bearer` token or with a `basic` authentication schema.```go
package mainimport (
"github.com/gin-gonic/gin"
mw "github.com/danteay/gin-extended-commons/middlewares"
)func main(){
app := gin.New()app.Use(mw.Authentication())
// ....
app.Run()
}
```You can set custom configuration for this middleware:
```go
package mainimport (
"github.com/gin-gonic/gin"
mw "github.com/danteay/gin-extended-commons/middlewares"
)func main(){
app := gin.New()authConf := &mw.AuthorizationConfig{
Type: "bearer",
APIKey: "123456789",
}app.Use(mw.AuthenticationWithConfig())
// ....
app.Run()
}
```Also you can config as basic authentication.
```go
package mainimport (
"github.com/gin-gonic/gin"
mw "github.com/danteay/gin-extended-commons/middlewares"
)func main(){
app := gin.New()authConf := &mw.AuthorizationConfig{
Type: "basic",
AuthCredentials: []string{"user", "password"},
}app.Use(mw.AuthenticationWithConfig())
// ....
app.Run()
}
```Or you can define a non static validation for the authentication
```go
package mainimport (
"github.com/gin-gonic/gin"
mw "github.com/danteay/gin-extended-commons/middlewares"
)func main(){
app := gin.New()authConf := &mw.AuthorizationConfig{
Type: "bearer",
Validator: func (key string) bool {
var res bool// validate key
return res
},
}app.Use(mw.AuthenticationWithConfig())
// ....
app.Run()
}
```
### SwaggerValidator
This middleware validate the api request and response schema with the OpenApi
specification. By default the middleware search for a file called `spec.yml`
to load the API specification.```go
package mainimport (
"github.com/gin-gonic/gin"
mw "github.com/danteay/gin-extended-commons/middlewares"
)func main(){
app := gin.New()app.Use(mw.SwaggerValidator())
// ....
app.Run()
}
```Also you can specify a route to load the specification.
```go
package mainimport (
"github.com/gin-gonic/gin"
mw "github.com/danteay/gin-extended-commons/middlewares"
)func main(){
app := gin.New()app.Use(mw.SwaggerValidatorWithConfig(&mw.SwaggerValidatorConfig{
Document: "my_spec_file.yml"
}))// ....
app.Run()
}
```
### Zerolog
This middleware provides a custom IO request interface for Gin with zerolog.
```go
package mainimport (
"github.com/gin-gonic/gin"
mw "github.com/danteay/gin-extended-commons/middlewares"
)func main(){
app := gin.New()app.Use(mw.Zerolog())
// ....
app.Run()
}
```
## Libraries
### Parse
#### ParseYmlFile
Parse and marshall a yml file into a structure
```yml
# config.ymluser: "user"
pass: "pass"
``````go
package mainimport (
"fmt"
"github.com/gin-gonic/gin"
"github.com/danteay/gin-extended-commons/libs"
)type Data struct {
User string `json:"user"`
Pass string `json:"pass"`
}func main() {
data := new(Data)err := libs.ParseYmlFile("config.yml")
if err != nil {
fmt.Println(err.Error())
return
}fmt.Println(data)
}// {user pass}
```
#### ShouldParseYmlFile
Parse and marshall a yml file into a structure and panic if any error happens
```yml
# config.ymluser: "user"
pass: "pass"
``````go
package mainimport (
"fmt"
"github.com/gin-gonic/gin"
"github.com/danteay/gin-extended-commons/libs"
)type Data struct {
User string `json:"user"`
Pass string `json:"pass"`
}func main() {
data := new(Data)libs.ShouldParseYmlFile("config.yml")
fmt.Println(data)
}// {user pass}
```
## Extras
### Ginrest
This is a dependency library that is used by the middlewares to provide an
standard payload response, you can review his specification from his
[Github repo](https://github.com/danteay/ginrest), also you can use it on your own API routes to respond with this standard.