2020-02-14 10:42:38 +00:00
|
|
|
package webserver
|
|
|
|
|
|
|
|
import (
|
2020-02-14 14:56:10 +00:00
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
irc "git.martyn.berlin/martyn/karaokards/internal/irc"
|
2020-02-14 10:42:38 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//var store = sessions.NewCookieStore(os.Getenv("SESSION_KEY"))
|
|
|
|
|
2020-02-22 13:05:30 +00:00
|
|
|
var ircBot *irc.KardBot
|
2020-02-14 10:42:38 +00:00
|
|
|
|
|
|
|
func HealthHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Header().Add("Content-type", "text/plain")
|
|
|
|
fmt.Fprint(response, "I'm okay jack!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotFoundHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Header().Add("X-Template-File", "html"+request.URL.Path)
|
2020-02-21 19:52:19 +00:00
|
|
|
response.WriteHeader(404)
|
2020-02-14 10:42:38 +00:00
|
|
|
tmpl := template.Must(template.ParseFiles("web/404.html"))
|
|
|
|
tmpl.Execute(response, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CSSHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Header().Add("Content-type", "text/css")
|
|
|
|
tmpl := template.Must(template.ParseFiles("web/cover.css"))
|
|
|
|
tmpl.Execute(response, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RootHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
request.URL.Path = "/index.html"
|
|
|
|
TemplateHandler(response, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TemplateHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Header().Add("X-Template-File", "web"+request.URL.Path)
|
|
|
|
type TemplateData struct {
|
2020-02-14 14:56:10 +00:00
|
|
|
Prompt string
|
|
|
|
AvailCount int
|
|
|
|
ChannelCount int
|
|
|
|
MessageCount int
|
2020-02-14 10:42:38 +00:00
|
|
|
}
|
|
|
|
// tmpl, err := template.New("html"+request.URL.Path).Funcs(template.FuncMap{
|
|
|
|
// "ToUpper": strings.ToUpper,
|
|
|
|
// "ToLower": strings.ToLower,
|
|
|
|
// }).ParseFiles("html"+request.URL.Path)
|
|
|
|
_ = strings.ToLower("Hello")
|
|
|
|
if strings.Index(request.URL.Path, "/") < 0 {
|
|
|
|
http.Error(response, "No slashes wat - "+request.URL.Path, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
basenameSlice := strings.Split(request.URL.Path, "/")
|
|
|
|
basename := basenameSlice[len(basenameSlice)-1]
|
|
|
|
//fmt.Fprintf(response, "%q", basenameSlice)
|
|
|
|
tmpl, err := template.New(basename).Funcs(template.FuncMap{
|
|
|
|
"ToUpper": strings.ToUpper,
|
|
|
|
"ToLower": strings.ToLower,
|
|
|
|
}).ParseFiles("web" + request.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(response, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
// NotFoundHandler(response, request)
|
|
|
|
// return
|
|
|
|
}
|
2020-02-22 13:05:30 +00:00
|
|
|
var td = TemplateData{ircBot.Prompts[rand.Intn(len(ircBot.Prompts))], len(ircBot.Prompts), len(ircBot.ChannelData), 0}
|
2020-02-14 10:42:38 +00:00
|
|
|
err = tmpl.Execute(response, td)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(response, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 13:05:30 +00:00
|
|
|
func LeaveHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
request.URL.Path = "/bye.html"
|
2020-02-21 19:52:19 +00:00
|
|
|
TemplateHandler(response, request)
|
|
|
|
}
|
|
|
|
|
2020-02-22 13:05:30 +00:00
|
|
|
func AdminHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
|
|
|
if vars["key"] != ircBot.ChannelData[vars["channel"]].AdminKey {
|
|
|
|
UnauthorizedHandler(response, request)
|
|
|
|
return
|
2020-02-22 13:08:01 +00:00
|
|
|
}
|
2020-02-22 13:05:30 +00:00
|
|
|
type TemplateData struct {
|
2020-02-22 13:08:01 +00:00
|
|
|
Channel string
|
|
|
|
Command string
|
|
|
|
ExtraStrings string
|
|
|
|
SinceTime time.Time
|
|
|
|
Leaving bool
|
2020-02-22 13:05:30 +00:00
|
|
|
}
|
|
|
|
channelData := ircBot.ChannelData[vars["channel"]]
|
|
|
|
var td = TemplateData{channelData.Name, channelData.Command, channelData.ExtraStrings, channelData.JoinTime, false}
|
|
|
|
|
|
|
|
if request.Method == "POST" {
|
|
|
|
request.ParseForm()
|
2020-02-22 13:08:01 +00:00
|
|
|
if strings.Join(request.PostForm["leave"], ",") == "Leave twitch channel" {
|
2020-02-22 13:05:30 +00:00
|
|
|
td.Leaving = true
|
2020-02-22 13:08:01 +00:00
|
|
|
} else if strings.Join(request.PostForm["reallyleave"], ",") == "Really leave twitch channel" {
|
|
|
|
delete(ircBot.ChannelData, vars["channel"])
|
2020-02-22 13:05:30 +00:00
|
|
|
ircBot.Database.Delete("channelData", vars["channel"])
|
|
|
|
ircBot.LeaveChannel(vars["channel"])
|
|
|
|
LeaveHandler(response, request)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sourceData := ircBot.ChannelData[vars["channel"]]
|
2020-02-22 13:08:01 +00:00
|
|
|
if strings.Join(request.PostForm["Command"], ",") != "" {
|
|
|
|
sourceData.Command = strings.Join(request.PostForm["Command"], ",")
|
2020-02-22 13:05:30 +00:00
|
|
|
td.Command = sourceData.Command
|
|
|
|
ircBot.ChannelData[vars["channel"]] = sourceData
|
|
|
|
}
|
2020-02-22 13:08:01 +00:00
|
|
|
if strings.Join(request.PostForm["ExtraStrings"], ",") != sourceData.ExtraStrings {
|
|
|
|
sourceData.ExtraStrings = strings.Join(request.PostForm["ExtraStrings"], ",")
|
2020-02-22 13:05:30 +00:00
|
|
|
td.ExtraStrings = sourceData.ExtraStrings
|
|
|
|
ircBot.ChannelData[vars["channel"]] = sourceData
|
|
|
|
}
|
2020-02-22 13:08:01 +00:00
|
|
|
ircBot.Database.Write("channelData", vars["channel"], sourceData)
|
2020-02-22 13:05:30 +00:00
|
|
|
}
|
|
|
|
tmpl := template.Must(template.ParseFiles("web/admin.html"))
|
|
|
|
tmpl.Execute(response, td)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnauthorizedHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Header().Add("X-Template-File", "html"+request.URL.Path)
|
|
|
|
response.WriteHeader(401)
|
|
|
|
tmpl := template.Must(template.ParseFiles("web/401.html"))
|
|
|
|
tmpl.Execute(response, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleHTTP(passedIrcBot *irc.KardBot) {
|
2020-02-14 14:56:10 +00:00
|
|
|
ircBot = passedIrcBot
|
2020-02-14 10:42:38 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
|
|
|
|
r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
|
|
|
|
r.HandleFunc("/", RootHandler)
|
|
|
|
r.HandleFunc("/healthz", HealthHandler)
|
2020-02-22 13:05:30 +00:00
|
|
|
r.HandleFunc("/web/{.*}", TemplateHandler)
|
|
|
|
r.PathPrefix("/static/").Handler(http.FileServer(http.Dir("./web/")))
|
2020-02-14 10:42:38 +00:00
|
|
|
r.HandleFunc("/cover.css", CSSHandler)
|
2020-02-21 19:52:19 +00:00
|
|
|
r.HandleFunc("/admin/{channel}/{key}", AdminHandler)
|
2020-02-14 10:42:38 +00:00
|
|
|
http.Handle("/", r)
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: loggedRouter,
|
|
|
|
Addr: "0.0.0.0:5353",
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
fmt.Println("Listening on 0.0.0.0:5353")
|
|
|
|
srv.ListenAndServe()
|
|
|
|
}
|