twitch: fix web server

This commit is contained in:
Chris Sexton 2022-05-20 17:09:39 -04:00
parent a4108ecc82
commit 69cee328e7
1 changed files with 5 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/go-chi/chi/v5"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -11,7 +12,6 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
@ -87,20 +87,19 @@ func New(b bot.Bot) *TwitchPlugin {
func (p *TwitchPlugin) registerWeb() { func (p *TwitchPlugin) registerWeb() {
r := chi.NewRouter() r := chi.NewRouter()
r.HandleFunc("/", p.serveStreaming) r.HandleFunc("/{user}", p.serveStreaming)
p.bot.RegisterWeb(r, "/isstreaming") p.bot.RegisterWeb(r, "/isstreaming")
} }
func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) { func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) {
pathParts := strings.Split(r.URL.Path, "/") userName := chi.URLParam(r, "user")
if len(pathParts) != 3 { if userName == "" {
fmt.Fprint(w, "User not found.") fmt.Fprint(w, "User not found.")
return return
} }
twitcher := p.twitchList[pathParts[2]] twitcher := p.twitchList[userName]
if twitcher == nil { if twitcher == nil {
fmt.Fprint(w, "User not found.") fmt.Fprint(w, "User not found.")
return return
} }