GoFrame 101: Add auth middleware
We will use rk-boot to enable auth middleware in gogf/gf framework.
Introduce rk-boot
rk-boot is a library used to create goLang micro-service with gogf/gf framework easily.
Please visit official site for doc details.
Install rk-boot
go get github.com/rookie-ninja/rk-boot/gfQuick start
Please visit rkdev.info for detailed document.
I. Create boot.yaml
boot.yaml tells rk-boot how to start gogf/gf server.
Get mofcloud’s stories in your inbox
Join Medium for free to get updates from this writer.
CommonService was enabled which contains couple of utility functions such as /rk/v1/healthy.
II. Create main.go
Add /v1/greeter API.
III. Directory hierarchy
$ tree
.
├── boot.yaml
├── go.mod
├── go.sum
└── main.go0 directories, 4 files
IV. Start main.go
$ go run main.goV. Validation
- 401
$ curl -X GET localhost:8080/rk/v1/healthy
{
"error":{
"code":401,
"status":"Unauthorized",
"message":"Missing authorization, provide one ...",
"details":[]
}
}
$ curl -X GET "localhost:8080/v1/greeter?name=rk-dev
{
"error":{
"code":401,
"status":"Unauthorized",
"message":"Missing authorization, provide one ...",
"details":[]
}
}- 200 (user and pass needs to be encoded with base64)
$ curl localhost:8080/rk/v1/healthy -H "Authorization: Basic dXNlcjpwYXNz"
{"healthy":true}
$ curl "localhost:8080/v1/greeter?name=rk-dev" -H "Authorization: Basic dXNlcjpwYXNz"
{"Message":"Hello rk-dev!"}X-API-Key
I. Modify boot.yaml
Set gf.interceptors.auth.apiKey to token.
II. Start main.go
$ go run main.goIII. Validation
- 401
$ curl -X GET localhost:8080/rk/v1/healthy
{
"error":{
"code":401,
"status":"Unauthorized",
"message":"Missing authorization, provide one ...",
"details":[]
}
}$ curl -X GET "localhost:8080/v1/greeter?name=rk-dev
{
"error":{
"code":401,
"status":"Unauthorized",
"message":"Missing authorization, provide one ...",
"details":[]
}
}
- 200
$ curl localhost:8080/rk/v1/healthy -H "X-API-Key: token"
{"healthy":true}
$ curl "localhost:8080/v1/greeter?name=rk-dev" -H "X-API-Key: token"
{"Message":"Hello rk-dev!"}Ignore auth
We can set gf.interceptors.auth.ignorePrefix with API path to ignore auth validation.
Swagger UI
In order to add Auth related inputs in Swagger UI, we need to use swag to generate swagger related config file.
Example
- main.go
- Run swag
$ swag init
$ tree
.
├── boot.yaml
├── docs
│ ├── docs.go
│ ├── swagger.json
│ └── swagger.yaml
├── go.mod
├── go.sum
└── main.go
1 directory, 7 files- boot.yaml
- Access http://localhost:8080/sw
Introduction of rk-boot
rk-boot is a library which support bootstrapping server at runtime. It is a little like spring boot way.
Why do I want it?
- Build application with unified project layout at enterprise level .
- Build API with the unified format of logging, metrics, tracing, authorization at enterprise level.
- Make application replace core dependencies quickly.
- Save learning time of writing initializing procedure of popular frameworks and libraries.
- User defined Entry for customization.
Key feature
User can bootstrap multiple Web Service with different framework with same layout of YAML. Hers is simple example where we start Gin, gRPC, Echo and GoFrame web framework in the same process.
- Install dependencies
go get github.com/rookie-ninja/rk-boot/grpc
go get github.com/rookie-ninja/rk-boot/gin
go get github.com/rookie-ninja/rk-boot/echo
go get github.com/rookie-ninja/rk-boot/gf- boot.yaml
- main.go
- Validation
# gRPC through grpc-gateway
$ curl localhost:8080/rk/v1/healthy
{"healthy":true}# Gin
$ curl localhost:8081/rk/v1/healthy
{"healthy":true}# Echo
$ curl localhost:8082/rk/v1/healthy
{"healthy":true}# GoFrame
$ curl localhost:8083/rk/v1/healthy
{"healthy":true}
Supported web frameworks
Introduction of rk-gf
Interceptor & bootstrapper designed for gogf/gf web framework.
This belongs to rk-boot family. We suggest use this lib from rk-boot.
Supported instances
All instances could be configured via YAML or Code.
User can enable anyone of those as needed! No mandatory binding!
Supported middleware
All middlewares could be configured via YAML or Code.
User can enable anyone of those as needed! No mandatory binding!










