forked from owncast/owncast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (125 loc) · 4.21 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"os"
"strconv"
"github.com/owncast/owncast/logging"
log "github.com/sirupsen/logrus"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/core"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/metrics"
"github.com/owncast/owncast/router"
"github.com/owncast/owncast/utils"
)
var (
dbFile = flag.String("database", "", "Path to the database file.")
logDirectory = flag.String("logdir", "", "Directory where logs will be written to")
backupDirectory = flag.String("backupdir", "", "Directory where backups will be written to")
enableDebugOptions = flag.Bool("enableDebugFeatures", false, "Enable additional debugging options.")
enableVerboseLogging = flag.Bool("enableVerboseLogging", false, "Enable additional logging.")
restoreDatabaseFile = flag.String("restoreDatabase", "", "Restore an Owncast database backup")
newStreamKey = flag.String("streamkey", "", "Set your stream key/admin password")
webServerPortOverride = flag.String("webserverport", "", "Force the web server to listen on a specific port")
webServerIPOverride = flag.String("webserverip", "", "Force web server to listen on this IP address")
rtmpPortOverride = flag.Int("rtmpport", 0, "Set listen port for the RTMP server")
)
func main() {
flag.Parse()
if *logDirectory != "" {
config.LogDirectory = *logDirectory
}
if *backupDirectory != "" {
config.BackupDirectory = *backupDirectory
}
// Create the data directory if needed
if !utils.DoesFileExists("data") {
if err := os.Mkdir("./data", 0700); err != nil {
log.Fatalln("Cannot create data directory", err)
}
}
// Recreate the temp dir
if utils.DoesFileExists(config.TempDir) {
err := os.RemoveAll(config.TempDir)
if err != nil {
log.Fatalln("Unable to remove temp dir!")
}
}
if err := os.Mkdir(config.TempDir, 0700); err != nil {
log.Fatalln("Unable to create temp dir!", err)
}
configureLogging(*enableDebugOptions, *enableVerboseLogging)
log.Infoln(config.GetReleaseString())
// Allows a user to restore a specific database backup
if *restoreDatabaseFile != "" {
databaseFile := config.DatabaseFilePath
if *dbFile != "" {
databaseFile = *dbFile
}
if err := utils.Restore(*restoreDatabaseFile, databaseFile); err != nil {
log.Fatalln(err)
}
log.Println("Database has been restored. Restart Owncast.")
log.Exit(0)
}
config.EnableDebugFeatures = *enableDebugOptions
if *dbFile != "" {
config.DatabaseFilePath = *dbFile
}
if err := data.SetupPersistence(config.DatabaseFilePath); err != nil {
log.Fatalln("failed to open database", err)
}
handleCommandLineFlags()
// starts the core
if err := core.Start(); err != nil {
log.Fatalln("failed to start the core package", err)
}
go metrics.Start(core.GetStatus)
if err := router.Start(); err != nil {
log.Fatalln("failed to start/run the router", err)
}
}
func handleCommandLineFlags() {
if *newStreamKey != "" {
if err := data.SetStreamKey(*newStreamKey); err != nil {
log.Errorln("Error setting your stream key.", err)
log.Exit(1)
} else {
log.Infoln("Stream key changed")
}
}
// Set the web server port
if *webServerPortOverride != "" {
portNumber, err := strconv.Atoi(*webServerPortOverride)
if err != nil {
log.Warnln(err)
return
}
log.Println("Saving new web server port number to", portNumber)
if err := data.SetHTTPPortNumber(float64(portNumber)); err != nil {
log.Errorln(err)
}
}
config.WebServerPort = data.GetHTTPPortNumber()
// Set the web server ip
if *webServerIPOverride != "" {
log.Println("Saving new web server listen IP address to", *webServerIPOverride)
if err := data.SetHTTPListenAddress(*webServerIPOverride); err != nil {
log.Errorln(err)
}
}
config.WebServerIP = data.GetHTTPListenAddress()
// Set the rtmp server port
if *rtmpPortOverride > 0 {
log.Println("Saving new RTMP server port number to", *rtmpPortOverride)
if err := data.SetRTMPPortNumber(float64(*rtmpPortOverride)); err != nil {
log.Errorln(err)
}
}
}
func configureLogging(enableDebugFeatures bool, enableVerboseLogging bool) {
logging.Setup(enableDebugFeatures, enableVerboseLogging)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
}