-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from getAlby/task-webhook
feat: add webhook support
- Loading branch information
Showing
12 changed files
with
961 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.env | ||
.env | ||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"http-nostr/internal/nostr" | ||
"time" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/kelseyhightower/envconfig" | ||
echologrus "github.com/davrux/echo-logrus/v4" | ||
"github.com/labstack/echo/v4" | ||
"github.com/sirupsen/logrus" | ||
ddEcho "gopkg.in/DataDog/dd-trace-go.v1/contrib/labstack/echo.v4" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
|
||
"github.com/getsentry/sentry-go" | ||
) | ||
|
||
|
||
func main() { | ||
logrus.SetFormatter(&logrus.JSONFormatter{}) | ||
// Load env file as env variables | ||
err := godotenv.Load(".env") | ||
ctx := context.Background() | ||
svc, err := nostr.NewService(ctx) | ||
if err != nil { | ||
logrus.Errorf("Error loading environment variables: %v", err) | ||
} | ||
//load global config | ||
type config struct { | ||
SentryDSN string `envconfig:"SENTRY_DSN"` | ||
DatadogAgentUrl string `envconfig:"DATADOG_AGENT_URL"` | ||
Port int `default:"8080"` | ||
} | ||
globalConf := &config{} | ||
err = envconfig.Process("", globalConf) | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
// Setup exception tracking with Sentry if configured | ||
if globalConf.SentryDSN != "" { | ||
if err = sentry.Init(sentry.ClientOptions{ | ||
Dsn: globalConf.SentryDSN, | ||
IgnoreErrors: []string{"401"}, | ||
}); err != nil { | ||
logrus.Errorf("sentry init error: %v", err) | ||
} | ||
defer sentry.Flush(2 * time.Second) | ||
logrus.Fatalf("Failed to initialize service: %v", err) | ||
} | ||
|
||
echologrus.Logger = svc.Logger | ||
e := echo.New() | ||
if globalConf.DatadogAgentUrl != "" { | ||
tracer.Start(tracer.WithAgentAddr(globalConf.DatadogAgentUrl)) | ||
if svc.Cfg.DatadogAgentUrl != "" { | ||
tracer.Start(tracer.WithAgentAddr(svc.Cfg.DatadogAgentUrl)) | ||
defer tracer.Stop() | ||
e.Use(ddEcho.Middleware(ddEcho.WithServiceName("http-nostr"))) | ||
} | ||
|
||
e.GET("/info", nostr.InfoHandler) | ||
e.POST("/nip47", nostr.NIP47Handler) | ||
// r.Use(loggingMiddleware) | ||
e.GET("/info", svc.InfoHandler) | ||
e.POST("/nip47", svc.NIP47Handler) | ||
e.POST("/subscribe", svc.SubscriptionHandler) | ||
e.DELETE("/subscribe/:id", svc.StopSubscriptionHandler) | ||
e.Use(echologrus.Middleware()) | ||
|
||
logrus.Infof("Server starting on port %d", globalConf.Port) | ||
logrus.Fatal(e.Start(fmt.Sprintf(":%d", globalConf.Port))) | ||
//start Echo server | ||
go func() { | ||
if err := e.Start(fmt.Sprintf(":%v", svc.Cfg.Port)); err != nil && err != http.ErrServerClosed { | ||
svc.Logger.Fatalf("Shutting down the server: %v", err) | ||
} | ||
}() | ||
//handle graceful shutdown | ||
<-svc.Ctx.Done() | ||
svc.Logger.Infof("Shutting down echo server...") | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
e.Shutdown(ctx) | ||
svc.Logger.Info("Echo server exited") | ||
svc.Relay.Close() | ||
svc.Logger.Info("Relay connection closed") | ||
svc.Logger.Info("Waiting for service to exit...") | ||
svc.Wg.Wait() | ||
svc.Logger.Info("Service exited") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.