This is an automated email from the ASF dual-hosted git repository. github-actions[bot] pushed a commit to tag nightly in repository https://gitbox.apache.org/repos/asf/casbin-gateway.git
commit 9a3304e9ecfa6fe6e1b012d43606d12d7b3ebaca Author: Yang Luo <[email protected]> AuthorDate: Sat Aug 22 15:57:22 2026 +0800 fix(run): drop desktop shortcut and auto-start files --- main.go | 1 - object/site.go | 6 ++-- run/run.go | 26 --------------- run/self.go | 52 ------------------------------ run/util.go | 42 ------------------------ run/util_path.go | 25 -------------- run/util_process.go | 93 +++++++++++++++++++++-------------------------------- 7 files changed, 39 insertions(+), 206 deletions(-) diff --git a/main.go b/main.go index fd44b2f..a67561f 100644 --- a/main.go +++ b/main.go @@ -58,7 +58,6 @@ func main() { object.InitRuleMap() run.InitAppMap() run.InitRdsClient() - run.InitSelfStart() object.StartMonitorSitesLoop() agentmonitor.Configure( diff --git a/object/site.go b/object/site.go index d1bbd14..792769c 100644 --- a/object/site.go +++ b/object/site.go @@ -292,12 +292,12 @@ func (site *Site) checkNodes() error { msg = addErrorToMsg(msg, "IsProcessActive", err) } } - // Also check if the window title exists (process may be starting/compiling) + // Also check if the process exists (it may be starting/compiling) if !ok { var err error - ok, err = run.IsWindowTitleActive(site.Name) + ok, err = run.IsSiteProcessActive(site.Name) if err != nil { - msg = addErrorToMsg(msg, "IsWindowTitleActive", err) + msg = addErrorToMsg(msg, "IsSiteProcessActive", err) } } } diff --git a/run/run.go b/run/run.go index 37064e1..28af6ca 100644 --- a/run/run.go +++ b/run/run.go @@ -94,19 +94,6 @@ func CreateRepo(siteName string, needStart bool, diff string, providerName strin return wrapRepoError("gitUploadCdn", path, err) } } - - _, err = updateBatFile(siteName) - if err != nil { - return wrapRepoError("updateBatFile", path, err) - } - - shortcutPath := getShortcutPath(siteName) - if !util.FileExist(shortcutPath) { - err = updateShortcutFile(siteName) - if err != nil { - return wrapRepoError("updateShortcutFile", path, err) - } - } } else { // For existing repositories, check if upgrade is allowed affected := false @@ -154,19 +141,6 @@ func CreateRepo(siteName string, needStart bool, diff string, providerName strin } } - _, err = updateBatFile(siteName) - if err != nil { - return wrapRepoError("updateBatFile", path, err) - } - - shortcutPath := getShortcutPath(siteName) - if !util.FileExist(shortcutPath) { - err = updateShortcutFile(siteName) - if err != nil { - return wrapRepoError("updateShortcutFile", path, err) - } - } - if affected { if !needStart { if !strings.HasPrefix(siteName, "casdoor") && !strings.HasPrefix(siteName, "casibase") && !strings.HasPrefix(siteName, "opendata") { diff --git a/run/self.go b/run/self.go deleted file mode 100644 index b1456b4..0000000 --- a/run/self.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2023 The casbin Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package run - -import ( - "runtime" - "strings" - - "github.com/apache/casbin-gateway/util" - "github.com/beego/beego" -) - -func InitSelfStart() { - if runtime.GOOS != "windows" { - return - } - - hostname := util.GetHostname() - if strings.Count(hostname, "-") == 2 { - return - } - - siteName := beego.AppConfig.String("dbName") - if siteName == "" { - panic("InitSelfStart() error, siteName should not be empty") - } - - _, err := updateBatFile(siteName) - if err != nil { - panic(err) - } - - shortcutPath := getShortcutPath(siteName) - if !util.FileExist(shortcutPath) { - err = updateShortcutFile(siteName) - if err != nil { - panic(err) - } - } -} diff --git a/run/util.go b/run/util.go index 4319861..cfbdefa 100644 --- a/run/util.go +++ b/run/util.go @@ -18,7 +18,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "strings" "github.com/apache/casbin-gateway/conf" @@ -64,22 +63,6 @@ func getRepoUrl(name string) string { } } -func getShortcut() string { - res := "Shortcut" - language := conf.GetConfigStringUnquoted("language") - if language != "en" { - res = "快捷方式" - } - return res -} - -func ensureFileFolderExists(path string) error { - if !util.FileExist(path) { - return os.MkdirAll(path, os.ModePerm) - } - return nil -} - func updateAppConfFile(name string, i int, orgName string) { fmt.Printf("updateAppConfFile(): [%s]\n", name) confPath := getCodeAppConfPath(name) @@ -132,28 +115,3 @@ func updateAppConfFile(name string, i int, orgName string) { util.WriteStringToPath(content, confPath) } - -func updateBatFile(name string) (bool, error) { - batPath := getBatPath(name) - err := ensureFileFolderExists(filepath.Dir(batPath)) - if err != nil { - return false, err - } - - if util.FileExist(batPath) { - return true, nil - } - - fmt.Printf("updateBatFile(): [%s]\n", name) - - content := fmt.Sprintf("cd %s\ngo run main.go", GetRepoPath(name)) - util.WriteStringToPath(content, batPath) - return false, nil -} - -func updateShortcutFile(name string) error { - fmt.Printf("updateShortcutFile(): [%s]\n", name) - - cmd := exec.Command("powershell", fmt.Sprintf("$s=(New-Object -COM WScript.Shell).CreateShortcut('%s');$s.TargetPath='%s';$s.Save()", getShortcutPath(name), getBatPath(name))) - return cmd.Run() -} diff --git a/run/util_path.go b/run/util_path.go index 9934cab..1c705f5 100644 --- a/run/util_path.go +++ b/run/util_path.go @@ -17,29 +17,14 @@ package run import ( "encoding/json" "fmt" - "os/user" "path/filepath" "strings" "github.com/apache/casbin-gateway/conf" ) -var username string var appMap = map[string]string{} -func init() { - usr, err := user.Current() - if err != nil { - panic(err) - } - - username = usr.Username - tokens := strings.Split(username, "\\") - if len(tokens) == 2 { - username = tokens[1] - } -} - func InitAppMap() { res := conf.GetConfigStringUnquoted("appMap") if res != "" { @@ -80,13 +65,3 @@ func getCodeAppConfPath(name string) string { appDir := conf.GetConfigStringUnquoted("appDir") return fmt.Sprintf("%s/%s/conf/app.conf", appDir, name) } - -func getBatPath(name string) string { - name = getMappedName(name) - return fmt.Sprintf("C:/Users/%s/Desktop/run/%s.bat", username, name) -} - -func getShortcutPath(name string) string { - name = getMappedName(name) - return fmt.Sprintf("C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/%s.bat - %s.lnk", username, name, getShortcut()) -} diff --git a/run/util_process.go b/run/util_process.go index 762f5ee..481199b 100644 --- a/run/util_process.go +++ b/run/util_process.go @@ -17,25 +17,23 @@ package run import ( "bytes" "fmt" - "io" "os/exec" "regexp" "strconv" "strings" - - "github.com/apache/casbin-gateway/util" - "golang.org/x/text/encoding/simplifiedchinese" - "golang.org/x/text/transform" ) -var reBatNames *regexp.Regexp +// A site runs in its own console window started from its repo folder. The +// "title" sets the window title so the user can tell the windows apart, and it +// is also what identifies the process in the command line listing below. +var reSiteNames = regexp.MustCompile(`title (\S+) & go run main\.go`) -func init() { - reBatNames = regexp.MustCompile(`\\Desktop\\run\\(.*?)\.bat`) +func getRunCommand(name string) string { + return fmt.Sprintf("title %s & go run main.go", name) } -func parseBatName(s string) string { - res := reBatNames.FindStringSubmatch(s) +func parseSiteName(s string) string { + res := reSiteNames.FindStringSubmatch(s) if res == nil { return "" } @@ -43,31 +41,27 @@ func parseBatName(s string) string { return res[1] } -func getBatNamesFromOutput(output string) map[string]int { - batNameMap := map[string]int{} +func getSiteNamesFromOutput(output string) map[string]int { + siteNameMap := map[string]int{} output = strings.ReplaceAll(output, "\r", "") lines := strings.Split(output, "\n") for _, line := range lines { - tokens := strings.Split(line, " ") - tokens2 := []string{} - for _, token := range tokens { - if token != "" { - tokens2 = append(tokens2, token) - } + name := parseSiteName(line) + if name == "" { + continue } - if len(tokens2) < 5 || strings.ToLower(tokens2[0]) != `c:\windows\system32\cmd.exe` || tokens2[1] != "/c" { + tokens := strings.Fields(line) + processId, err := strconv.Atoi(tokens[len(tokens)-1]) + if err != nil { continue } - batName := parseBatName(tokens2[2]) - processId := util.ParseInt(tokens2[len(tokens2)-1]) - batNameMap[batName] = processId - //fmt.Printf("%s, %d\n", batName, processId) + siteNameMap[name] = processId } - return batNameMap + return siteNameMap } func getPid(name string) (int, error) { @@ -85,30 +79,33 @@ func getPid(name string) (int, error) { return 0, fmt.Errorf("powershell command failed: %v, stderr: %s", err, stderr.String()) } - batNameMap := getBatNamesFromOutput(out.String()) - pid, ok := batNameMap[name] + siteNameMap := getSiteNamesFromOutput(out.String()) + pid, ok := siteNameMap[name] if ok { return pid, nil } else { - return 0, fmt.Errorf("getBatNamesFromOutput() error, name = %s, batNameMap = %v", name, batNameMap) + return 0, fmt.Errorf("getSiteNamesFromOutput() error, name = %s, siteNameMap = %v", name, siteNameMap) } } func startProcess(name string) error { fmt.Printf("startProcess(): [%s]\n", name) - cmd := exec.Command("cmd", "/C", "start", "", getShortcutPath(name)) + cmd := exec.Command("cmd", "/C", "start", "", "cmd", "/C", getRunCommand(getMappedName(name))) + cmd.Dir = GetRepoPath(name) return cmd.Run() } func stopProcess(name string) error { fmt.Printf("stopProcess(): [%s]\n", name) - name = getMappedName(name) - windowName := fmt.Sprintf("%s.bat - %s", name, getShortcut()) - // taskkill /IM "casdoor.bat - Shortcut" /F - // taskkill /F /FI "WINDOWTITLE eq casdoor.bat - Shortcut" /T - cmd := exec.Command("taskkill", "/F", "/FI", fmt.Sprintf("WINDOWTITLE eq %s", windowName), "/T") + pid, err := getPid(name) + if err != nil { + // Not running is not an error to report here. + return nil + } + + cmd := exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(pid)) return cmd.Run() } @@ -126,31 +123,13 @@ func IsProcessActive(pid int) (bool, error) { return res, nil } -func IsWindowTitleActive(name string) (bool, error) { - name = getMappedName(name) - windowName := fmt.Sprintf("%s.bat - %s", name, getShortcut()) - - // Use tasklist to check if a window with the specific title exists - cmd := exec.Command("tasklist", "/V", "/FI", fmt.Sprintf("WINDOWTITLE eq %s", windowName)) - var out bytes.Buffer - cmd.Stdout = &out - err := cmd.Run() - if err != nil { - return false, err - } - - // Decode output from GBK (Windows default codepage) to UTF-8 - decoder := simplifiedchinese.GBK.NewDecoder() - reader := transform.NewReader(&out, decoder) - decoded, err := io.ReadAll(reader) +// IsSiteProcessActive covers the window between "started" and "serving", while +// go run is still compiling and the port is not up yet. +func IsSiteProcessActive(name string) (bool, error) { + _, err := getPid(name) if err != nil { - // If decoding fails, fall back to original output - decoded = out.Bytes() + return false, nil } - output := string(decoded) - // Check if cmd.exe process with the window title exists - // If window title is found, output will contain "cmd.exe" and the window title - res := strings.Contains(output, "cmd.exe") && strings.Contains(output, windowName) - return res, nil + return true, nil } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
