package main import ( "encoding/json" "fmt" "log" "net/http" "os" "github.com/gogs/git-module" ) type DeployError struct { code int where string details string } func newDeployError(code int, where string, details string) DeployError { return DeployError{ code: code, where: where, details: details } } // 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 } // startServer attempts to start a Lapis server in the given repository. func startServer(name string) DeployError { return DeployError{} } // pullRepo attempts to pull or clone a repository using the given name and url func pullRepo(url string, name string) DeployError { exists, err := fileExists(name) if err != nil { return newDeployError(1, "fileExists", fmt.Sprintf("Failed to check whether folder '%s' exists while pulling down repository '%s'!", name, name)) } if !exists { if err = git.Clone(url, name); err != nil { return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name)) } } else { repo,err := git.Open(name) if err != nil { return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name)) } repo.Pull() } return DeployError{} } func handler(data map[string]interface{}) { repository := data["repository"].(map[string]interface{}) log.Default().Printf("Repo: %s", repository["full_name"]) log.Default().Printf("Ref: %s", data["ref"]) 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) } //syscall.Chdir(repository["name"].(string)) //syscall.Exec("lapis", []string{"serve"}, []string{"production"}) } func main() { 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) fmt.Fprint(w, "Received!") }) log.Fatal(http.ListenAndServe(":7575", nil)) }