pomme/internal/api/api.go
2022-12-11 01:57:53 -05:00

73 lines
1.6 KiB
Go

package api
import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/jackc/pgx/v5"
"github.com/miekg/dns"
"golang.org/x/crypto/bcrypt"
)
func Ingest(w http.ResponseWriter, r *http.Request) {
request := r.URL.Query().Get("req")
zp := dns.NewZoneParser(strings.NewReader(request), "", "")
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
log.Println(rr)
}
// todo -- add functions to apply to master zonefile if above check is OK
if err := zp.Err(); err != nil {
log.Println(err)
http.Error(w, "Unable to parse Zonefile", http.StatusBadRequest)
}
}
func NewUser(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.Form.Get("username")
if username == "" {
username = autoUname()
}
password := r.Form.Get("password")
if password == "" {
password = "nigga" //testing purposes
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
panic(err)
}
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var hashedDB string
err = conn.QueryRow(context.Background(), "select 'query here'").Scan(&hashedDB)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
w.Write([]byte(username))
w.Write([]byte("██████████"))
w.Write(hashedPassword)
}
func autoUname() string {
rand.Seed(time.Now().UnixNano())
return fmt.Sprintf("user%d", rand.Intn(99999-00000)+0000)
}