summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/cli/main.go59
-rw-r--r--etc/config.json10
-rw-r--r--internal/config/config.go52
3 files changed, 121 insertions, 0 deletions
diff --git a/cmd/cli/main.go b/cmd/cli/main.go
new file mode 100644
index 0000000..5cfd5e0
--- /dev/null
+++ b/cmd/cli/main.go
@@ -0,0 +1,59 @@
+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)
+ }
+}
diff --git a/etc/config.json b/etc/config.json
new file mode 100644
index 0000000..246d903
--- /dev/null
+++ b/etc/config.json
@@ -0,0 +1,10 @@
+{
+ "listen": ":5656",
+ "db": {
+ "path": "tree.db"
+ },
+ "jwt": {
+ "algorithm": "HS512",
+ "privateKey": "s3cr3t"
+ }
+} \ No newline at end of file
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 0000000..0c48a87
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,52 @@
+package config
+
+import (
+ "os"
+
+ "go.neonxp.dev/json"
+ "go.neonxp.dev/json/model"
+)
+
+type Config struct {
+ Listen string
+ DB dbConfig
+ JWT jwtConfig
+}
+
+type dbConfig struct {
+ Path string
+}
+
+type jwtConfig struct {
+ Algorithm string
+ PrivateKey []byte
+}
+
+func Parse(file string) (*Config, error) {
+ fb, err := os.ReadFile(file)
+ if err != nil {
+ return nil, err
+ }
+
+ cfgNode, err := json.Unmarshal(fb)
+ if err != nil {
+ return nil, err
+ }
+ listen := model.MustQuery(cfgNode, []string{"listen"}).(*model.StringNode).Value
+ dbPath := model.MustQuery(cfgNode, []string{"db", "path"}).(*model.StringNode).Value
+ algorithm := model.MustQuery(cfgNode, []string{"jwt", "algorithm"}).(*model.StringNode).Value
+ privateKey := model.MustQuery(cfgNode, []string{"jwt", "privateKey"}).(*model.StringNode).Value
+
+ cfg := &Config{
+ Listen: listen,
+ DB: dbConfig{
+ Path: dbPath,
+ },
+ JWT: jwtConfig{
+ Algorithm: algorithm,
+ PrivateKey: []byte(privateKey),
+ },
+ }
+
+ return cfg, nil
+}