@@ -2,15 +2,11 @@ package main
22
33import (
44 "context"
5- "fmt"
65 log "github.com/sirupsen/logrus"
76 "github.com/spf13/pflag"
8- "github.com/spf13/viper"
9- "net/url"
107 "os"
118 "os/signal"
129 "path/filepath"
13- "reflect"
1410 "sync"
1511 "syscall"
1612 "time"
@@ -23,110 +19,36 @@ import (
2319 "transcoder/version"
2420)
2521
26- type CmdLineOpts struct {
27- Database repository.SQLServerConfig `mapstructure:"database"`
28- Web web.WebServerConfig `mapstructure:"web"`
29- Scheduler scheduler.SchedulerConfig `mapstructure:"scheduler"`
30- NoUpdateMode bool `mapstructure:"noUpdateMode"`
31- NoUpdates bool `mapstructure:"noUpdates"`
32- UpdateCheckInterval time.Duration `mapstructure:"updateCheckInterval"`
33- }
34-
3522var (
3623 ApplicationName = "transcoderd-server"
37- showVersion = false
38- opts CmdLineOpts
24+ opts cmd.CommandLineConfig
3925)
4026
4127func init () {
42- //Scheduler
43- var verbose bool
44- pflag .BoolVar (& showVersion , "version" , false , "Print version and exit" )
45- pflag .BoolVar (& verbose , "verbose" , false , "Enable verbose logging" )
46- pflag .Duration ("updateCheckInterval" , time .Minute * 15 , "Check for updates every X duration" )
47-
48- pflag .Duration ("scheduler.scheduleTime" , time .Minute * 5 , "Execute the scheduling loop every X seconds" )
49- pflag .Duration ("scheduler.jobTimeout" , time .Hour * 24 , "Requeue jobs that are running for more than X minutes" )
50- pflag .String ("scheduler.sourcePath" , "/data/current" , "Download path" )
51- pflag .Int64 ("scheduler.minFileSize" , 1e+8 , "Min File Size" )
52-
53- //WebConfig Config
54-
55- cmd .WebFlags ()
56-
57- //DB Config
58- pflag .String ("database.Driver" , "postgres" , "DB Driver" )
59- pflag .String ("database.Host" , "localhost" , "DB Host" )
60- pflag .Int ("database.port" , 5432 , "DB Port" )
61- pflag .String ("database.User" , "postgres" , "DB User" )
62- pflag .String ("database.Password" , "postgres" , "DB Password" )
63- pflag .String ("database.Scheme" , "server" , "DB Scheme" )
64- update .PFlags ()
65- pflag .Usage = usage
66-
67- //pflag.Parse()
68- //viper.SetConfigFile("config")
69- viper .SetConfigType ("yaml" )
70- viper .AddConfigPath ("/etc/transcoderd/" )
71- viper .AddConfigPath ("$HOME/.transcoderd/" )
72- viper .AddConfigPath ("." )
73- viper .AutomaticEnv ()
74- viper .SetEnvPrefix ("TR" )
75- err := viper .ReadInConfig ()
76- if err != nil {
77- switch err .(type ) {
78- case viper.ConfigFileNotFoundError :
79- log .Warnf ("No Config File Found" )
80- default :
81- log .Panic (err )
82- }
83- }
84- pflag .Parse ()
85- log .SetFormatter (& log.TextFormatter {
86- ForceColors : true ,
87- EnvironmentOverrideColors : true ,
88- })
89- if verbose {
90- log .SetLevel (log .DebugLevel )
91- } else {
92- log .SetLevel (log .InfoLevel )
93- }
94- viper .BindPFlags (pflag .CommandLine )
95-
96- urlAndDurationDecoder := viper .DecodeHook (func (source reflect.Type , target reflect.Type , data interface {}) (interface {}, error ) {
97- if source .Kind () != reflect .String {
98- return data , nil
99- }
100- if target == reflect .TypeOf (url.URL {}) {
101- url , err := url .Parse (data .(string ))
102- return url , err
103- } else if target == reflect .TypeOf (time .Duration (5 )) {
104- return time .ParseDuration (data .(string ))
105- }
106- return data , nil
107-
108- })
109- err = viper .Unmarshal (& opts , urlAndDurationDecoder )
110- if err != nil {
111- log .Panic (err )
112- }
28+ cmd .CommonFlags ()
11329
114- //Fix Paths
115- opts .Scheduler .SourcePath = filepath .Clean (opts .Scheduler .SourcePath )
116- helper .CheckPath (opts .Scheduler .SourcePath )
117- }
30+ pflag .Duration ("server.scheduler.scheduleTime" , time .Minute * 5 , "Execute the scheduling loop every X seconds" )
31+ pflag .Duration ("server.scheduler.jobTimeout" , time .Hour * 24 , "Requeue jobs that are running for more than X minutes" )
32+ pflag .String ("server.scheduler.sourcePath" , "/data/current" , "Download path" )
33+ pflag .Int64 ("server.scheduler.minFileSize" , 1e+8 , "Min File Size" )
34+ pflag .String ("server.database.Driver" , "postgres" , "DB Driver" )
35+ pflag .String ("server.database.Host" , "localhost" , "DB Host" )
36+ pflag .Int ("server.database.port" , 5432 , "DB Port" )
37+ pflag .String ("server.database.User" , "postgres" , "DB User" )
38+ pflag .String ("server.database.Password" , "postgres" , "DB Password" )
39+ pflag .String ("server.database.Scheme" , "server" , "DB Scheme" )
11840
119- func usage () {
120- fmt .Fprintf (os .Stderr , "Usage: %s [OPTION]...\n " , os .Args [0 ])
121- pflag .PrintDefaults ()
122- os .Exit (0 )
41+ cmd .ViperConfig (& opts )
42+
43+ //Fix Paths
44+ serverConfig := opts .Server
45+ serverConfig .Scheduler .SourcePath = filepath .Clean (serverConfig .Scheduler .SourcePath )
46+ helper .CheckPath (serverConfig .Scheduler .SourcePath )
12347}
12448
12549func main () {
126- if showVersion {
127- version .LogVersion ()
128- os .Exit (0 )
129- }
50+ opts .PrintVersion ()
51+
13052 wg := & sync.WaitGroup {}
13153 ctx , cancel := context .WithCancel (context .Background ())
13254 sigs := make (chan os.Signal , 1 )
@@ -159,7 +81,7 @@ func main() {
15981func applicationRun (wg * sync.WaitGroup , ctx context.Context , updater * update.Updater ) {
16082 //Repository persist
16183 var repo repository.Repository
162- repo , err := repository .NewSQLRepository (opts .Database )
84+ repo , err := repository .NewSQLRepository (opts .Server . Database )
16385 if err != nil {
16486 log .Panic (err )
16587 }
@@ -169,14 +91,14 @@ func applicationRun(wg *sync.WaitGroup, ctx context.Context, updater *update.Upd
16991 }
17092
17193 //Scheduler
172- scheduler , err := scheduler .NewScheduler (opts .Scheduler , repo )
94+ scheduler , err := scheduler .NewScheduler (opts .Server . Scheduler , repo )
17395 if err != nil {
17496 log .Panic (err )
17597 }
17698 scheduler .Run (wg , ctx )
17799
178100 //WebConfig Server
179- var webServer * web.WebServer
101+ var webServer * web.Server
180102 webServer = web .NewWebServer (opts .Web , scheduler , updater )
181103 webServer .Run (wg , ctx )
182104}
0 commit comments