traefik/traefik.go

75 lines
2.1 KiB
Go
Raw Normal View History

2015-09-11 14:37:13 +00:00
package main
import (
"errors"
fmtlog "log"
"os"
"reflect"
"runtime"
"strings"
log "github.com/Sirupsen/logrus"
"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 (
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
globalConfiguration := LoadFileConfig(*globalConfigFile)
2015-09-11 14:37: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
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)
}
log.SetLevel(level)
2015-09-11 14:48:52 +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 {
log.SetOutput(fi)
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true})
2015-09-11 14:37:13 +00:00
}
} else {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true})
2015-09-11 14:37:13 +00:00
}
log.Debugf("Global configuration loaded %+v", globalConfiguration)
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
}
// 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))
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
}