Files
cish/api/main.go
2025-08-24 00:04:05 +02:00

75 lines
1.7 KiB
Go

package main
import (
"context"
"fmt"
"net/http"
"os"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo/v4"
"github.com/ren3gadem4rm0t/github-hook-types-go/webhook"
"github.com/spf13/viper"
"github.com/zoriya/cish/api/github"
)
type Handler struct {
db *pgxpool.Pool
}
func (h Handler) CreateOci(c echo.Context) error {
return nil
}
func (h Handler) CreateSubJob(c echo.Context) error {
return nil
}
type config struct {
GithubAppID string `mapstructure:"github_app_id"`
GithubAppPrivateKey string `mapstructure:"github_app_private_key"`
GithubWebhookSecret string `mapstructure:"github_webhook_secret"`
DatabaseURL string `mapstructure:"database_url"`
}
func main() {
ctx := context.Background()
e := echo.New()
var C config
v := viper.NewWithOptions(viper.ExperimentalBindStruct())
v.AutomaticEnv()
err := v.Unmarshal(&C)
if err != nil {
e.Logger.Fatalf("unable to decode into struct, %v", err)
}
dbpool, err := pgxpool.New(ctx, C.DatabaseURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\n", err)
os.Exit(1)
}
defer dbpool.Close()
h := Handler{dbpool}
e.PUT("/job/:uid/oci", h.CreateOci)
e.PUT("/job/:uid/job", h.CreateSubJob)
webhookHandler := webhook.NewHandler(C.GithubWebhookSecret)
e.POST("/hooks/github", func(c echo.Context) error {
event, err := webhookHandler.ProcessWebhook(c.Request())
if err != nil {
return c.String(http.StatusBadRequest, "Webhook payload parsing failed")
}
if err := github.HandleEvent(event); err != nil {
return c.String(http.StatusInternalServerError, "Webhook event handler failed")
}
return c.String(http.StatusOK, "Webhook handled succesfully")
})
e.Logger.Fatal(e.Server.ListenAndServe())
}