This repository has been archived by the owner on May 3, 2023. It is now read-only.
/
main.go
102 lines (85 loc) · 2.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package botsbyuberswe
import (
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/joho/godotenv"
"github.com/syndtr/goleveldb/leveldb"
"io"
"log"
"math/rand"
"net/http"
"os"
"time"
)
var (
cookieName = "botbyuber"
defaultBot = "botbyuber"
clients []Wconn // connected websocket clients
broadcast = make(chan WebsocketMessage) // broadcast channel
universalBot = make(chan ConnectChannel) // broadcast channel
upgrader websocket.Upgrader
db *leveldb.DB
clientID string
clientSecret string
redirectURL = "/callback"
botRedirectURL = "/bot/callback"
// The twitch IRC clients for users
clientConnections = make(map[string]User)
// The botbyuber bot and other custom bots that can write to channels
botConnections = make(map[string]Bot)
r *mux.Router
universalBotTwitchID = ""
universalConnectedChannels []string
)
// Init is called before run and currently makes a seed for random number generators
func Init() {
rand.Seed(time.Now().UnixNano())
}
// Run runs the application, it loads the config, starts everything and keeps it running
func Run() {
// Load environmental variables
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
clientID = os.Getenv("CLIENT_ID")
clientSecret = os.Getenv("CLIENT_SECRET")
cookieName = os.Getenv("COOKIE_NAME")
defaultBot = os.Getenv("UNIVERSAL_BOT")
// Log handling
f, err := os.OpenFile("uberswe.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
wrt := io.MultiWriter(os.Stdout, f)
log.SetOutput(wrt)
// Database handling
db, err = leveldb.OpenFile("uberswe.db", nil)
if err != nil {
panic(err)
}
defer db.Close()
// Mux router
r = mux.NewRouter()
// Websockets
// starts it's own thread for websocket connections
initWebsockets()
// Initialize routes
routes()
// Refresh tokens every 10 min
go refreshHandler()
// Connect to twitch channels
go twitchIRCHandler()
// Connect to needed channels with the main IRC bot botbyuber
go handleMainBotConnects()
log.Println("Listening on port 8010")
srv := &http.Server{
Handler: r,
Addr: ":8010",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}