#3939 用户退出,通知pipeline

Merged
zouap merged 3 commits from fix-3790 into V20230410 1 year ago
  1. +3
    -1
      modules/setting/setting.go
  2. +6
    -0
      routers/user/auth.go
  3. +52
    -0
      routers/user/pipeline.go

+ 3
- 1
modules/setting/setting.go View File

@@ -114,6 +114,7 @@ var (
AppName string
MLOPS bool
MlopsHost string
MlopsToken string
AppURL string
AppSubURL string
AppSubURLDepth int // Number of slashes
@@ -1077,7 +1078,8 @@ func NewContext() {
AppSubURLDepth = strings.Count(AppSubURL, "/")

MLOPS = Cfg.Section("").Key("MLOPS").MustBool(false)
MlopsHost = Cfg.Section("").Key("MLOPS_HOST").MustString(AppSubURL)
MlopsHost = Cfg.Section("").Key("MLOPS_HOST").MustString("")
MlopsToken = Cfg.Section("").Key("MLOPS_TOKEN").MustString("")
// Check if Domain differs from AppURL domain than update it to AppURL's domain
// TODO: Can be replaced with url.Hostname() when minimal GoLang version is 1.8
urlHostname := strings.SplitN(appURL.Host, ":", 2)[0]


+ 6
- 0
routers/user/auth.go View File

@@ -1318,11 +1318,17 @@ func SignOut(ctx *context.Context) {
Name: "logout",
Data: ctx.Session.ID(),
})

go SignOutNotify(ctx.User.ID)
}
HandleSignOut(ctx)
ctx.Redirect(setting.AppSubURL + "/")
}

func SignOutNotify(uid int64) {
NotifyPipelineUserLogout(uid)
}

// SignUp render the register page
func SignUp(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("sign_up")


+ 52
- 0
routers/user/pipeline.go View File

@@ -0,0 +1,52 @@
package user

import (
"crypto/tls"
"net/http"
"strconv"
"time"

"github.com/go-resty/resty/v2"

"code.gitea.io/gitea/modules/log"

"code.gitea.io/gitea/modules/setting"
)

var restyClient *resty.Client

func getRestyClient() *resty.Client {
if restyClient == nil {
restyClient = resty.New()
restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
return restyClient
}

type pipelineResult struct {
Code int `json:"code"`
Message string `json:"msg"`
}

func NotifyPipelineUserLogout(uid int64) {
if setting.MLOPS && setting.MlopsHost != "" && setting.MlopsToken != "" {
client := getRestyClient()
var result pipelineResult
for i := 0; i < 4; i++ {
if i != 0 {
time.Sleep(10 * time.Second)
}
res, err := client.R().
SetHeader("Content-Type", "application/json").
SetResult(&result).
Get(setting.MlopsHost + "/api/v1/pengcheng/logout?userId=" + strconv.FormatInt(uid, 10) + "&token=" + setting.MlopsToken)

if err != nil {
log.Warn("log out notify pipeline failed:", err)
} else if res.StatusCode() == http.StatusOK && result.Code == 0 {
break
}

}
}
}

Loading…
Cancel
Save