158 lines
3.3 KiB
Go
Raw Normal View History

package web
2017-07-26 22:40:21 +09:30
import (
"embed"
2017-07-27 12:18:02 +09:30
"encoding/json"
2017-07-26 22:40:21 +09:30
"fmt"
"html/template"
"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"
"strings"
"github.com/tardisx/discord-auto-upload/config"
daulog "github.com/tardisx/discord-auto-upload/log"
"github.com/tardisx/discord-auto-upload/version"
2017-07-26 22:40:21 +09:30
)
type WebService struct {
Config config.ConfigService
}
//go:embed data
var webFS embed.FS
2017-07-27 12:18:02 +09:30
// DAUWebServer - stuff for the web server
type DAUWebServer struct {
// ConfigChange chan int
2021-02-07 08:10:06 +10:30
}
2017-07-27 12:18:02 +09:30
func (ws *WebService) getStatic(w http.ResponseWriter, r *http.Request) {
2020-03-26 11:40:35 +10:30
path := r.URL.Path
path = strings.TrimLeft(path, "/")
if path == "" {
path = "index.html"
2017-07-27 22:04:32 +09:30
}
extension := filepath.Ext(string(path))
if extension == ".html" { // html file
t, err := template.ParseFS(webFS, "data/wrapper.tmpl", "data/"+path)
if err != nil {
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
}
log.Printf("req: %s", r.URL.Path)
var b struct {
2021-01-31 18:48:48 +10:30
Body string
Path string
Version string
}
b.Path = path
b.Version = version.CurrentVersion
err = t.ExecuteTemplate(w, "layout", b)
if err != nil {
panic(err)
}
return
} else { // anything else
otherStatic, err := webFS.ReadFile("data/" + path)
if err != nil {
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
}
w.Header().Set("Content-Type", mime.TypeByExtension(extension))
w.Write(otherStatic)
return
}
2017-07-27 12:18:02 +09:30
}
func (ws *WebService) getLogs(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
showDebug := false
debug, present := r.URL.Query()["debug"]
if present && len(debug[0]) > 0 && debug[0] != "0" {
showDebug = true
}
text := ""
for _, log := range daulog.LogEntries {
if !showDebug && log.Type == daulog.LogTypeDebug {
continue
}
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))
}
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-02-09 22:07:40 +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() {
http.HandleFunc("/", ws.getStatic)
2021-02-09 22:07:40 +10:30
http.HandleFunc("/rest/logs", ws.getLogs)
// http.HandleFunc("/rest/uploads", getUploads)
http.HandleFunc("/rest/config", ws.handleConfig)
go func() {
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
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}()
}