Skip to content

Commit

Permalink
Go add read JWT_PRIVATE_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Sep 3, 2024
1 parent c955bc8 commit 67924f0
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,51 +266,42 @@ func loadSeafileDB() {
dbType = dbEngine
}

func loadSeahubPK() {
func loadSeahubConfig() error {
seahubPK = os.Getenv("JWT_PRIVATE_KEY")
if seahubPK == "" {
return fmt.Errorf("failed to read JWT_PRIVATE_KEY")
}
confPath := filepath.Join(centralDir, "seahub_settings.py")

file, err := os.Open(confPath)
if err != nil {
log.Warnf("Failed to open seahub_settings.py: %v", err)
return
return fmt.Errorf("Failed to open seahub_settings.py: %v", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)

pkExp := "SECRET_KEY\\s*=\\s*'([^']*)'"
pkRe, err := regexp.Compile(pkExp)
if err != nil {
log.Warnf("Failed to compile regex: %v", err)
return
}
siteRootExpr := "SITE_ROOT\\s*=\\s*'([^']*)'"
siteRootRe, err := regexp.Compile(siteRootExpr)
if err != nil {
log.Warnf("Failed to compile regex: %v", err)
return
return fmt.Errorf("Failed to compile regex: %v", err)
}

siteRoot := ""
for scanner.Scan() {
line := scanner.Text()
matches := pkRe.FindStringSubmatch(line)
if matches != nil {
seahubPK = matches[1]
}
matches = siteRootRe.FindStringSubmatch(line)
matches := siteRootRe.FindStringSubmatch(line)
if matches != nil {
siteRoot = matches[1]
}
}
if siteRoot != "" {
seahubURL = fmt.Sprintf("http://127.0.0.1:8000%sapi/v2.1/internal", siteRoot)
} else {
seahubURL = ("http://127.0.0.1:8000/api/v2.1/internal")
}
if seahubPK == "" {
log.Warnf("No seahub private key is configured")
seahubURL = "http://127.0.0.1:8000/api/v2.1/internal"
}

return nil
}

func writePidFile(pid_file_path string) error {
Expand Down Expand Up @@ -411,7 +402,9 @@ func main() {
fp.Close()
}

loadSeahubPK()
if err := loadSeahubConfig(); err != nil {
log.Fatalf("Failed to read seahub config: %v", err)
}

repomgr.Init(seafileDB)

Expand Down

0 comments on commit 67924f0

Please sign in to comment.