Compare commits

..

5 Commits
v0.5 ... main

Author SHA1 Message Date
Urko. 99236b5fb5 fix: add more logs 2024-05-30 12:29:17 +02:00
Urko. d22a6816c4 fix: load endless and block main with ctx.Done() 2024-05-30 07:03:37 +02:00
Urko. f6e4e64e81 feat: update logs 2024-05-25 06:47:21 +02:00
Urko. eaf0b1ff69 fix: change args by slice 2024-05-25 06:31:37 +02:00
Urko. 9c735ccf64 fix: allow just binary execution 2024-05-24 22:25:36 +02:00
2 changed files with 45 additions and 7 deletions

View File

@ -13,8 +13,8 @@ type Config struct {
}
type ConfigScript struct {
Environment string `yaml:"environment"`
BinaryPath string `yaml:"binary"`
ScriptPath string `yaml:"script"`
Command string `yaml:"command"`
Arguments []string `yaml:"args"`
}
func LoadConfig(path string) (*Config, error) {

46
main.go
View File

@ -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,7 +35,17 @@ func main() {
log.Fatalf("Error loading config: %v", err)
}
http.HandleFunc("/", handlePayload(cfg.Secret, cfg.Projects))
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
go func() {
err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil)
}()
log.Printf("server is up on %d\n", cfg.Port)
<-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) {
@ -56,6 +73,7 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
proj, found := projects[project]
if !found {
http.Error(w, "not found", http.StatusNotFound)
log.Printf("project %s not found\n", project)
return
}
var payload internal.WebhookPayload
@ -64,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 {
@ -76,25 +95,44 @@ func handlePayload(secret string, projects map[string][]config.ConfigScript) fun
}
if !found {
http.Error(w, "not found", http.StatusNotFound)
log.Printf("project %s with branch %s not found\n", project, branchName)
return
}
go func() {
if err := execute(scr.BinaryPath, scr.ScriptPath); err != nil {
if err := execute(scr.Command, scr.Arguments...); err != nil {
log.Println(err)
}
log.Println("script was deployed successful")
}()
})
}
func execute(binaryPath, scriptPath string) error {
cmd := exec.Command(binaryPath, scriptPath)
func execute(command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
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
}