Skip to content

Commit 6321239

Browse files
committed
feat: add the auth and context middleware
doc: update readme and license
1 parent a0f62b5 commit 6321239

File tree

17 files changed

+543
-3
lines changed

17 files changed

+543
-3
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright [2023-2033] [Tianyi Zheng]
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,10 @@ Microservice framework implemented based on Golang.
138138
└─codegen errorCode 代码生成工具
139139
```
140140

141+
## 命令行工具
142+
```shell
143+
# 查看帮助信息
144+
go run .\cmd\***\***.go --help
145+
```
141146

142147

app/pkg/code/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package code
22

3-
//go:generate codeg -type=int -doc -output ./error_code_generated.md
3+
//go:generate codeg -type=int
44
const (
55
// ErrConnectDB - 500: Init db error.
66
ErrConnectDB int = iota + 100601

app/pkg/code/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package code
2+
3+
//go:generate codeg -type=int -doc -output ./error_code_generated.md

configs/admin/admin.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ server:
2323
profiling: true # 开启性能分析, 可以通过 <host>:<port>/debug/pprof/地址查看程序栈、线程等系统信息,默认值为 true
2424
middlewares: # 中间件
2525
- recovery # panic 恢复
26-
- cors # 跨域
26+
- cors # 跨域
27+
- context

gmicro/code/base.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package code
2+
3+
//go:generate codeg -type=int
4+
//go:generate codeg -type=int -doc -output ./error_code_generated.md
5+
6+
// Common: basic errors.
7+
// Code must start with 1xxxxx.
8+
const (
9+
// ErrSuccess - 200: OK.
10+
ErrSuccess = iota + 100001
11+
12+
// ErrUnknown - 500: Internal server error.
13+
ErrUnknown
14+
15+
// ErrBind - 400: Error occurred while binding the request body to the struct.
16+
ErrBind
17+
18+
// ErrValidation - 400: Validation failed.
19+
ErrValidation
20+
21+
// ErrTokenInvalid - 401: Token invalid.
22+
ErrTokenInvalid
23+
24+
// ErrPageNotFound - 404: Page not found.
25+
ErrPageNotFound
26+
)
27+
28+
// common: database errors.
29+
const (
30+
// ErrDatabase - 500: Database error.
31+
ErrDatabase int = iota + 100101
32+
)
33+
34+
// common: authorization and authentication errors.
35+
const (
36+
// ErrEncrypt - 401: Error occurred while encrypting the user password.
37+
ErrEncrypt int = iota + 100201
38+
39+
// ErrSignatureInvalid - 401: Signature is invalid.
40+
ErrSignatureInvalid
41+
42+
// ErrExpired - 401: Token expired.
43+
ErrExpired
44+
45+
// ErrInvalidAuthHeader - 401: Invalid authorization header.
46+
ErrInvalidAuthHeader
47+
48+
// ErrMissingHeader - 401: The `Authorization` header was empty.
49+
ErrMissingHeader
50+
51+
// ErrPasswordIncorrect - 401: Password was incorrect.
52+
ErrPasswordIncorrect
53+
54+
// PermissionDenied - 403: Permission denied.
55+
ErrPermissionDenied
56+
)
57+
58+
// common: encode/decode errors.
59+
const (
60+
// ErrEncodingFailed - 500: Encoding failed due to an error with the data.
61+
ErrEncodingFailed int = iota + 100301
62+
63+
// ErrDecodingFailed - 500: Decoding failed due to an error with the data.
64+
ErrDecodingFailed
65+
66+
// ErrInvalidJSON - 500: Data is not valid JSON.
67+
ErrInvalidJSON
68+
69+
// ErrEncodingJSON - 500: JSON data could not be encoded.
70+
ErrEncodingJSON
71+
72+
// ErrDecodingJSON - 500: JSON data could not be decoded.
73+
ErrDecodingJSON
74+
75+
// ErrInvalidYaml - 500: Data is not valid Yaml.
76+
ErrInvalidYaml
77+
78+
// ErrEncodingYaml - 500: Yaml data could not be encoded.
79+
ErrEncodingYaml
80+
81+
// ErrDecodingYaml - 500: Yaml data could not be decoded.
82+
ErrDecodingYaml
83+
)

gmicro/code/code.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package code
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/CoderI421/gframework/pkg/common/util/sliceutil"
7+
"github.com/CoderI421/gframework/pkg/errors"
8+
)
9+
10+
type ErrCode struct {
11+
//错误码
12+
C int
13+
14+
//http的状态码
15+
HTTP int
16+
17+
//扩展字段
18+
Ext string
19+
20+
//引用文档
21+
Ref string
22+
}
23+
24+
func (e ErrCode) HTTPStatus() int {
25+
return e.HTTP
26+
}
27+
28+
func (e ErrCode) String() string {
29+
return e.Ext
30+
}
31+
32+
func (e ErrCode) Reference() string {
33+
return e.Ref
34+
}
35+
36+
func (e ErrCode) Code() int {
37+
if e.C == 0 {
38+
return http.StatusInternalServerError
39+
}
40+
return e.C
41+
}
42+
43+
func register(code int, httpStatus int, message string, refs ...string) {
44+
found := sliceutil.Find([]int{200, 400, 401, 403, 404, 500}, httpStatus)
45+
46+
if !found {
47+
panic("http code not in `200,400,401,403,404,500`")
48+
}
49+
var ref string
50+
if len(refs) > 0 {
51+
ref = refs[0]
52+
}
53+
coder := ErrCode{
54+
C: code,
55+
HTTP: httpStatus,
56+
Ext: message,
57+
Ref: ref,
58+
}
59+
errors.MustRegister(coder)
60+
}
61+
62+
var _ errors.Coder = (*ErrCode)(nil)

gmicro/code/code_generated.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gmicro/code/error_code_generated.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 错误码
2+
3+
!!系统错误码列表,由 `codegen -type=int -doc` 命令生成,不要对此文件做任何更改。
4+
5+
## 功能说明
6+
7+
如果返回结果中存在 `code` 字段,则表示调用 API 接口失败。例如:
8+
9+
```json
10+
{
11+
"code": 100101,
12+
"message": "Database error"
13+
}
14+
```
15+
16+
上述返回中 `code` 表示错误码,`message` 表示该错误的具体信息。每个错误同时也对应一个 HTTP 状态码,比如上述错误码对应了 HTTP 状态码 500(Internal Server Error)。
17+
18+
## 错误码列表
19+
20+
系统支持的错误码列表如下:
21+
22+
| Identifier | Code | HTTP Code | Description |
23+
| ---------- | ---- | --------- | ----------- |
24+
| ErrDatabase | 100101 | 500 | Database error |
25+
| ErrEncrypt | 100201 | 401 | Error occurred while encrypting the user password |
26+
| ErrSignatureInvalid | 100202 | 401 | Signature is invalid |
27+
| ErrExpired | 100203 | 401 | Token expired |
28+
| ErrInvalidAuthHeader | 100204 | 401 | Invalid authorization header |
29+
| ErrMissingHeader | 100205 | 401 | The `Authorization` header was empty |
30+
| ErrPasswordIncorrect | 100206 | 401 | Password was incorrect |
31+
| ErrPermissionDenied | 100207 | 403 | Permission denied |
32+
| ErrEncodingFailed | 100301 | 500 | Encoding failed due to an error with the data |
33+
| ErrDecodingFailed | 100302 | 500 | Decoding failed due to an error with the data |
34+
| ErrInvalidJSON | 100303 | 500 | Data is not valid JSON |
35+
| ErrEncodingJSON | 100304 | 500 | JSON data could not be encoded |
36+
| ErrDecodingJSON | 100305 | 500 | JSON data could not be decoded |
37+
| ErrInvalidYaml | 100306 | 500 | Data is not valid Yaml |
38+
| ErrEncodingYaml | 100307 | 500 | Yaml data could not be encoded |
39+
| ErrDecodingYaml | 100308 | 500 | Yaml data could not be decoded |
40+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package middlewares
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
// AuthStrategy 认证策略
6+
type AuthStrategy interface {
7+
// AuthFunc 相当于是实现了一个名为 AuthFunc 的中间件
8+
AuthFunc() gin.HandlerFunc
9+
}
10+
11+
// AuthOperator 认证实体工厂类
12+
type AuthOperator struct {
13+
strategy AuthStrategy
14+
}
15+
16+
func (ao *AuthOperator) SetStrategy(s AuthStrategy) {
17+
ao.strategy = s
18+
}
19+
20+
// AuthFunc 返回一个 gin.HandlerFunc 中间件接口函数
21+
func (ao *AuthOperator) AuthFunc() gin.HandlerFunc {
22+
return ao.strategy.AuthFunc()
23+
}

0 commit comments

Comments
 (0)