Test webhook

This commit is contained in:
2025-10-10 20:48:35 +02:00
parent 23253eb06c
commit 6d88711576
3 changed files with 58 additions and 32 deletions

View File

@@ -7,11 +7,14 @@ import (
"os/signal" "os/signal"
logger "git.site.quack-lab.dev/dave/cylogger" logger "git.site.quack-lab.dev/dave/cylogger"
wh "go-eve-pi/webhook"
"github.com/fasthttp/router" "github.com/fasthttp/router"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
var webhook wh.Webhook
func main() { func main() {
flag.Parse() flag.Parse()
logger.InitFlag() logger.InitFlag()
@@ -27,6 +30,8 @@ func main() {
logger.Error("Failed to create SSO instance %v", err) logger.Error("Failed to create SSO instance %v", err)
return return
} }
webhook = wh.NewZulipWebhook(options.ZulipURL, options.ZulipEmail, options.ZulipToken)
// Setup fasthttp router // Setup fasthttp router
r := router.New() r := router.New()
@@ -70,6 +75,10 @@ func main() {
} }
}() }()
for i := 0; i < 10; i++ {
webhook.Post("eve-pi", "test", "test")
}
// Listen for SIGINT and gracefully shut down the server // Listen for SIGINT and gracefully shut down the server
sigCh := make(chan os.Signal, 1) sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt) signal.Notify(sigCh, os.Interrupt)

View File

@@ -21,11 +21,16 @@ func init() {
} }
type Options struct { type Options struct {
ClientID string DBPath string
RedirectURI string Port string
Scopes []string
DBPath string ESIScopes []string
Port string ESIRedirectURI string
ESIClientID string
WebhookURL string
WebhookEmail string
WebhookToken string
} }
func LoadOptions() (Options, error) { func LoadOptions() (Options, error) {
@@ -34,33 +39,45 @@ func LoadOptions() (Options, error) {
return Options{}, fmt.Errorf("error loading .env file: %w", err) return Options{}, fmt.Errorf("error loading .env file: %w", err)
} }
clientID := os.Getenv("ESI_CLIENT_ID") dbPath := getOrDefault("DB_PATH", "eve-pi.db")
if clientID == "" { port := getOrDefault("HTTP_SERVER_PORT", "3000")
return Options{}, fmt.Errorf("ESI_CLIENT_ID is required in .env file")
} clientID := getOrFatal("ESI_CLIENT_ID")
redirectURI := os.Getenv("ESI_REDIRECT_URI") redirectURI := getOrFatal("ESI_REDIRECT_URI")
if redirectURI == "" { rawScopes := getOrDefault("ESI_SCOPES", "esi-planets.manage_planets.v1")
return Options{}, fmt.Errorf("ESI_REDIRECT_URI is required in .env file")
}
rawScopes := os.Getenv("ESI_SCOPES")
if rawScopes == "" {
return Options{}, fmt.Errorf("ESI_SCOPES is required in .env file")
}
scopes := strings.Fields(rawScopes) scopes := strings.Fields(rawScopes)
dbPath := os.Getenv("DB_PATH")
if dbPath == "" { webhookUrl := getOrFatal("WEBHOOK_URL")
return Options{}, fmt.Errorf("DB_PATH is required in .env file") webhookEmail := getOrFatal("WEBHOOK_EMAIL")
} webhookToken := getOrFatal("WEBHOOK_TOKEN")
port := os.Getenv("HTTP_SERVER_PORT")
if port == "" {
return Options{}, fmt.Errorf("HTTP_SERVER_PORT is required in .env file")
}
return Options{ return Options{
ClientID: clientID, DBPath: dbPath,
RedirectURI: redirectURI, Port: port,
Scopes: scopes,
DBPath: dbPath, ESIClientID: clientID,
Port: port, ESIRedirectURI: redirectURI,
ESIScopes: scopes,
WebhookURL: webhookUrl,
WebhookEmail: webhookEmail,
WebhookToken: webhookToken,
}, nil }, nil
} }
func getOrFatal(key string) string {
value := os.Getenv(key)
if value == "" {
logger.Error("Environment variable %s is required", key)
os.Exit(1)
}
return value
}
func getOrDefault(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}

View File

@@ -14,7 +14,7 @@ type WebhookMessage struct {
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
} }
// WebhookInterface defines the contract for webhook operations // Webhook defines the contract for webhook operations
type WebhookInterface interface { type Webhook interface {
Post(channel, topic, message string) error Post(channel, topic, message string) error
} }