Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Build RESTful API service in golang using gin-gonic framework.

License

Notifications You must be signed in to change notification settings

userfhy/gin-web-admin

Repository files navigation

Gin Web Admin

Web admin frontend project

web-admin-frontend

First Run

cp conf/app.toml.example conf/app.toml
go mod download
go run main.go

Cross Compile

Windows

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -a -ldflags '-extldflags "-static"' .

Linux

CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' .

Logs

$ go run main.go 
2020年06月28日 15:42:40 [info] Redis connected 192.168.3.5:6379 DB: 0
2020年06月28日 15:42:40 PONG
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env: export GIN_MODE=release
 - using code: gin.SetMode(gin.ReleaseMode)
INFO[2025年03月16日 14:18:39] Redis connected 192.168.1.128:6379 DB: 3 caller="main.init.0:53" service=sse-service
INFO[2025年03月16日 14:18:39] PONG caller="gin-web-admin/utils/gredis.Setup:49" service=sse-service
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env: export GIN_MODE=release
 - using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] POST /v1/api/login --> gin-web-admin/app/controllers/v1/auth.UserLogin (4 handlers)
[GIN-debug] POST /v1/api/refresh_token --> gin-web-admin/app/controllers/v1/auth.RefreshAccessToken (4 handlers)
[GIN-debug] GET /v1/api/user --> gin-web-admin/app/controllers/v1/user.GetUsers (7 handlers)
[GIN-debug] PUT /v1/api/user/logout --> gin-web-admin/app/controllers/v1/auth.UserLogout (7 handlers)
[GIN-debug] PUT /v1/api/user/change_password --> gin-web-admin/app/controllers/v1/auth.ChangePassword (7 handlers)
[GIN-debug] GET /v1/api/user/logged_in --> gin-web-admin/app/controllers/v1/auth.GetLoggedInUser (7 handlers)
[GIN-debug] GET /v1/api/role --> gin-web-admin/app/controllers/v1/role.GetRoles (7 handlers)
[GIN-debug] POST /v1/api/role --> gin-web-admin/app/controllers/v1/role.CreateRole (7 handlers)
[GIN-debug] PUT /v1/api/role/:role_id --> gin-web-admin/app/controllers/v1/role.UpdateRole (7 handlers)
[GIN-debug] DELETE /v1/api/role/:role_id --> gin-web-admin/app/controllers/v1/role.DeleteRole (7 handlers)
[GIN-debug] GET /v1/api/casbin --> gin-web-admin/app/controllers/v1/casbin.GetCasbinList (7 handlers)
[GIN-debug] POST /v1/api/casbin --> gin-web-admin/app/controllers/v1/casbin.CreateCasbin (7 handlers)
[GIN-debug] PUT /v1/api/casbin/:id --> gin-web-admin/app/controllers/v1/casbin.UpdateCasbin (7 handlers)
[GIN-debug] DELETE /v1/api/casbin/:id --> gin-web-admin/app/controllers/v1/casbin.DeleteCasbin (7 handlers)
[GIN-debug] GET /v1/api/sys/router --> gin-web-admin/app/controllers/v1/sys.GetRouterList (7 handlers)
[GIN-debug] GET /v1/api/sys/menu_list --> gin-web-admin/app/controllers/v1/sys.GetMenuList (7 handlers)
[GIN-debug] POST /v1/api/test/ping --> gin-web-admin/app/controllers/v1/index.Ping (5 handlers)
[GIN-debug] GET /v1/api/test/ping --> gin-web-admin/app/controllers/v1/index.Ping (5 handlers)
[GIN-debug] GET /v1/api/test/font --> gin-web-admin/app/controllers/v1/index.Test (5 handlers)
[GIN-debug] GET /v1/api/test/sse --> gin-web-admin/routers.InitTestRouter.func1 (5 handlers)
[GIN-debug] GET /v1/api/test/events --> gin-web-admin/common/sse.(*sseImpl).Handler.func1 (5 handlers)
[GIN-debug] POST /v1/api/test/send --> gin-web-admin/app/controllers/v1/index.SendStream (5 handlers)
[GIN-debug] GET /v1/api/test/count --> gin-web-admin/app/controllers/v1/index.SSEClientCount (5 handlers)
[GIN-debug] POST /v1/api/report --> gin-web-admin/app/controllers/v1/report.Report (5 handlers)
[GIN-debug] GET /swagger --> gin-web-admin/routers.InitSwaggerRouter.func1 (4 handlers)
[GIN-debug] GET /swagger/*any --> github.com/swaggo/gin-swagger.CustomWrapHandler.func1 (4 handlers)
INFO[2025年03月16日 14:18:39] start http server listening :8081 caller="runtime.main:283" service=sse-service
INFO[2025年03月16日 14:18:39] Actual pid is 2969363 caller="runtime.main:283" service=sse-service

Swagger Docs

Preview

swagger_preview

swagger_preview_2

Access BASE_URL/swagger/index.html view docs.

Please check the instructions for use. gin-swagger

Generate

$ swag init
2019年08月22日 16:17:11 Generate swagger docs....
2019年08月22日 16:17:11 Generate general API Info, search dir:./
2019年08月22日 16:17:11 create docs.go at docs/docs.go
2019年08月22日 16:17:11 create swagger.json at docs/swagger.json
2019年08月22日 16:17:11 create swagger.yaml at docs/swagger.yaml

Parameter Verification

1.Defining structure

use validator.v10 Docs: validator.v10

type Page struct {
 P uint `json:"p" form:"p" validate:"required,numeric,min=1"`
 N uint `json:"n" form:"n" validate:"required,numeric,min=1"`
}

2.Binding Request Parameters

 var p Page
 if err := c.ShouldBindQuery(&p); err != nil {
 return err, "参数绑定失败,请检查传递参数类型!", 0, 0
 }

3.Verify Binding Parameters

 err, parameterErrorStr := common.CheckBindStructParameter(p, c)

Complete example

type Page struct {
 P uint `json:"p" form:"p" validate:"required,numeric,min=1"`
 N uint `json:"n" form:"n" validate:"required,numeric,min=1"`
}
// GetPage get page parameters
func GetPage(c *gin.Context) (error, string, int, int) {
 currentPage := 0
 // 绑定 query 参数到结构体
 var p Page
 if err := c.ShouldBindQuery(&p); err != nil {
 return err, "参数绑定失败,请检查传递参数类型!", 0, 0
 }
 // 验证绑定结构体参数
 err, parameterErrorStr := common.CheckBindStructParameter(p, c)
 if err != nil {
 return err, parameterErrorStr, 0, 0
 }
 page := com.StrTo(c.DefaultQuery("p", "0")).MustInt()
 limit := com.StrTo(c.DefaultQuery("n", "15")).MustInt()
 
 if page > 0 {
 currentPage = (page - 1) * limit
 }
 return nil, "", currentPage, limit
}

Features

License

MIT

About

Build RESTful API service in golang using gin-gonic framework.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

AltStyle によって変換されたページ (->オリジナル) /