Add username and watch period web configuration

This commit is contained in:
Justin Hawkins 2021-02-06 12:50:44 +10:30
parent 4d09901fb3
commit 6e493522c8
2 changed files with 97 additions and 1 deletions

View File

@ -18,6 +18,21 @@
</div> </div>
</form> </form>
<form class="">
<div class="form-row align-items-center config-item" data-key="username">
<div class="col-sm-5 my-1">
<span>Bot username (optional)</span>
</div>
<div class="col-sm-4 my-1">
<label class="sr-only" for="inlineFormInputName">Name</label>
<input type="text" class="form-control rest-field" placeholder="">
</div>
<div class="col-auto my-1">
<button type="submit" class="btn btn-primary">update</button>
</div>
</div>
</form>
<form class=""> <form class="">
<div class="form-row align-items-center config-item" data-key="directory"> <div class="form-row align-items-center config-item" data-key="directory">
<div class="col-sm-5 my-1"> <div class="col-sm-5 my-1">
@ -33,6 +48,21 @@
</div> </div>
</form> </form>
<form class="">
<div class="form-row align-items-center config-item" data-key="watch">
<div class="col-sm-5 my-1">
<span>Period between filesystem checks</span>
</div>
<div class="col-sm-4 my-1">
<label class="sr-only" for="inlineFormInputName">Seconds</label>
<input type="text" class="form-control rest-field" placeholder="/...">
</div>
<div class="col-auto my-1">
<button type="submit" class="btn btn-primary">update</button>
</div>
</div>
</form>
<p class="lead"> <p class="lead">
<a href="#" class="btn btn-lg btn-secondary">Learn more</a> <a href="#" class="btn btn-lg btn-secondary">Learn more</a>
</p> </p>

View File

@ -6,7 +6,7 @@ import (
"github.com/tardisx/discord-auto-upload/assets" "github.com/tardisx/discord-auto-upload/assets"
"log" "log"
"net/http" "net/http"
// "strings" "strconv"
"github.com/tardisx/discord-auto-upload/config" "github.com/tardisx/discord-auto-upload/config"
"mime" "mime"
"os" "os"
@ -95,6 +95,70 @@ func getSetWebhook(w http.ResponseWriter, r *http.Request) {
} }
} }
// TODO there should be locks around all these config accesses
func getSetUsername(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "GET" {
getResponse := valueStringResponse{Success: true, Value: config.Config.Username}
// I can't see any way this will fail
js, _ := json.Marshal(getResponse)
w.Write(js)
} else if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
config.Config.Username = r.PostForm.Get("value")
postResponse := valueStringResponse{Success: true, Value: config.Config.Username}
js, _ := json.Marshal(postResponse)
w.Write(js)
}
}
func getSetWatch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "GET" {
getResponse := valueStringResponse{Success: true, Value: strconv.Itoa(config.Config.Watch)}
// I can't see any way this will fail
js, _ := json.Marshal(getResponse)
w.Write(js)
} else if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
i, err := strconv.Atoi(r.PostForm.Get("value"))
if err != nil {
response := errorResponse{Success: false, Error: fmt.Sprintf("Bad value for watch: %v", err)}
js, _ := json.Marshal(response)
w.Write(js)
return
}
if i < 1 {
response := errorResponse{Success: false, Error: "must be > 0"}
js, _ := json.Marshal(response)
w.Write(js)
return
}
config.Config.Watch = i
postResponse := valueStringResponse{Success: true, Value: strconv.Itoa(config.Config.Watch)}
js, _ := json.Marshal(postResponse)
w.Write(js)
}
}
func getSetDirectory(w http.ResponseWriter, r *http.Request) { func getSetDirectory(w http.ResponseWriter, r *http.Request) {
log.Print("ok") log.Print("ok")
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@ -139,6 +203,8 @@ func getSetDirectory(w http.ResponseWriter, r *http.Request) {
func StartWebServer() { func StartWebServer() {
http.HandleFunc("/", getStatic) http.HandleFunc("/", getStatic)
http.HandleFunc("/rest/config/webhook", getSetWebhook) http.HandleFunc("/rest/config/webhook", getSetWebhook)
http.HandleFunc("/rest/config/username", getSetUsername)
http.HandleFunc("/rest/config/watch", getSetWatch)
http.HandleFunc("/rest/config/directory", getSetDirectory) http.HandleFunc("/rest/config/directory", getSetDirectory)
log.Print("Starting web server on http://localhost:9090") log.Print("Starting web server on http://localhost:9090")