2017-07-26 14:24:02 +09:30
|
|
|
package web
|
|
|
|
|
2017-07-26 22:40:21 +09:30
|
|
|
import (
|
2021-10-04 13:03:26 +10:30
|
|
|
"embed"
|
2017-07-27 12:18:02 +09:30
|
|
|
"encoding/json"
|
2017-07-26 22:40:21 +09:30
|
|
|
"fmt"
|
2021-10-04 13:03:26 +10:30
|
|
|
"html/template"
|
2021-10-06 23:12:43 +10:30
|
|
|
"io/ioutil"
|
2021-02-07 08:10:27 +10:30
|
|
|
"log"
|
2021-01-31 18:48:48 +10:30
|
|
|
"mime"
|
2021-02-07 08:10:27 +10:30
|
|
|
"net/http"
|
2021-01-31 18:48:48 +10:30
|
|
|
"path/filepath"
|
2021-10-04 13:03:26 +10:30
|
|
|
"strings"
|
2021-06-02 23:42:29 +09:30
|
|
|
|
|
|
|
"github.com/tardisx/discord-auto-upload/config"
|
2021-06-03 19:36:48 +09:30
|
|
|
daulog "github.com/tardisx/discord-auto-upload/log"
|
2021-10-04 12:20:16 +10:30
|
|
|
"github.com/tardisx/discord-auto-upload/version"
|
2017-07-26 22:40:21 +09:30
|
|
|
)
|
2017-07-26 14:24:02 +09:30
|
|
|
|
2021-10-06 23:12:43 +10:30
|
|
|
type WebService struct {
|
|
|
|
Config config.ConfigService
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:03:26 +10:30
|
|
|
//go:embed data
|
|
|
|
var webFS embed.FS
|
|
|
|
|
2017-07-27 12:18:02 +09:30
|
|
|
// DAUWebServer - stuff for the web server
|
|
|
|
type DAUWebServer struct {
|
2021-10-06 23:12:43 +10:30
|
|
|
// ConfigChange chan int
|
2021-02-07 08:10:06 +10:30
|
|
|
|
2021-01-31 17:53:32 +10:30
|
|
|
}
|
2017-07-27 12:18:02 +09:30
|
|
|
|
2021-10-06 23:12:43 +10:30
|
|
|
func (ws *WebService) getStatic(w http.ResponseWriter, r *http.Request) {
|
2020-03-26 11:40:35 +10:30
|
|
|
|
2021-10-04 13:03:26 +10:30
|
|
|
path := r.URL.Path
|
|
|
|
path = strings.TrimLeft(path, "/")
|
|
|
|
if path == "" {
|
|
|
|
path = "index.html"
|
2017-07-27 22:04:32 +09:30
|
|
|
}
|
2021-01-31 17:53:32 +10:30
|
|
|
|
2021-10-04 13:03:26 +10:30
|
|
|
extension := filepath.Ext(string(path))
|
2021-01-31 17:53:32 +10:30
|
|
|
|
2021-10-06 23:12:43 +10:30
|
|
|
if extension == ".html" { // html file
|
2021-10-04 13:03:26 +10:30
|
|
|
t, err := template.ParseFS(webFS, "data/wrapper.tmpl", "data/"+path)
|
|
|
|
if err != nil {
|
2021-10-04 13:27:46 +10:30
|
|
|
log.Printf("when fetching: %s got: %s", path, err)
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte("not found"))
|
|
|
|
return
|
2021-10-04 13:03:26 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("req: %s", r.URL.Path)
|
|
|
|
|
2021-01-31 17:53:32 +10:30
|
|
|
var b struct {
|
2021-01-31 18:48:48 +10:30
|
|
|
Body string
|
|
|
|
Path string
|
2021-01-31 17:53:32 +10:30
|
|
|
Version string
|
|
|
|
}
|
2021-10-04 13:03:26 +10:30
|
|
|
b.Path = path
|
2021-10-04 12:20:16 +10:30
|
|
|
b.Version = version.CurrentVersion
|
2021-10-04 13:03:26 +10:30
|
|
|
|
|
|
|
err = t.ExecuteTemplate(w, "layout", b)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-31 17:53:32 +10:30
|
|
|
return
|
2021-10-06 23:12:43 +10:30
|
|
|
} else { // anything else
|
2021-10-04 13:03:26 +10:30
|
|
|
otherStatic, err := webFS.ReadFile("data/" + path)
|
2021-01-31 17:53:32 +10:30
|
|
|
|
2021-10-04 13:03:26 +10:30
|
|
|
if err != nil {
|
2021-10-04 13:27:46 +10:30
|
|
|
log.Printf("when fetching: %s got: %s", path, err)
|
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte("not found"))
|
|
|
|
return
|
2021-10-04 13:03:26 +10:30
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(extension))
|
|
|
|
|
|
|
|
w.Write(otherStatic)
|
|
|
|
return
|
|
|
|
}
|
2021-01-31 17:53:32 +10:30
|
|
|
|
2017-07-27 12:18:02 +09:30
|
|
|
}
|
|
|
|
|
2021-10-06 23:12:43 +10:30
|
|
|
func (ws *WebService) getLogs(w http.ResponseWriter, r *http.Request) {
|
2021-06-03 19:36:48 +09:30
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
|
2021-06-06 17:15:38 +09:30
|
|
|
showDebug := false
|
|
|
|
debug, present := r.URL.Query()["debug"]
|
|
|
|
if present && len(debug[0]) > 0 && debug[0] != "0" {
|
|
|
|
showDebug = true
|
|
|
|
}
|
|
|
|
|
2021-06-03 19:36:48 +09:30
|
|
|
text := ""
|
|
|
|
for _, log := range daulog.LogEntries {
|
2021-06-06 17:15:38 +09:30
|
|
|
if !showDebug && log.Type == daulog.LogTypeDebug {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-03 19:36:48 +09:30
|
|
|
text = text + fmt.Sprintf(
|
|
|
|
"%-6s %-19s %s\n", log.Type, log.Timestamp.Format("2006-01-02 15:04:05"), log.Entry,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// js, _ := json.Marshal(daulog.LogEntries)
|
|
|
|
w.Write([]byte(text))
|
2021-06-07 21:13:57 +09:30
|
|
|
}
|
2021-06-03 19:36:48 +09:30
|
|
|
|
2021-10-06 23:12:43 +10:30
|
|
|
func (ws *WebService) handleConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "POST" {
|
|
|
|
newConfig := config.ConfigV2{}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte("bad body"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(b, &newConfig)
|
|
|
|
if err != nil {
|
|
|
|
w.Write([]byte("bad data"))
|
|
|
|
log.Printf("%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.Config.Config = newConfig
|
|
|
|
ws.Config.Save()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
b, _ := json.Marshal(ws.Config.Config)
|
|
|
|
w.Write(b)
|
2021-06-02 23:42:29 +09:30
|
|
|
}
|
2021-02-09 22:07:40 +10:30
|
|
|
|
2021-10-06 23:12:43 +10:30
|
|
|
// func getUploads(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// w.Header().Set("Content-Type", "application/json")
|
|
|
|
// ups := uploads.Uploads
|
|
|
|
// text, _ := json.Marshal(ups)
|
|
|
|
// w.Write([]byte(text))
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (ws *WebService) StartWebServer() {
|
2021-06-02 23:42:29 +09:30
|
|
|
|
2021-10-06 23:12:43 +10:30
|
|
|
http.HandleFunc("/", ws.getStatic)
|
2021-02-09 22:07:40 +10:30
|
|
|
|
2021-10-06 23:12:43 +10:30
|
|
|
http.HandleFunc("/rest/logs", ws.getLogs)
|
|
|
|
// http.HandleFunc("/rest/uploads", getUploads)
|
|
|
|
http.HandleFunc("/rest/config", ws.handleConfig)
|
2021-06-07 21:13:57 +09:30
|
|
|
|
2021-06-02 23:42:29 +09:30
|
|
|
go func() {
|
2021-10-06 23:12:43 +10:30
|
|
|
listen := fmt.Sprintf(":%d", ws.Config.Config.Port)
|
|
|
|
log.Print("Starting web server on http://localhost%s", listen)
|
|
|
|
err := http.ListenAndServe(listen, nil) // set listen port
|
2021-06-02 23:42:29 +09:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
}
|
|
|
|
}()
|
2017-07-26 14:24:02 +09:30
|
|
|
}
|