Skip to content

Commit

Permalink
Merge pull request #15 from getAlby/task-webhook
Browse files Browse the repository at this point in the history
feat: add webhook support
  • Loading branch information
im-adithya authored Feb 27, 2024
2 parents f39393f + 3abd463 commit 257b89a
Show file tree
Hide file tree
Showing 12 changed files with 961 additions and 119 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env
*.db
72 changes: 34 additions & 38 deletions cmd/server/main.go
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")
}
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ module http-nostr
go 1.21.3

require (
github.com/getsentry/sentry-go v0.25.0
github.com/jackc/pgx/v5 v5.3.1
github.com/joho/godotenv v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/labstack/echo/v4 v4.11.1
github.com/nbd-wtf/go-nostr v0.25.7
github.com/sirupsen/logrus v1.9.3
gopkg.in/DataDog/dd-trace-go.v1 v1.58.0
gorm.io/driver/postgres v1.4.6
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
)

require (
Expand All @@ -24,11 +32,13 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davrux/echo-logrus/v4 v4.0.3
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ebitengine/purego v0.5.0 // indirect
github.com/go-gormigrate/gormigrate/v2 v2.1.1
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.2.0 // indirect
Expand Down Expand Up @@ -64,5 +74,6 @@ require (
golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gorm.io/gorm v1.25.4
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a // indirect
)
Loading

0 comments on commit 257b89a

Please sign in to comment.