81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
|
|
package handler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"myschools.me/campus/campus-core/campus"
|
||
|
|
"myschools.me/campus/demo-2/consul"
|
||
|
|
"myschools.me/campus/demo-2/tools"
|
||
|
|
)
|
||
|
|
|
||
|
|
func HealthCheck(c *gin.Context) {
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, nil)
|
||
|
|
}
|
||
|
|
|
||
|
|
func getPort(key string) (string, error) {
|
||
|
|
client, err := consul.New()
|
||
|
|
if err != nil {
|
||
|
|
fmt.Println(err.Error())
|
||
|
|
}
|
||
|
|
service, _, err := client.Health().Service(key, "", true, nil)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
fmt.Println("Service", service[0].Service.Address, "port", service[0].Service.Port)
|
||
|
|
if len(service) > 0 {
|
||
|
|
return service[0].Service.Address + ":" + fmt.Sprintf("%v", service[0].Service.Port), nil
|
||
|
|
} else {
|
||
|
|
return "", nil
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
func UserLogin(c *gin.Context) {
|
||
|
|
username, _ := c.GetQuery("username")
|
||
|
|
pwd, _ := c.GetQuery("password")
|
||
|
|
str := c.GetHeader("Authorization")
|
||
|
|
reqid := campus.NewRequestID(nil)
|
||
|
|
|
||
|
|
if username != "admin" || pwd != "admin" {
|
||
|
|
campus.RespBadRequest(c, "账户密码错误", errors.New("账户密码错误"), &reqid)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
url, err := getPort("demo1")
|
||
|
|
url = "http://" + url + "/token/verify"
|
||
|
|
fmt.Println("URL:", url)
|
||
|
|
if err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
headerMap := make(map[string]string, 0)
|
||
|
|
headerMap["Authorization"] = str
|
||
|
|
headerMap["Content-Type"] = "application/json"
|
||
|
|
fmt.Println("Authorization:" + str)
|
||
|
|
err2, bytes := tools.GetPost(url, "get", headerMap, nil)
|
||
|
|
if err2 != nil {
|
||
|
|
panic(err2)
|
||
|
|
}
|
||
|
|
|
||
|
|
base := struct {
|
||
|
|
Code int `json:"code"`
|
||
|
|
Msg string `json:"msg"`
|
||
|
|
Requestid string `json:"requestid"`
|
||
|
|
Data struct{}
|
||
|
|
}{}
|
||
|
|
|
||
|
|
_ = json.Unmarshal(bytes, &base)
|
||
|
|
|
||
|
|
if base.Code == 0 {
|
||
|
|
campus.RespSuccess(c, "密码正确,请求头正确", "欢迎登录", &reqid)
|
||
|
|
return
|
||
|
|
} else {
|
||
|
|
campus.RespBadRequest(c, "请求头错误", errors.New("请求头错误"), &reqid)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|