Compare commits
2 Commits
f6e4e64e81
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 99236b5fb5 | |||
| d22a6816c4 |
37
main.go
37
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -8,15 +9,21 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"gitea.urkob.com/urko/gitea-webhook-listener/internal"
|
||||
"gitea.urkob.com/urko/gitea-webhook-listener/kit/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
ctx, cancel := context.WithCancel(signalContext(context.Background()))
|
||||
defer cancel()
|
||||
|
||||
cfgFile := os.Getenv("CONFIG_FILE")
|
||||
if cfgFile == "" {
|
||||
// Get root path
|
||||
@@ -28,8 +35,17 @@ func main() {
|
||||
log.Fatalf("Error loading config: %v", err)
|
||||
}
|
||||
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
|
||||
|
||||
go func() {
|
||||
err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
|
||||
}()
|
||||
log.Printf("server is up on %d\n", cfg.Port)
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
|
||||
|
||||
<-ctx.Done()
|
||||
if err != nil {
|
||||
log.Println("some error happened", err)
|
||||
}
|
||||
log.Println("server shutdown")
|
||||
}
|
||||
|
||||
func handlePayload(secret string, projects map[string][]config.ConfigScript) func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -66,6 +82,7 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
|
||||
return
|
||||
}
|
||||
branchName := strings.Split(payload.Ref, "/")[len(strings.Split(payload.Ref, "/"))-1]
|
||||
log.Println("branchName", branchName)
|
||||
found = false
|
||||
var scr config.ConfigScript
|
||||
for i := range proj {
|
||||
@@ -86,6 +103,7 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
|
||||
if err := execute(scr.Command, scr.Arguments...); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
log.Println("script was deployed successful")
|
||||
}()
|
||||
})
|
||||
}
|
||||
@@ -98,6 +116,23 @@ func execute(command string, args ...string) error {
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("cmd.Run %w", err)
|
||||
}
|
||||
log.Println()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func signalContext(ctx context.Context) context.Context {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-sigs
|
||||
signal.Stop(sigs)
|
||||
close(sigs)
|
||||
cancel()
|
||||
}()
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user