diff --git a/main.go b/main.go index 06d93c4..12c83bc 100644 --- a/main.go +++ b/main.go @@ -7,11 +7,14 @@ import ( "os/signal" logger "git.site.quack-lab.dev/dave/cylogger" + wh "go-eve-pi/webhook" "github.com/fasthttp/router" "github.com/valyala/fasthttp" ) +var webhook wh.Webhook + func main() { flag.Parse() logger.InitFlag() @@ -27,6 +30,8 @@ func main() { logger.Error("Failed to create SSO instance %v", err) return } + + webhook = wh.NewZulipWebhook(options.ZulipURL, options.ZulipEmail, options.ZulipToken) // Setup fasthttp router 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 sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, os.Interrupt) diff --git a/options.go b/options.go index 598d114..a04499a 100644 --- a/options.go +++ b/options.go @@ -21,11 +21,16 @@ func init() { } type Options struct { - ClientID string - RedirectURI string - Scopes []string - DBPath string - Port string + DBPath string + Port string + + ESIScopes []string + ESIRedirectURI string + ESIClientID string + + WebhookURL string + WebhookEmail string + WebhookToken string } func LoadOptions() (Options, error) { @@ -34,33 +39,45 @@ func LoadOptions() (Options, error) { return Options{}, fmt.Errorf("error loading .env file: %w", err) } - clientID := os.Getenv("ESI_CLIENT_ID") - if clientID == "" { - return Options{}, fmt.Errorf("ESI_CLIENT_ID is required in .env file") - } - redirectURI := os.Getenv("ESI_REDIRECT_URI") - if redirectURI == "" { - 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") - } + dbPath := getOrDefault("DB_PATH", "eve-pi.db") + port := getOrDefault("HTTP_SERVER_PORT", "3000") + + clientID := getOrFatal("ESI_CLIENT_ID") + redirectURI := getOrFatal("ESI_REDIRECT_URI") + rawScopes := getOrDefault("ESI_SCOPES", "esi-planets.manage_planets.v1") scopes := strings.Fields(rawScopes) - dbPath := os.Getenv("DB_PATH") - if dbPath == "" { - return Options{}, fmt.Errorf("DB_PATH is required in .env file") - } - port := os.Getenv("HTTP_SERVER_PORT") - if port == "" { - return Options{}, fmt.Errorf("HTTP_SERVER_PORT is required in .env file") - } + + webhookUrl := getOrFatal("WEBHOOK_URL") + webhookEmail := getOrFatal("WEBHOOK_EMAIL") + webhookToken := getOrFatal("WEBHOOK_TOKEN") return Options{ - ClientID: clientID, - RedirectURI: redirectURI, - Scopes: scopes, - DBPath: dbPath, - Port: port, + DBPath: dbPath, + Port: port, + + ESIClientID: clientID, + ESIRedirectURI: redirectURI, + ESIScopes: scopes, + + WebhookURL: webhookUrl, + WebhookEmail: webhookEmail, + WebhookToken: webhookToken, }, 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 +} diff --git a/webhook/webhook.go b/webhook/webhook.go index 9f4071d..56a372a 100644 --- a/webhook/webhook.go +++ b/webhook/webhook.go @@ -14,7 +14,7 @@ type WebhookMessage struct { Timestamp time.Time `json:"timestamp"` } -// WebhookInterface defines the contract for webhook operations -type WebhookInterface interface { +// Webhook defines the contract for webhook operations +type Webhook interface { Post(channel, topic, message string) error }