gin.BasicAuth 简单密码访问控制

13 min read

gin.BasicAuth 简单密码访问控制实例

package main

import (
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	docs "sunzhongwei.com/some_service/api_docs"
)

func main() {
	gin.SetMode(gin.ReleaseMode)

	r := gin.Default()
	docs.SwaggerInfo.Host = "api.sunzhongwei.com"
	docs.SwaggerInfo.BasePath = "/api"

	authorized := r.Group("/api-doc", gin.BasicAuth(gin.Accounts{
		"some_user": "some_password",
	}))
	authorized.GET("/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	r.Run(":8089") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}