This commit is contained in:
fanxb 2023-03-13 18:03:08 +08:00
parent 56580d4413
commit 5db5691710
6 changed files with 36 additions and 7 deletions

View File

@ -1,11 +1,21 @@
package controller package controller
import "github.com/gin-gonic/gin" import (
"encoding/json"
"github.com/gin-gonic/gin"
"qieziGo/util"
)
var base = `/application` var base = `/application`
type test struct {
aa string
bb string
}
func sign(c *gin.Context) { func sign(c *gin.Context) {
c.String(200, `ok`) data, _ := json.Marshal(test{"asdf", "asdf"})
util.Success(c, data)
} }
func visit(c *gin.Context) { func visit(c *gin.Context) {
@ -20,7 +30,7 @@ func download(c *gin.Context) {
} }
func LoadApplication(e *gin.Engine) { func LoadApplication(e *gin.RouterGroup) {
e.POST(base+"/sign", sign). e.POST(base+"/sign", sign).
GET(base+"/visit", visit). GET(base+"/visit", visit).
POST(base+"/check", check). POST(base+"/check", check).

View File

@ -11,6 +11,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.0 // indirect github.com/goccy/go-json v0.10.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.2 // indirect github.com/leodido/go-urn v1.2.2 // indirect

View File

@ -18,6 +18,8 @@ github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyh
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA= github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=

View File

@ -11,7 +11,8 @@ import (
func main() { func main() {
r := gin.Default() r := gin.Default()
r.Use(middleware.Auth()) r.Use(middleware.Auth())
controller.LoadApplication(r) v1 := r.Group("/qiezi/api")
controller.LoadApplication(v1)
if err := r.Run(":8080"); err != nil { if err := r.Run(":8080"); err != nil {
fmt.Printf("start service error:%v\n", err) fmt.Printf("start service error:%v\n", err)
} }

View File

@ -2,7 +2,6 @@ package middleware
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http"
"time" "time"
) )
@ -15,8 +14,7 @@ func Auth() gin.HandlerFunc {
if ok { if ok {
context.Next() context.Next()
} else { } else {
context.Status(http.StatusUnauthorized) context.String(401, "无访问权限")
context.Writer.WriteString(`无访问权限`)
context.Abort() context.Abort()
} }
ok = !ok ok = !ok

View File

@ -0,0 +1,17 @@
package util
import "github.com/gin-gonic/gin"
type result struct {
Code int
Message string
Data any
}
func Success(ctx *gin.Context, data any) {
ctx.JSON(200, result{200, "success", data})
}
func Fail(ctx *gin.Context) {
ctx.JSON(200, result{0, "fail", nil})
}