summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/api/flags.go12
-rw-r--r--cmd/api/main.go64
-rw-r--r--cmd/cli/main.go59
3 files changed, 38 insertions, 97 deletions
diff --git a/cmd/api/flags.go b/cmd/api/flags.go
deleted file mode 100644
index 4d967fd..0000000
--- a/cmd/api/flags.go
+++ /dev/null
@@ -1,12 +0,0 @@
-package main
-
-import "flag"
-
-var (
- // cluster = flag.String("cluster", "", "Master node address (ex: 192.168.1.10:5657)")
- // clusterID = flag.String("cluster_id", "", "Cluster id")
- // clusterAddr = flag.String("cluster_addr", ":5657", "Self address for cluster")
- apiAddr = flag.String("api_addr", ":5656", "API address")
- dbPath = flag.String("db_path", "/var/djson.db", "DB file")
- logLevel = flag.String("log_level", "info", "Log level")
-)
diff --git a/cmd/api/main.go b/cmd/api/main.go
index 3d2f587..f6d0166 100644
--- a/cmd/api/main.go
+++ b/cmd/api/main.go
@@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
+ "log"
"net/http"
"os"
"os/signal"
@@ -10,39 +11,54 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog"
- "github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
+ badger "github.com/dgraph-io/badger/v3"
+
+ "go.neonxp.dev/djson/internal/config"
+ "go.neonxp.dev/djson/internal/core"
"go.neonxp.dev/djson/internal/events"
"go.neonxp.dev/djson/internal/handler"
- "go.neonxp.dev/djson/internal/storage"
- "go.neonxp.dev/djson/internal/tree"
+ "go.neonxp.dev/djson/internal/logger"
)
+var configFile = flag.String("config", "/etc/djson/config.json", "Path to config file")
+
func main() {
flag.Parse()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
- logger := httplog.NewLogger("djson", httplog.Options{
+ cfg, err := config.Parse(*configFile)
+ if err != nil {
+ panic(err)
+ }
+
+ baseLogger := httplog.NewLogger("djson", httplog.Options{
JSON: true,
- LogLevel: *logLevel,
+ LogLevel: cfg.Log.Level,
})
- // Tree storage
- storage, err := storage.New(*dbPath, logger)
+ appLogger := logger.Logger{
+ Logger: baseLogger,
+ }
+
+ dbOpts := badger.DefaultOptions(cfg.DB)
+ dbOpts.Logger = appLogger
+
+ db, err := badger.Open(dbOpts)
if err != nil {
- panic(err)
+ log.Fatal(err)
}
+ defer db.Close()
+
eventsDispatcher := events.New()
- // Tree engine
- core := tree.New(
- storage,
- eventsDispatcher,
- )
- if err := core.Init(); err != nil {
- panic(err)
+
+ // Tree core
+ core := core.New(db)
+ if err := core.Init(ctx); err != nil {
+ log.Fatal(err)
}
// Tree HTTP wrapper
@@ -51,7 +67,7 @@ func main() {
// HTTP router
r := chi.NewRouter()
- r.Use(httplog.RequestLogger(logger))
+ r.Use(httplog.RequestLogger(appLogger.Logger))
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
@@ -61,20 +77,14 @@ func main() {
r.Route("/events", treeHandler.HandleEvents)
server := &http.Server{
- Addr: *apiAddr,
+ Addr: cfg.Listen,
Handler: r,
}
- if err := run(ctx, logger, server, storage); err != nil {
- panic(err)
- }
-}
-
-func run(ctx context.Context, logger zerolog.Logger, server *http.Server, storage storage.Storage) error {
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
- logger.Info().Str("server listen", *apiAddr).Send()
+ appLogger.Info().Str("server listen", cfg.Listen).Send()
if err := server.ListenAndServe(); err != http.ErrServerClosed {
return err
}
@@ -83,9 +93,11 @@ func run(ctx context.Context, logger zerolog.Logger, server *http.Server, storag
eg.Go(func() error {
<-ctx.Done()
- storage.Close()
+ db.Close()
return server.Close()
})
- return eg.Wait()
+ if err := eg.Wait(); err != nil {
+ panic(err)
+ }
}
diff --git a/cmd/cli/main.go b/cmd/cli/main.go
deleted file mode 100644
index 5cfd5e0..0000000
--- a/cmd/cli/main.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package main
-
-import (
- "fmt"
- "log"
- "os"
- "time"
-
- "github.com/lestrrat-go/jwx/v2/jwa"
- "github.com/lestrrat-go/jwx/v2/jwt"
- "github.com/urfave/cli/v2"
- "go.neonxp.dev/djson/internal/config"
-)
-
-func main() {
- app := &cli.App{
- Name: "djson cli tool",
- Commands: []*cli.Command{
- {
- Name: "token",
- Action: func(ctx *cli.Context) error {
- cfg, err := config.Parse(ctx.String("config"))
- if err != nil {
- return err
- }
-
- t := jwt.New()
- t.Set(jwt.SubjectKey, `djson`)
- t.Set(jwt.IssuedAtKey, time.Now())
- t.Set("allowed", []string{
- "a/b/c",
- "d/e/f",
- })
- signed, err := jwt.Sign(t, jwt.WithKey(
- jwa.KeyAlgorithmFrom(cfg.JWT.Algorithm),
- cfg.JWT.PrivateKey,
- ))
- if err != nil {
- return err
- }
- fmt.Println(string(signed))
- return nil
- },
- },
- },
- Action: cli.ShowAppHelp,
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "config",
- Usage: "Path to config file",
- Value: "/etc/djson/config.json",
- },
- },
- }
-
- if err := app.Run(os.Args); err != nil {
- log.Fatal(err)
- }
-}