From b4ffd640802165d6e25d9d7153f34511884844a1 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Fri, 31 Mar 2023 17:13:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=94=A8=E6=88=B7=E9=80=80=E5=87=BA?= =?UTF-8?q?=EF=BC=8C=E9=80=9A=E7=9F=A5pipeline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routers/user/auth.go | 6 +++++ routers/user/pipeline.go | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 routers/user/pipeline.go diff --git a/routers/user/auth.go b/routers/user/auth.go index 73e8c44c1a..ddd11f2103 100755 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -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") diff --git a/routers/user/pipeline.go b/routers/user/pipeline.go new file mode 100644 index 0000000000..42f6662bfd --- /dev/null +++ b/routers/user/pipeline.go @@ -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 + } + + } + } +} -- 2.34.1 From ebfe0c3dc2655be881d3b59ffc63b3695d13bb04 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Fri, 31 Mar 2023 17:15:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/setting/setting.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 3a688e2a21..49854004f0 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -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] -- 2.34.1