2017-07-26 14:24:02 +09:30
|
|
|
package web
|
|
|
|
|
2017-07-26 22:40:21 +09:30
|
|
|
import (
|
2017-07-27 22:04:32 +09:30
|
|
|
"discord-auto-upload/asset"
|
2017-07-27 12:18:02 +09:30
|
|
|
"encoding/json"
|
2017-07-26 22:40:21 +09:30
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
2017-07-26 14:24:02 +09:30
|
|
|
|
2017-07-27 12:18:02 +09:30
|
|
|
// DAUWebServer - stuff for the web server
|
|
|
|
type DAUWebServer struct {
|
|
|
|
ConfigChange chan int
|
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Success int
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, ""))
|
|
|
|
// }
|
|
|
|
|
|
|
|
func getIndex(w http.ResponseWriter, r *http.Request) {
|
2017-07-27 22:04:32 +09:30
|
|
|
data, err := asset.Asset("index.html")
|
|
|
|
if err != nil {
|
|
|
|
// Asset was not found.
|
|
|
|
fmt.Fprintln(w, err)
|
|
|
|
}
|
|
|
|
w.Write(data)
|
|
|
|
|
2017-07-27 12:18:02 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-07-26 22:40:21 +09:30
|
|
|
}
|
2017-07-27 12:18:02 +09:30
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSetDirectory(w http.ResponseWriter, r *http.Request) {
|
2017-07-26 22:40:21 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
// Init is great
|
2017-07-27 12:18:02 +09:30
|
|
|
func Init() DAUWebServer {
|
|
|
|
wsConfig.ConfigChange = make(chan int)
|
2017-07-26 22:40:21 +09:30
|
|
|
go startWebServer()
|
2017-07-27 12:18:02 +09:30
|
|
|
return wsConfig
|
2017-07-26 22:40:21 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
func startWebServer() {
|
2017-07-27 12:18:02 +09:30
|
|
|
|
|
|
|
http.HandleFunc("/", getIndex)
|
|
|
|
http.HandleFunc("/rest/config/webhook", getSetWebhook)
|
|
|
|
http.HandleFunc("/rest/config/directory", getSetDirectory)
|
|
|
|
|
2017-07-26 22:40:21 +09:30
|
|
|
log.Print("Starting web server on http://localhost:9090")
|
|
|
|
err := http.ListenAndServe(":9090", nil) // set listen port
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
}
|
2017-07-27 22:04:32 +09:30
|
|
|
|
2017-07-26 14:24:02 +09:30
|
|
|
}
|