-
Notifications
You must be signed in to change notification settings - Fork 2
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: use db #17
Merged
Merged
feat: use db #17
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d5b3a12
feat: use db
im-adithya a27a466
chore: use postgres db
im-adithya 20b4a5b
chore: remove db and add gitignore
im-adithya 92c981a
feat: add subscription, request and response tables
im-adithya 995924a
fix: db issues beforesave and afterfind
im-adithya 74412d9
chore: publish state for request event
im-adithya bf8d0dd
chore: refactorings
im-adithya 937cc84
chore: fix migration README
im-adithya e46ebbd
feat: add subscriptions
im-adithya ebfedfd
chore: subscription fixes
im-adithya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,72 +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"` | ||
DefaultRelayURL string `envconfig:"DEFAULT_RELAY_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"))) | ||
} | ||
|
||
service, err := nostr.NewNostrService(&nostr.Config{ | ||
DefaultRelayURL: globalConf.DefaultRelayURL, | ||
}) | ||
if err != nil { | ||
logrus.Fatalf("Failed to initialize NostrService: %v", err) | ||
} | ||
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()) | ||
|
||
e.GET("/info", service.InfoHandler) | ||
e.POST("/nip47", service.NIP47Handler) | ||
// r.Use(loggingMiddleware) | ||
|
||
logrus.Infof("Server starting on port %d", globalConf.Port) | ||
if err := e.Start(fmt.Sprintf(":%d", globalConf.Port)); err != nil { | ||
logrus.Fatalf("Server failed to start: %v", err) | ||
} | ||
//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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this also how we do it in lndhub?