mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
more features and fix database/mysql:jwt example
This commit is contained in:
@@ -89,7 +89,7 @@ MYSQL_HOST=localhost
|
||||
MYSQL_DATABASE=myapp
|
||||
```
|
||||
|
||||
Download the schema from [migration/myapp.sql](migration/myapp.sql) and execute it against your MySQL server instance.
|
||||
Download the schema from [migration/db.sql](migration/db.sql) and execute it against your MySQL server instance.
|
||||
|
||||
```sql
|
||||
CREATE DATABASE IF NOT EXISTS myapp DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
@@ -139,7 +139,7 @@ Testing is important. The code is written in a way that testing should be trivia
|
||||
|
||||
## Packages
|
||||
|
||||
- https://github.com/dgrijalva/jwt-go (JWT parsing)
|
||||
- https://github.com/kataras/jwt (JWT parsing)
|
||||
- https://github.com/go-sql-driver/mysql (Go Driver for MySQL)
|
||||
- https://github.com/DATA-DOG/go-sqlmock (Testing DB see [service/category_service_test.go](service/category_service_test.go))
|
||||
- https://github.com/kataras/iris (HTTP)
|
||||
|
||||
@@ -15,18 +15,19 @@ import (
|
||||
// Router accepts any required dependencies and returns the main server's handler.
|
||||
func Router(db sql.Database, secret string) func(iris.Party) {
|
||||
return func(r iris.Party) {
|
||||
j := jwt.HMAC(15*time.Minute, secret)
|
||||
|
||||
r.Use(requestid.New())
|
||||
r.Use(verifyToken(j))
|
||||
|
||||
signer := jwt.NewSigner(jwt.HS256, secret, 15*time.Minute)
|
||||
r.Get("/token", writeToken(signer))
|
||||
|
||||
verify := jwt.NewVerifier(jwt.HS256, secret).Verify(nil)
|
||||
r.Use(verify)
|
||||
// Generate a token for testing by navigating to
|
||||
// http://localhost:8080/token endpoint.
|
||||
// Copy-paste it to a ?token=$token url parameter or
|
||||
// open postman and put an Authentication: Bearer $token to get
|
||||
// access on create, update and delete endpoinds.
|
||||
|
||||
r.Get("/token", writeToken(j))
|
||||
|
||||
var (
|
||||
categoryService = service.NewCategoryService(db)
|
||||
productService = service.NewProductService(db)
|
||||
@@ -73,25 +74,19 @@ func Router(db sql.Database, secret string) func(iris.Party) {
|
||||
}
|
||||
}
|
||||
|
||||
func writeToken(j *jwt.JWT) iris.Handler {
|
||||
func writeToken(signer *jwt.Signer) iris.Handler {
|
||||
return func(ctx iris.Context) {
|
||||
claims := jwt.Claims{
|
||||
Issuer: "https://iris-go.com",
|
||||
Audience: jwt.Audience{requestid.Get(ctx)},
|
||||
Audience: []string{requestid.Get(ctx)},
|
||||
}
|
||||
|
||||
j.WriteToken(ctx, claims)
|
||||
}
|
||||
}
|
||||
|
||||
func verifyToken(j *jwt.JWT) iris.Handler {
|
||||
return func(ctx iris.Context) {
|
||||
// Allow all GET.
|
||||
if ctx.Method() == iris.MethodGet {
|
||||
ctx.Next()
|
||||
token, err := signer.Sign(claims)
|
||||
if err != nil {
|
||||
ctx.StopWithStatus(iris.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
j.Verify(ctx)
|
||||
ctx.Write(token)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
github.com/kataras/iris/v12 master
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201117050536-962ffd67721a
|
||||
github.com/mailgun/groupcache/v2 v2.1.0
|
||||
)
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// $ go build .
|
||||
|
||||
func main() {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?parseTime=true&charset=utf8mb4&collation=utf8mb4_unicode_ci",
|
||||
getenv("MYSQL_USER", "user_myapp"),
|
||||
|
||||
Reference in New Issue
Block a user