66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
// package main is the main package for the LapisDeploy program
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"github.com/akamensky/argparse"
|
|
)
|
|
|
|
// Create a configuration struct
|
|
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"])
|
|
|
|
deploy_error := pullRepo(repository["ssh_url"].(string), repository["name"].(string))
|
|
if deploy_error.code != 0 {
|
|
log.Default().Printf("Error in %s! [Code: %d] (%s)", deploy_error.where, deploy_error.code, deploy_error.details)
|
|
}
|
|
deploy_error = restartServer(repository["name"].(string))
|
|
if deploy_error.code != 0 {
|
|
log.Printf("Error in %s! [Code: %d] (%s) {Command output: '%s'}",
|
|
deploy_error.where, deploy_error.code, deploy_error.details, deploy_error.command_output)
|
|
}
|
|
}
|
|
|
|
// main is the starting point of the program
|
|
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){
|
|
var data map[string]interface{}
|
|
err := json.NewDecoder(r.Body).Decode(&data)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
return
|
|
}
|
|
|
|
go handler(data) // Run the handler
|
|
fmt.Fprint(w, "Received!")
|
|
})
|
|
log.Printf("Starting Lapis Deploy...")
|
|
log.Fatal(http.ListenAndServe(":7575", nil))
|
|
}
|