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 + + + + + + + + + +
+
+
+

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!

+
+ +
+ + + + + \ No newline at end of file diff --git a/web/cover.css b/web/cover.css new file mode 100644 index 0000000..b275c1c --- /dev/null +++ b/web/cover.css @@ -0,0 +1,106 @@ +/* +* Globals +*/ + +/* Links */ +a, +a:focus, +a:hover { + color: #fff; +} + +/* Custom default button */ +.btn-secondary, +.btn-secondary:hover, +.btn-secondary:focus { + color: #333; + text-shadow: none; /* Prevent inheritance from body */ + background-color: #fff; + border: .05rem solid #fff; +} + + +/* +* Base structure +*/ + +html, +body { + height: 100%; + background-color: #333; +} + +body { + display: -ms-flexbox; + display: flex; + color: #fff; + text-shadow: 0 .05rem .1rem rgba(0, 0, 0, .5); + box-shadow: inset 0 0 5rem rgba(0, 0, 0, .5); +} + +.cover-container { + max-width: 142em; +} + + +/* +* Header +*/ +.masthead { + margin-bottom: 2rem; +} + +.masthead-brand { + margin-bottom: 0; +} + +.nav-masthead .nav-link { + padding: .25rem 0; + font-weight: 700; + color: rgba(255, 255, 255, .5); + background-color: transparent; + border-bottom: .25rem solid transparent; +} + +.nav-masthead .nav-link:hover, +.nav-masthead .nav-link:focus { + border-bottom-color: rgba(255, 255, 255, .25); +} + +.nav-masthead .nav-link + .nav-link { + margin-left: 1rem; +} + +.nav-masthead .active { + color: #fff; + border-bottom-color: #fff; +} + +@media (min-width: 48em) { + .masthead-brand { + float: left; + } + .nav-masthead { + float: right; + } +} + + +/* +* Cover +*/ +.cover { + padding: 0 1.5rem; +} +.cover .btn-lg { + padding: .75rem 1.25rem; + font-weight: 700; +} + + +/* +* Footer +*/ +.mastfoot { + color: rgba(255, 255, 255, .5); +} \ No newline at end of file diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..4102077 --- /dev/null +++ b/web/index.html @@ -0,0 +1,90 @@ + + + + + + + + + Karaokards + + + + + + + + + +
+
+
+

k8s-zoo

+ +
+
+
+

Karaokards!!!

+

Random prompt for you : {{.Prompt}}

+

There are a total of {{.AvailCount}} prompts available.

+
+ +
+ + + + + \ No newline at end of file