-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (80 loc) · 2.72 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
package main
import (
"context"
"flag"
"os"
"path"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/gatsby-tv/dapper/api"
_ "github.com/gatsby-tv/dapper/docs"
"github.com/gatsby-tv/dapper/ipfs"
)
// Folder to search for config file
const configFileLocation = "."
// Name of config file (without extension)
const configFileName = "configuration"
// Type of config file
const configFileExtension = "toml"
func main() {
log.Info().Msgf("Dapper version: %s-%s", CurrentVersionNumber, CurrentCommit)
readConfigFile()
log.Trace().Msg("Successfully loaded config")
portPtr := flag.Int("p", 10000, "Port to listen for requests on.")
flag.Parse()
// Verify the given port is a valid port number
if *portPtr < 1 || *portPtr > 65535 {
log.Fatal().Msg("Invalid port specified.")
}
// Create video scratch path if it does not exist
if _, err := os.Stat(path.Join(viper.GetString("Videos.TempVideoStorageFolder"), api.VideoScratchFolder)); os.IsNotExist(err) {
err := os.Mkdir(path.Join(viper.GetString("Videos.TempVideoStorageFolder"), api.VideoScratchFolder), 0755)
if err != nil {
log.Fatal().Msgf("Failed setting up video directory: %s", err)
}
}
// Setup memory map for keeping track of videos being processed
api.EncodingVideos.Videos = make(map[string]api.EncodingVideo)
startDaemon(*portPtr)
}
// Read in config values to viper and check that necessary values are set
func readConfigFile() {
viper.SetConfigName(configFileName)
viper.SetConfigType(configFileExtension)
viper.AddConfigPath(configFileLocation)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found ignore error and use defaults
log.Info().Msg("Configuration file not found, using defaults.")
} else {
log.Fatal().Msg(err.Error())
}
}
// Verify necessary config values are set
if videoDir := viper.GetString("Videos.TempVideoStorageFolder"); videoDir == "" {
videoDir, err := os.MkdirTemp(os.TempDir(), "dapper-*")
if err != nil {
log.Fatal().Msg(err.Error())
}
viper.Set("Videos.TempVideoStorageFolder", videoDir)
}
// If none was set, use the one on the path
if ffmpegDir := viper.GetString("ffmpeg.ffmpegDir"); ffmpegDir == "" {
viper.Set("ffmpeg.ffmpegDir", "ffmpeg")
}
if ffmpegDir := viper.GetString("ffmpeg.ffprobeDir"); ffmpegDir == "" {
viper.Set("ffmpeg.ffprobeDir", "ffprobe")
}
}
// Setup IPFS and start listening for requests
func startDaemon(port int) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.Trace().Msg("Setting up IPFS")
err := ipfs.StartIPFS(ctx)
if err != nil {
log.Fatal().Msgf("Failed to start IPFS: %s", err)
}
log.Info().Msg("Ready for requests")
api.HandleRequests(port)
}