Impliment StartupTesting and started OAuth Server
Testing for IRC & Channels connection for debugging OAuth Server (because it's better to roll my own for this) internal/auth/oauth.go tests/startup_checks.go More tests to come for debugging Plan to impliment Logging in internal/stoage/logs for internal & UI debugging
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"streambot_twitch/internal/auth"
|
||||||
"streambot_twitch/internal/chat"
|
"streambot_twitch/internal/chat"
|
||||||
"streambot_twitch/internal/commands"
|
"streambot_twitch/internal/commands"
|
||||||
twitchapi "streambot_twitch/internal/twitchapi"
|
twitchapi "streambot_twitch/internal/twitchapi"
|
||||||
@@ -12,11 +13,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
go func() {
|
||||||
|
if err := oauth.StartOAuthServer(":8080"); err != nil {
|
||||||
|
log.Fatalf("OAuth server error: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
log.Println("OAuth server started on port 8080")
|
||||||
|
|
||||||
botUsername := os.Getenv("nz_chatterbot")
|
botUsername := os.Getenv("nz_chatterbot")
|
||||||
botOAuth :+ os.Getenv("bot_oAuth") // replace this
|
botOAuth :+ os.Getenv("BOT_OAUTH")
|
||||||
channel := os Getenv("BRYCEFROMNZ101")
|
channel := os Getenv("brycefromnz101")
|
||||||
clientID := os.Getenv("Twitch_client_id") // replace this
|
clientID := os.Getenv("TWITCH_CLIENTID")
|
||||||
clientSecret := os.Getenv("Twitch_Client_Secret") // replace this
|
clientSecret := os.Getenv("TWITCH_CLIENT_SECRET")
|
||||||
botChannel := botUsername
|
botChannel := botUsername
|
||||||
|
|
||||||
if botUsername == "" || botOAuth == "" || channel == "" || clientID == "" || clientSecret == "" {
|
if botUsername == "" || botOAuth == "" || channel == "" || clientID == "" || clientSecret == "" {
|
||||||
|
46
internal/auth/oauth.go
Normal file
46
internal/auth/oauth.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package oauth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
clientID = os.Getenv("TWITCH_CLIENT_ID")
|
||||||
|
clientSecret = os.Getenv("TWITCH_CLIENT_SECRET")
|
||||||
|
redirectURI = "http://localhost:8080/callback"
|
||||||
|
|
||||||
|
scopes = []string{
|
||||||
|
"chat:read",
|
||||||
|
"chat:edit",
|
||||||
|
"channel:read:subscriptions",
|
||||||
|
"user:read:email",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type TokenResponse struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
ExpiresIn int `json:"expires_in"`
|
||||||
|
Scope []string `json:"scope"`
|
||||||
|
TokenType string `json:"token_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// start http server for oauth login & callback
|
||||||
|
func StartOAuthServer(addr string) error {
|
||||||
|
if clientID == "" || clientSecret == "" {
|
||||||
|
return fmt.Errorf("TWITCH_CLIENT_ID and TWITCH_CLIENT_SECRET must be set")
|
||||||
|
}
|
||||||
|
|
||||||
|
http.HandleFunc("/", handleIndex)
|
||||||
|
http.HandleFunc("/callback", handleCallback)
|
||||||
|
|
||||||
|
log.Printf("OAuth server listening on %s", addr)
|
||||||
|
return http.ListenAndServe(addr, nil)
|
||||||
|
}
|
||||||
|
|
52
internal/tests/startup_checks.go
Normal file
52
internal/tests/startup_checks.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
twitch "github.com/gempir/go-twitch-irc/v4"
|
||||||
|
twitchapi "streambot_twitch/internal/twitchapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// check Twitch API connectivity
|
||||||
|
func TestTwitchAPI(client *twitchapi.Client, streamerChannel string) error {
|
||||||
|
_, err := client.GetUserID(streamerChannel)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Twitch API test failed: %w", err)
|
||||||
|
}
|
||||||
|
log.Println("Twitch API connection successful")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// check IRC connection to channels
|
||||||
|
func TestIRCConnection(botClient *twitch.Client, channels []string) error {
|
||||||
|
ctx, channel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
connected := make(chan struct{})
|
||||||
|
|
||||||
|
botClient.OnConnect(func() {
|
||||||
|
log.Println("IRC connected successfully")
|
||||||
|
close(connected)
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, ch := range channels {
|
||||||
|
botClient.Join(ch)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := botClient.Connect(); err != nil {
|
||||||
|
log.Printf("IRC connection error: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select{
|
||||||
|
case <-connected:
|
||||||
|
log.Println("IRC connecton test successful")
|
||||||
|
return nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
return fmt.Errorf("IRC connection test timed out")
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user