write some stuff

trunk
vilmibm 2022-04-19 22:27:31 -05:00
parent 09bd315096
commit 9549c1cd22
1 changed files with 59 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"database/sql" "database/sql"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -51,6 +52,7 @@ func main() {
}, },
Logf: func(s string, args ...interface{}) { Logf: func(s string, args ...interface{}) {
fmt.Fprintf(io.Out, s, args...) fmt.Fprintf(io.Out, s, args...)
fmt.Fprintf(io.Out, "\n")
}, },
} }
@ -89,7 +91,7 @@ func _main(opts *Opts) error {
setupAPI(*opts) setupAPI(*opts)
// TODO TLS or SSL or something // TODO TLS or SSL or something
opts.Logf("starting server at %s:%d\n", cfg.Host, cfg.Port) opts.Logf("starting server at %s:%d", cfg.Host, cfg.Port)
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), nil); err != nil { if err := http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), nil); err != nil {
return fmt.Errorf("http server exited with error: %w", err) return fmt.Errorf("http server exited with error: %w", err)
} }
@ -105,8 +107,63 @@ func handler(opts Opts, f http.HandlerFunc) http.HandlerFunc {
} }
} }
// TODO I'm not entirely sold on this hash system; without transport
// encryption, it doesn't really help anything. I'd rather have plaintext +
// transport encryption and then, on the server side, proper salted hashing.
type User struct {
// TODO
ID string
}
type BBJResponse struct {
Error bool `json:"error"`
Data interface{} `json:"data"`
Usermap map[string]User `json:"usermap"`
}
func writeResponse(w http.ResponseWriter, resp BBJResponse) {
json.NewEncoder(w).Encode(resp)
}
func setupAPI(opts Opts) { func setupAPI(opts Opts) {
http.HandleFunc("/instance", handler(opts, func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/instance", handler(opts, func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, opts.Config.InstanceName) w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
writeResponse(w, BBJResponse{
Data: opts.Config.InstanceName,
})
}))
http.HandleFunc("/check_auth", handler(opts, func(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.Error(w, "bad method", 400)
return
}
type args struct {
TargetUser string `json:"target_user"`
TargetHash string `json:"target_hash"`
}
var a args
err := json.NewDecoder(req.Body).Decode(&a)
if err != nil {
http.Error(w, "could not parse arguments", 400)
}
opts.Logf("got %s %s", a.TargetUser, a.TargetHash)
// TODO
result := false
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
writeResponse(w, BBJResponse{
Data: result,
})
})) }))
} }