Start all sites on program launch (fix #8)

This commit is contained in:
Noah 2024-03-10 14:06:10 -05:00
parent 260df0914a
commit b6b7357676
2 changed files with 47 additions and 3 deletions

View File

@ -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
}

44
site.go
View File

@ -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)