From b6b7357676dcc4ddeaa3a70c10d7f098e4af32ff Mon Sep 17 00:00:00 2001 From: Noah Date: Sun, 10 Mar 2024 14:06:10 -0500 Subject: [PATCH] Start all sites on program launch (fix #8) --- main.go | 6 ++++-- site.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 9446f37..d893d7b 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,7 @@ func main() { parseConfig(*config_path, &configuration) // Parse JSON configuration file - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){ + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){ // Webhook receiver var data map[string]interface{} err := json.NewDecoder(r.Body).Decode(&data) if err != nil { @@ -61,5 +61,7 @@ func main() { fmt.Fprint(w, "Received!") }) log.Printf("Starting Lapis Deploy on port %d...", configuration.port) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", configuration.port), nil)) + + startAllServers(configuration.sites_dir) // Start all the servers + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", configuration.port), nil)) // Start HTTP server } diff --git a/site.go b/site.go index 8ebfab2..e451213 100644 --- a/site.go +++ b/site.go @@ -12,7 +12,46 @@ import ( "github.com/gogs/git-module" ) +// startAllServers attempts to start every server in 'sites_dir' +func startAllServers(sites_dir string) DeployError { + files, err := os.ReadDir(sites_dir) + if err != nil { + return newDeployError(1, "startAllServers", + fmt.Sprintf("Failed to read sites from '%s'", sites_dir), fmt.Sprint(err)) + } + for _, file := range files { + if !file.IsDir() { + continue + } + err := startServer(file.Name()) + if err.code != 0 { + return err + } + } + + return DeployError{} +} + // startServer attempts to start a Lapis server in the given repository. +func startServer(name string) DeployError { + log.Printf("Starting Lapis server in repository %s...", name) + + old_cwd, _ := os.Getwd() + defer syscall.Chdir(old_cwd) + syscall.Chdir(configuration.sites_dir+"/"+name) + + cmd := exec.Command("lapis", "serve") + if err := cmd.Start(); err != nil { + return newDeployError(1, "startServer", fmt.Sprintf("Failed to start Lapis server in repository '%s'", name), "") + } + go func() { + cmd.Wait() + }() + + return DeployError{} +} + +// restartServer attempts to restart the Lapis server in the given repository. func restartServer(name string) DeployError { log.Printf("Restarting Lapis server on repository %s...", name) @@ -26,7 +65,7 @@ func restartServer(name string) DeployError { } log.Printf("[lapis build] %s", out) if !strings.Contains(string(out), "HUP") { - return newDeployError(1, "restartServer", "'lapis build' command returned unexpected value!", string(out)) + return newDeployError(1, "restartServer", "'lapis build' command returned unexpected value! (Lapis server not running?)", string(out)) } return DeployError{} } @@ -43,6 +82,9 @@ func pullRepo(url string, name string) DeployError { if err = git.Clone(url, configuration.sites_dir+"/"+name); err != nil { return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name), "") } + if err := startServer(name); err.code != 0 { + return err + } } else { log.Printf("Pulling down repository %s...", name) repo,err := git.Open(configuration.sites_dir+"/"+name)