Rework configuration to it's own package and make it available to web server. Start a template driven web interface.

This commit is contained in:
2021-01-31 17:53:32 +10:30
parent 55bb5a8bae
commit 46a0f5a187
8 changed files with 326 additions and 90 deletions

View File

@@ -8,6 +8,12 @@ import (
"github.com/tardisx/discord-auto-upload/assets"
// "strings"
"regexp"
"github.com/tardisx/discord-auto-upload/config"
"os"
"path/filepath"
"mime"
"text/template"
)
// DAUWebServer - stuff for the web server
@@ -15,66 +21,124 @@ type DAUWebServer struct {
ConfigChange chan int
}
type response struct {
Success int
type valueStringResponse struct {
Success bool `json: 'success'`
Value string `json: 'value'`
}
// I am too noob to work out how to pass context around
var wsConfig DAUWebServer
// r.ParseForm() // parse arguments, you have to call this by yourself
// fmt.Println(r.Form) // print form information in server side
// fmt.Println("path", r.URL.Path)
// fmt.Println("scheme", r.URL.Scheme)
// fmt.Println(r.Form["url_long"])
// for k, v := range r.Form {
// fmt.Println("key:", k)
// fmt.Println("val:", strings.Join(v, ""))
// }
type errorResponse struct {
Success bool `json: 'success'`
Error string `json: 'error'`
}
func getStatic(w http.ResponseWriter, r *http.Request) {
// haha this is dumb and I should change it
fmt.Println(r.URL)
// fmt.Println(r.URL)
re := regexp.MustCompile(`[^a-zA-Z0-9\.]`)
path := r.URL.Path
path := r.URL.Path[1:]
sanitized_path := re.ReplaceAll([]byte(path), []byte("_"))
fmt.Println(sanitized_path)
if string(sanitized_path) == "" {
sanitized_path = []byte("index.html");
}
data, err := assets.Asset(string(sanitized_path))
if err != nil {
// Asset was not found.
fmt.Fprintln(w, err)
}
extension := filepath.Ext(string(sanitized_path))
// is this a HTML file? if so wrap it in the template
if extension == ".html" {
wrapper, _ := assets.Asset("wrapper.tmpl")
t := template.Must(template.New("wrapper").Parse(string(wrapper)))
var b struct {
Body string
Path string
Version string
}
b.Body = string(data)
b.Path = string(sanitized_path)
b.Version = config.CurrentVersion
t.Execute(w, b)
return
}
// otherwise we are a static thing
w.Header().Set("Content-Type", mime.TypeByExtension(extension))
w.Write(data)
//
}
// TODO there should be locks around all these config accesses
func getSetWebhook(w http.ResponseWriter, r *http.Request) {
wsConfig.ConfigChange <- 1
goodResponse := response{1}
js, err := json.Marshal(goodResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
if r.Method == "GET" {
getResponse := valueStringResponse{ Success: true, Value: config.Config.WebHookURL }
// 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.WebHookURL = r.PostForm.Get("value")
postResponse := valueStringResponse{ Success: true, Value: config.Config.WebHookURL }
js, _ := json.Marshal(postResponse)
w.Write(js)
}
}
func getSetDirectory(w http.ResponseWriter, r *http.Request) {
log.Print("ok")
w.Header().Set("Content-Type", "application/json")
if r.Method == "GET" {
getResponse := valueStringResponse{ Success: true, Value: config.Config.Path }
// 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)
}
newPath := r.PostForm.Get("value")
// sanity check this path
stat, err := os.Stat(newPath)
if os.IsNotExist(err) {
// not exist
response := errorResponse{ Success: false, Error: fmt.Sprintf("Path: %s - does not exist", newPath) }
js, _ := json.Marshal(response)
w.Write(js)
return;
} else if ! stat.IsDir() {
// not a directory
response := errorResponse{ Success: false, Error: fmt.Sprintf("Path: %s - is not a directory", newPath) }
js, _ := json.Marshal(response)
w.Write(js)
return;
}
config.Config.Path = newPath
postResponse := valueStringResponse{ Success: true, Value: config.Config.Path }
js, _ := json.Marshal(postResponse)
w.Write(js)
}
}
// Init is great
func Init() DAUWebServer {
wsConfig.ConfigChange = make(chan int)
go startWebServer()
return wsConfig
}
func startWebServer() {
func StartWebServer() {
http.HandleFunc("/", getStatic)
http.HandleFunc("/rest/config/webhook", getSetWebhook)
http.HandleFunc("/rest/config/directory", getSetDirectory)