From f5748636014c4c91d8dd09c26e59751593b1b2c8 Mon Sep 17 00:00:00 2001 From: Martyn Ranyard Date: Tue, 30 Jun 2020 17:30:05 +0200 Subject: [PATCH] gofmt Signed-off-by: Martyn Ranyard --- internal/irc/irc.go | 1 + internal/webserver/webserver.go | 95 ++++++++++++++++----------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/internal/irc/irc.go b/internal/irc/irc.go index da004ef..849cebe 100644 --- a/internal/irc/irc.go +++ b/internal/irc/irc.go @@ -181,6 +181,7 @@ func (bb *KardBot) HandleChat() error { case "PRIVMSG": msg := matches[4] rgb.GPrintf("[%s] %s: %s\n", TimeStamp(), userName, msg) + rgb.GPrintf("[%s] raw line: %s\n", TimeStamp(), line) // parse commands from user message cmdMatches := CmdRegex.FindStringSubmatch(msg) diff --git a/internal/webserver/webserver.go b/internal/webserver/webserver.go index 0039962..8ab9268 100755 --- a/internal/webserver/webserver.go +++ b/internal/webserver/webserver.go @@ -7,37 +7,37 @@ import ( "github.com/gorilla/handlers" "github.com/gorilla/mux" + "encoding/json" "fmt" "html/template" + "io/ioutil" "net/http" "net/url" "os" "strings" "time" - "encoding/json" - "io/ioutil" ) //var store = sessions.NewCookieStore(os.Getenv("SESSION_KEY")) type twitchauthresponse struct { - Access_token string `json: "access_token"` - Expires_in int `json: "expires_in"` - Refresh_token string `json: "refresh_token"` - Scope []string `json: "scope"` - Token_type string `json: "token_type"` + Access_token string `json: "access_token"` + Expires_in int `json: "expires_in"` + Refresh_token string `json: "refresh_token"` + Scope []string `json: "scope"` + Token_type string `json: "token_type"` } type twitchUser struct { - Id string `json: "id"` - Login string `json: "login"` - Display_name string `json: "display_name"` - Type string `json: "type"` - Broadcaster_type string `json: "affiliate"` - Description string `json: "description"` + Id string `json: "id"` + Login string `json: "login"` + Display_name string `json: "display_name"` + Type string `json: "type"` + Broadcaster_type string `json: "affiliate"` + Description string `json: "description"` Profile_image_url string `json: "profile_image_url"` Offline_image_url string `json: "offline_image_url"` - View_count int `json: "view_count"` + View_count int `json: "view_count"` } type twitchUsersBigResponse struct { @@ -102,7 +102,7 @@ func TemplateHandler(response http.ResponseWriter, request *http.Request) { // NotFoundHandler(response, request) // return } - var td = TemplateData{ircBot.Prompts[rand.Intn(len(ircBot.Prompts))], len(ircBot.Prompts), ircBot.ActiveChannels(), 0, ircBot.AppCredentials.ClientID, "https://"+ircBot.Config.ExternalUrl} + var td = TemplateData{ircBot.Prompts[rand.Intn(len(ircBot.Prompts))], len(ircBot.Prompts), ircBot.ActiveChannels(), 0, ircBot.AppCredentials.ClientID, "https://" + ircBot.Config.ExternalUrl} err = tmpl.Execute(response, td) if err != nil { http.Error(response, err.Error(), http.StatusInternalServerError) @@ -184,37 +184,37 @@ func UnauthorizedHandler(response http.ResponseWriter, request *http.Request) { tmpl.Execute(response, nil) } -func twitchHTTPClient(call string, bearer string) (string,error) { +func twitchHTTPClient(call string, bearer string) (string, error) { url := "https://api.twitch.tv/helix/" + call var bearerHeader = "Bearer " + bearer - req, err := http.NewRequest("GET", url, nil) - req.Header.Add("Authorization", bearerHeader) + req, err := http.NewRequest("GET", url, nil) + req.Header.Add("Authorization", bearerHeader) - client := &http.Client{} + client := &http.Client{} resp, err := client.Do(req) if err != nil { - return "",err + return "", err } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) - return string([]byte(body)), nil + return string([]byte(body)), nil } func TwitchAdminHandler(response http.ResponseWriter, request *http.Request) { - + vars := mux.Vars(request) - if (vars["code"] != "") { + if vars["code"] != "" { response.Header().Add("Content-type", "text/plain") resp, err := http.PostForm( - "https://id.twitch.tv/oauth2/token", + "https://id.twitch.tv/oauth2/token", url.Values{ - "client_id": {ircBot.AppCredentials.ClientID}, + "client_id": {ircBot.AppCredentials.ClientID}, "client_secret": {ircBot.AppCredentials.Password}, - "code": {vars["code"]}, - "grant_type": {"authorization_code"}, - "redirect_uri": {"https://"+ircBot.Config.ExternalUrl+"/twitchadmin"}}) + "code": {vars["code"]}, + "grant_type": {"authorization_code"}, + "redirect_uri": {"https://" + ircBot.Config.ExternalUrl + "/twitchadmin"}}) if err != nil { response.WriteHeader(500) response.Header().Add("Content-type", "text/plain") @@ -265,37 +265,36 @@ func TwitchAdminHandler(response http.ResponseWriter, request *http.Request) { } user := usersObject.Data[0] - - magicCode := ircBot.ReadOrCreateChannelKey(user.Login) - url := "https://"+ircBot.Config.ExternalUrl+"/admin/"+user.Login+"/"+magicCode - http.Redirect(response, request, url, http.StatusFound) + magicCode := ircBot.ReadOrCreateChannelKey(user.Login) + url := "https://" + ircBot.Config.ExternalUrl + "/admin/" + user.Login + "/" + magicCode + http.Redirect(response, request, url, http.StatusFound) } else { - + fmt.Fprintf(response, "I'm not okay jack! %v \n", vars) - for key, val := range(vars) { + for key, val := range vars { fmt.Fprint(response, "%s = %s\n", key, val) } } } -func TwitchBackendHandler(response http.ResponseWriter, request *http.Request){ +func TwitchBackendHandler(response http.ResponseWriter, request *http.Request) { response.Header().Add("Content-type", "text/plain") vars := mux.Vars(request) // fmt.Fprintf(response, "I'm okay jack! %v \n", vars) // for key, val := range(vars) { // fmt.Fprint(response, "%s = %s\n", key, val) // } - - if (vars["code"] != "") { + + if vars["code"] != "" { // https://id.twitch.tv/oauth2/token // ?client_id= - // &client_secret= - // &code= - // &grant_type=authorization_code + // &client_secret= + // &code= + // &grant_type=authorization_code // &redirect_uri= - + // ircBot.AppCredentials.ClientID // ircBot.AppCredentials.Password // vars["oauthtoken"] @@ -303,13 +302,13 @@ func TwitchBackendHandler(response http.ResponseWriter, request *http.Request){ // "https://"+ircBot.Config.ExternalUrl+/twitchadmin fmt.Println("Asking twitch for more...") resp, err := http.PostForm( - "https://id.twitch.tv/oauth2/token", + "https://id.twitch.tv/oauth2/token", url.Values{ - "client_id": {ircBot.AppCredentials.ClientID}, + "client_id": {ircBot.AppCredentials.ClientID}, "client_secret": {ircBot.AppCredentials.Password}, - "code": {vars["code"]}, - "grant_type": {"authorization_code"}, - "redirect_uri": {"https://"+ircBot.Config.ExternalUrl+"/twitchadmin"}}) + "code": {vars["code"]}, + "grant_type": {"authorization_code"}, + "redirect_uri": {"https://" + ircBot.Config.ExternalUrl + "/twitchadmin"}}) if err != nil { response.Header().Add("Content-type", "text/plain") fmt.Fprint(response, "ERROR: "+err.Error()) @@ -340,8 +339,8 @@ func HandleHTTP(passedIrcBot *irc.KardBot) { r.HandleFunc("/admin/{channel}/{key}", AdminHandler) //r.HandleFunc("/twitchadmin", TwitchAdminHandler) //r.HandleFunc("/twitchtobackend", TwitchBackendHandler) - r.Path("/twitchtobackend").Queries("access_token","{access_token}","scope","{scope}","token_type","{token_type}").HandlerFunc(TwitchBackendHandler) - r.Path("/twitchadmin").Queries("code","{code}","scope","{scope}").HandlerFunc(TwitchAdminHandler) + r.Path("/twitchtobackend").Queries("access_token", "{access_token}", "scope", "{scope}", "token_type", "{token_type}").HandlerFunc(TwitchBackendHandler) + r.Path("/twitchadmin").Queries("code", "{code}", "scope", "{scope}").HandlerFunc(TwitchAdminHandler) http.Handle("/", r) srv := &http.Server{ Handler: loggedRouter,