2015-09-11 14:37:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-11-01 15:35:01 +00:00
|
|
|
"errors"
|
2015-09-24 15:16:13 +00:00
|
|
|
fmtlog "log"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2015-11-01 15:35:01 +00:00
|
|
|
"runtime"
|
2015-09-24 15:16:13 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-09-24 12:32:37 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-09-15 19:38:54 +00:00
|
|
|
"github.com/emilevauge/traefik/middlewares"
|
2015-09-12 13:10:03 +00:00
|
|
|
"github.com/thoas/stats"
|
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2015-09-11 14:37:13 +00:00
|
|
|
)
|
|
|
|
|
2015-09-11 23:55:10 +00:00
|
|
|
var (
|
2016-01-13 21:45:49 +00:00
|
|
|
globalConfigFile = kingpin.Arg("conf", "Main configration file.").Default("traefik.toml").String()
|
|
|
|
version = kingpin.Flag("version", "Get Version.").Short('v').Bool()
|
|
|
|
metrics = stats.New()
|
|
|
|
oxyLogger = &OxyLogger{}
|
2015-09-11 23:55:10 +00:00
|
|
|
)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
func main() {
|
2015-09-25 22:20:45 +00:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2015-10-14 20:04:56 +00:00
|
|
|
kingpin.Version(Version + " built on the " + BuildDate)
|
2015-09-11 23:55:10 +00:00
|
|
|
kingpin.Parse()
|
2015-09-19 11:02:59 +00:00
|
|
|
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
|
|
|
// load global configuration
|
2015-09-24 15:16:13 +00:00
|
|
|
globalConfiguration := LoadFileConfig(*globalConfigFile)
|
2015-09-11 14:37:13 +00:00
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
loggerMiddleware := middlewares.NewLogger(globalConfiguration.AccessLogsFile)
|
2015-09-12 13:20:56 +00:00
|
|
|
defer loggerMiddleware.Close()
|
|
|
|
|
2015-09-11 14:37:13 +00:00
|
|
|
// logging
|
2015-09-24 15:16:13 +00:00
|
|
|
level, err := log.ParseLevel(strings.ToLower(globalConfiguration.LogLevel))
|
2015-09-11 14:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error getting level", err)
|
|
|
|
}
|
2015-09-24 12:32:37 +00:00
|
|
|
log.SetLevel(level)
|
2015-09-11 14:48:52 +00:00
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
if len(globalConfiguration.TraefikLogsFile) > 0 {
|
|
|
|
fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
2015-09-12 13:20:56 +00:00
|
|
|
defer fi.Close()
|
2015-09-11 14:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error opening file", err)
|
2015-09-12 13:10:03 +00:00
|
|
|
} else {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.SetOutput(fi)
|
|
|
|
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true})
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-09-24 12:32:37 +00:00
|
|
|
} else {
|
|
|
|
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true})
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
2015-10-13 12:15:32 +00:00
|
|
|
log.Debugf("Global configuration loaded %+v", globalConfiguration)
|
2016-01-13 21:45:49 +00:00
|
|
|
server := NewServer(*globalConfiguration)
|
|
|
|
server.Start()
|
|
|
|
server.Close()
|
2015-09-25 22:20:45 +00:00
|
|
|
log.Info("Shutting down")
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 17:11:57 +00:00
|
|
|
// Invoke calls the specified method with the specified arguments on the specified interface.
|
|
|
|
// It uses the go(lang) reflect package.
|
2015-11-23 14:41:16 +00:00
|
|
|
func invoke(any interface{}, name string, args ...interface{}) ([]reflect.Value, error) {
|
2015-09-11 14:37:13 +00:00
|
|
|
inputs := make([]reflect.Value, len(args))
|
2015-09-15 20:32:09 +00:00
|
|
|
for i := range args {
|
2015-09-11 14:37:13 +00:00
|
|
|
inputs[i] = reflect.ValueOf(args[i])
|
|
|
|
}
|
2015-11-23 14:41:16 +00:00
|
|
|
method := reflect.ValueOf(any).MethodByName(name)
|
|
|
|
if method.IsValid() {
|
|
|
|
return method.Call(inputs), nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("Method not found: " + name)
|
2015-09-11 14:37:13 +00:00
|
|
|
}
|