diff --git a/deployments/kubernetes/deploy.yaml b/deployments/kubernetes/deploy.yaml
index ed3692f..b9dcf05 100644
--- a/deployments/kubernetes/deploy.yaml
+++ b/deployments/kubernetes/deploy.yaml
@@ -34,6 +34,9 @@ spec:
- image: imartyn/karaokardbot:0.0.1
imagePullPolicy: IfNotPresent
name: kardbot
+ ports:
+ - name: web
+ containerPort: 5353
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
diff --git a/internal/webserver/webserver.go b/internal/webserver/webserver.go
new file mode 100755
index 0000000..e0fe2ef
--- /dev/null
+++ b/internal/webserver/webserver.go
@@ -0,0 +1,96 @@
+package webserver
+
+import (
+ "github.com/gorilla/handlers"
+ "github.com/gorilla/mux"
+ "github.com/gorilla/sessions"
+
+ "fmt"
+ "html/template"
+ "net/http"
+ "os"
+ "strings"
+ "time"
+)
+
+//var store = sessions.NewCookieStore(os.Getenv("SESSION_KEY"))
+
+var store = sessions.NewCookieStore([]byte("GO_SESS"))
+
+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)
+ 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 {
+ Prompt string
+ AvailCount int
+ }
+ // 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
+ }
+ var td = TemplateData{"Hello", 1}
+ err = tmpl.Execute(response, td)
+ if err != nil {
+ http.Error(response, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+func HandleHTTP() {
+ r := mux.NewRouter()
+ loggedRouter := handlers.LoggingHandler(os.Stdout, r)
+ r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
+ r.HandleFunc("/", RootHandler)
+ r.HandleFunc("/healthz", HealthHandler)
+ r.HandleFunc("/example/{.*}", TemplateHandler)
+ r.HandleFunc("/cover.css", CSSHandler)
+ 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()
+}
diff --git a/main.go b/main.go
index 969a97b..e63d772 100755
--- a/main.go
+++ b/main.go
@@ -11,6 +11,7 @@ import (
builtins "git.martyn.berlin/martyn/karaokards/internal/builtins"
irc "git.martyn.berlin/martyn/karaokards/internal/irc"
+ webserver "git.martyn.berlin/martyn/karaokards/internal/webserver"
rgb "github.com/foresthoffman/rgblog"
)
@@ -26,8 +27,8 @@ func readBonusStrings() []string {
ex, err := os.Executable()
if err != nil {
- return []string{}
fmt.Println("Could not read `strings.json`, will only have builtin prompts. File reading error", err)
+ return []string{}
}
exPath := filepath.Dir(ex)
data, err := ioutil.ReadFile(exPath + "/strings.json")
@@ -83,5 +84,9 @@ func main() {
Server: "irc.chat.twitch.tv",
Prompts: selectablePrompts,
}
+ go func() {
+ rgb.YPrintf("[%s] Starting webserver on port %s\n", irc.TimeStamp(), "5353")
+ webserver.HandleHTTP()
+ }()
myBot.Start()
}
diff --git a/web/404.html b/web/404.html
new file mode 100644
index 0000000..3926de0
--- /dev/null
+++ b/web/404.html
@@ -0,0 +1,108 @@
+
+
+
+
+
+
+
+
+ The k8s zoo
+
+
+
+
+
+
+
+
+
+
+
+
+ Ooops!
+ It seems you've gone somewhere you shouldn't! 404 NOT FOUND!
+
+ This can happen if you enter the spotify id wrong and search, go back and check it!
+ Spotify IDs look like this : 37i9dQZF1DX4UtSsGT1Sbe - 22 characters and can be got by clicking share and "Copy (Playlist|Album) Link".
+ Shameless self-promotion : Follow me on twitch - iMartynOnTwitch, oddly enough, I do a lot of twitchsings!
+
+
+
+
+
+
+
+