Added basic admin client
This commit is contained in:
64
cmd/qddns-admin/main.go
Normal file
64
cmd/qddns-admin/main.go
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user