Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add webhook support #15

Merged
merged 17 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading