Added basic admin client

This commit is contained in:
2022-08-06 16:25:03 +02:00
parent 95ea2190fa
commit 03ad7d60cc
5 changed files with 102 additions and 13 deletions

64
cmd/qddns-admin/main.go Normal file
View File

@@ -0,0 +1,64 @@
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"github.com/alecthomas/kong"
_ "github.com/alecthomas/kong"
"github.com/thequux/qddns/db"
"os"
)
type AddToken struct {
Token string `help:"The token to add access to"`
Domain string `arg:"" help:"Domain to allow access to"`
Note string `arg:"" help:"Comment to include in the DB"`
}
func (cmd *AddToken) Run() error {
ctx := context.Background()
if cmd.Token == "" {
raw := make([]byte, 30)
_, err := rand.Read(raw)
if err != nil {
return err
}
cmd.Token = base64.RawURLEncoding.EncodeToString(raw)
}
println("Token: ", cmd.Token)
tx, err := db.Db.Begin(ctx)
if err != nil {
return err
}
_, err = tx.Exec(ctx, "INSERT INTO qddns_auth (token, domain, description) VALUES ($1, $2, $3)", cmd.Token, cmd.Domain, cmd.Note)
if err != nil {
return err
}
return nil
}
type Cli struct {
Db string `help:"DB connection string (default: postgres:///pdns)"`
Add AddToken `cmd:"" help:"Add a token"`
}
func (cli *Cli) Run() error {
println("Root")
return nil
//db.Connect()
}
var cli Cli
func main() {
ctx := kong.Parse(&cli)
//db.Connect(cli.Db)
println(ctx.Command())
if err := ctx.Run(); err != nil {
println(err.Error())
os.Exit(1)
}
}

View File

@@ -1,11 +1,10 @@
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/thequux/qddns/common"
"github.com/thequux/qddns/db"
_ "go.uber.org/zap"
"net"
"net/http"
@@ -13,16 +12,11 @@ import (
"strings"
)
var db *pgxpool.Pool
func main() {
conn, err := pgxpool.Connect(context.Background(), os.Getenv("QDDNS_DB"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v")
if err := db.Connect(""); err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v", err)
os.Exit(1)
}
defer conn.Close()
db = conn
// Set up server
r := gin.Default()
@@ -47,7 +41,7 @@ func main() {
}
// get a connection
tx, err := db.Begin(c)
tx, err := db.Db.Begin(c)
defer tx.Rollback(c)
if err != nil {
c.JSON(http.StatusInternalServerError, common.Response{Status: "error", Message: "Failed to access database", Code: "QDE0001"})
@@ -88,7 +82,7 @@ func main() {
if tag.RowsAffected() != 1 {
c.JSON(http.StatusInternalServerError, common.Response{
Status: "error",
Message: "Wrong number of rows affected: " + string(tag.RowsAffected()),
Message: fmt.Sprintf("Wrong number of rows affected: %v", tag.RowsAffected()),
})
return
}

24
db/db.go Normal file
View File

@@ -0,0 +1,24 @@
package db
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
"os"
)
var Db *pgxpool.Pool
func Connect(connStr string) error {
if connStr == "" {
connStr = os.Getenv("QDDNS_DB")
}
if connStr == "" {
connStr = "postgresql:///pdns"
}
conn, err := pgxpool.Connect(context.Background(), connStr)
if err != nil {
return err
}
Db = conn
return nil
}

1
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/thequux/qddns
go 1.17
require (
github.com/alecthomas/kong v0.6.1
github.com/gin-gonic/gin v1.8.1
github.com/jackc/pgx/v4 v4.16.1
go.uber.org/zap v1.13.0

10
go.sum
View File

@@ -1,6 +1,10 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/alecthomas/kong v0.6.1 h1:1kNhcFepkR+HmasQpbiKDLylIL8yh5B5y1zPp5bJimA=
github.com/alecthomas/kong v0.6.1/go.mod h1:JfHWDzLmbh/puW6I3V7uWenoh56YNVONW+w8eKeUr9I=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
@@ -146,8 +150,9 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
@@ -242,7 +247,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=