Skip to content

Commit

Permalink
Merge pull request #10 from OTOT-dev/add-info-240323
Browse files Browse the repository at this point in the history
docs: 补充一些信息
  • Loading branch information
xudaqian1 authored Mar 24, 2024
2 parents 8955704 + 8c09fae commit c44891e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ jobs:
- name: Install
run: go install github.com/swaggo/swag/cmd/swag@latest

# 生成swag信息
# build项目
- name: Build
run: swag init && go build
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

基于属性的权限验证(ABAC,Attribute-Based Access Control)的微服务,为其他业务服务提供权限认证。

## 参考
https://github.com/marmotedu/iam/tree/v1.0.0

- [go swagg api文档](https://github.com/swaggo/swag/blob/master/README_zh-CN.md#%E5%A3%B0%E6%98%8E%E5%BC%8F%E6%B3%A8%E9%87%8A%E6%A0%BC%E5%BC%8F)

## 快速开始

Expand All @@ -17,6 +14,22 @@ https://github.com/marmotedu/iam/tree/v1.0.0
air
```

- 查看接口文档
> air启动后需要过一段时间才可以终止程序(立即`CTRL+C`后,程序可能仍然运行)。
- 获取/查看接口文档

拉取项目后,安装[go swag](https://github.com/swaggo/gin-swagger)组件,执行以下命令生成swag文档。

```sh
swag init
```

然后可以**在debug模式下**前往http://<BaseUrl>/swagger/index.html查看。



## 参考

- [企业级的 Go 语言实战项目:认证和授权系统](https://github.com/marmotedu/iam/tree/v1.0.0)

跑起项目后,前往http://<BaseUrl>/swagger/index.html查看。
- [go swag api文档](https://github.com/swaggo/swag/blob/master/README_zh-CN.md#%E5%A3%B0%E6%98%8E%E5%BC%8F%E6%B3%A8%E9%87%8A%E6%A0%BC%E5%BC%8F)
30 changes: 20 additions & 10 deletions router/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"auth-server/config"
"auth-server/docs"
"auth-server/middleware"
"strconv"

swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"strconv"

"github.com/gin-gonic/contrib/sessions"

Expand All @@ -16,10 +17,17 @@ import (
)

var (
apiUser api.UserApi
apiAuth api.AuthApi
apiUser api.UserApi
)

var (
authRoutesPrefix = "/auth"
serviceRoutesPrefix = "/api/v1"
)

var sessionName = "sid"

func InitRouter() {
//是否开启debug模式
if !config.DebugMode {
Expand All @@ -29,28 +37,30 @@ func InitRouter() {
engine := gin.New()
engine.Use(gin.Recovery())

engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
docs.SwaggerInfo.BasePath = "/api/v1"
if config.DebugMode {
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
docs.SwaggerInfo.BasePath = authRoutesPrefix
}

// session 设置
store := sessions.NewCookieStore([]byte(config.SessionSecret))
store.Options(sessions.Options{
Path: "/",
MaxAge: config.SessionExpire, // 设置超时时间为一个小时
})
engine.Use(sessions.Sessions("sid", store))
engine.Use(sessions.Sessions(sessionName, store))

// 日志设置
engine.Use(middleware.LogMiddleware())

// 登陆认证相关路由
authRouterGroup := engine.Group("/auth")
authRouterGroup := engine.Group(authRoutesPrefix)
authRouter(authRouterGroup)
// 用户登陆
userRouterGroup := engine.Group("/api/v1")
userRouterGroup.Use(middleware.SessionAuth())
// 业务路由
serviceRouterGroup := engine.Group(serviceRoutesPrefix)
serviceRouterGroup.Use(middleware.SessionAuth())
userRouter(serviceRouterGroup)

userRouter(userRouterGroup)
port := config.ServerPort
runParams := config.ServerHost + ":" + strconv.Itoa(port)
log.Println("master server at ", runParams)
Expand Down

0 comments on commit c44891e

Please sign in to comment.