LapisDeploy/main.go

83 lines
2.7 KiB
Go
Raw Normal View History

// package main is the main package for the LapisDeploy program
2024-03-07 15:11:25 -06:00
package main
2024-03-07 16:43:06 -06:00
import (
"encoding/json"
"fmt"
2024-03-07 16:43:06 -06:00
"log"
"net/http"
"os"
"github.com/akamensky/argparse"
2024-03-07 16:43:06 -06:00
)
2024-03-07 15:11:25 -06:00
// Create a configuration struct
2024-03-08 15:16:33 -06:00
var configuration = Configuration{}
// fileExists returns whether the given file or directory exists.
func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return false, err
}
// handler is in charge of handling requests after the JSON has been parsed
func handler(data map[string]interface{}) {
repository := data["repository"].(map[string]interface{})
log.Default().Printf("Repo: %s", repository["full_name"])
2024-03-10 21:22:05 -05:00
sendMessage(MatrixMessage{text: fmt.Sprintf("⚪️ Handling webhook for `%s`...", repository["full_name"])})
repo_name := repository["name"].(string)
exists, err := fileExists(configuration.sites_dir+"/"+repo_name)
if err != nil {
newDeployError(1, "handler", "Failed to check if site '%s' exists", fmt.Sprint(err))
}
if exists {
2024-03-11 15:52:49 -05:00
if deploy_error := updateSite(repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return
}
if deploy_error := restartSite(repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return
}
} else {
if deploy_error := cloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return
}
if deploy_error := startSite(repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return
}
2024-03-10 21:22:05 -05:00
}
sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"})
}
// main is the starting point of the program
2024-03-07 15:11:25 -06:00
func main() {
// Parse arguments
parser := argparse.NewParser("lapisdeploy", "Easily deploy Lapis web applications through Gitea webhooks")
config_path := parser.String("c", "config", &argparse.Options{Required: false, Help: "Configuration file", Default: "./config.json"})
if err := parser.Parse(os.Args); err != nil { // Parse arguments
fmt.Print(parser.Usage(err)) // Show usage if there's an error
return
}
parseConfig(*config_path, &configuration) // Parse JSON configuration file
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){ // Webhook receiver
2024-03-07 16:43:06 -06:00
var data map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
log.Panic(err)
2024-03-07 16:43:06 -06:00
return
}
go handler(data) // Run the handler
fmt.Fprint(w, "Received!")
2024-03-07 16:43:06 -06:00
})
log.Printf("Starting Lapis Deploy on port %d...", configuration.port)
startAllSites() // Start all the servers
2024-03-10 17:21:27 -05:00
go initMatrix() // Start Matrix stuff in a goroutine
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", configuration.port), nil)) // Start HTTP server
2024-03-07 15:11:25 -06:00
}