Logrus logging backend https://github.com/EmileVauge/traefik/issues/15
This commit is contained in:
parent
35a1454082
commit
f5ddbcbcde
11 changed files with 83 additions and 98 deletions
14
Godeps/Godeps.json
generated
14
Godeps/Godeps.json
generated
|
@ -1,9 +1,6 @@
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/emilevauge/traefik",
|
"ImportPath": "github.com/emilevauge/traefik",
|
||||||
"GoVersion": "go1.5",
|
"GoVersion": "go1.4.2",
|
||||||
"Packages": [
|
|
||||||
"./..."
|
|
||||||
],
|
|
||||||
"Deps": [
|
"Deps": [
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/BurntSushi/toml",
|
"ImportPath": "github.com/BurntSushi/toml",
|
||||||
|
@ -13,6 +10,11 @@
|
||||||
"ImportPath": "github.com/BurntSushi/ty",
|
"ImportPath": "github.com/BurntSushi/ty",
|
||||||
"Rev": "6add9cd6ad42d389d6ead1dde60b4ad71e46fd74"
|
"Rev": "6add9cd6ad42d389d6ead1dde60b4ad71e46fd74"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/Sirupsen/logrus",
|
||||||
|
"Comment": "v0.8.7",
|
||||||
|
"Rev": "418b41d23a1bf978c06faea5313ba194650ac088"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/alecthomas/template",
|
"ImportPath": "github.com/alecthomas/template",
|
||||||
"Rev": "b867cc6ab45cece8143cfcc6fc9c77cf3f2c23c0"
|
"Rev": "b867cc6ab45cece8143cfcc6fc9c77cf3f2c23c0"
|
||||||
|
@ -95,10 +97,6 @@
|
||||||
"ImportPath": "github.com/mailgun/timetools",
|
"ImportPath": "github.com/mailgun/timetools",
|
||||||
"Rev": "fd192d755b00c968d312d23f521eb0cdc6f66bd0"
|
"Rev": "fd192d755b00c968d312d23f521eb0cdc6f66bd0"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"ImportPath": "github.com/op/go-logging",
|
|
||||||
"Rev": "e8d5414f0947014548c2334044a0fac13187dfee"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/thoas/stats",
|
"ImportPath": "github.com/thoas/stats",
|
||||||
"Rev": "54ed61c2b47e263ae2f01b86837b0c4bd1da28e8"
|
"Rev": "54ed61c2b47e263ae2f01b86837b0c4bd1da28e8"
|
||||||
|
|
16
adapters.go
16
adapters.go
|
@ -2,25 +2,27 @@
|
||||||
Copyright
|
Copyright
|
||||||
*/
|
*/
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/mailgun/oxy/utils"
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/mailgun/oxy/utils"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OxyLogger struct{
|
type OxyLogger struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oxylogger *OxyLogger) Infof(format string, args ...interface{}) {
|
func (oxylogger *OxyLogger) Infof(format string, args ...interface{}) {
|
||||||
log.Debug(format, args...)
|
log.Debugf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oxylogger *OxyLogger) Warningf(format string, args ...interface{}) {
|
func (oxylogger *OxyLogger) Warningf(format string, args ...interface{}) {
|
||||||
log.Warning(format, args...)
|
log.Warningf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oxylogger *OxyLogger) Errorf(format string, args ...interface{}) {
|
func (oxylogger *OxyLogger) Errorf(format string, args ...interface{}) {
|
||||||
log.Error(format, args...)
|
log.Errorf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ErrorHandler struct {
|
type ErrorHandler struct {
|
||||||
|
@ -40,4 +42,4 @@ func LoadDefaultConfig(gloablConfiguration *GlobalConfiguration) *mux.Router {
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
19
consul.go
19
consul.go
|
@ -1,16 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hashicorp/consul/api"
|
|
||||||
"text/template"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"strings"
|
|
||||||
"github.com/BurntSushi/ty/fun"
|
"github.com/BurntSushi/ty/fun"
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
|
"github.com/hashicorp/consul/api"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Key struct {
|
type Key struct {
|
||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ type ConsulProvider struct {
|
||||||
Filename string
|
Filename string
|
||||||
consulClient *api.Client
|
consulClient *api.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
var kvClient *api.KV
|
var kvClient *api.KV
|
||||||
|
|
||||||
var ConsulFuncMap = template.FuncMap{
|
var ConsulFuncMap = template.FuncMap{
|
||||||
|
@ -33,7 +34,7 @@ var ConsulFuncMap = template.FuncMap{
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
keysPairs = fun.Filter(func(key string) bool {
|
keysPairs = fun.Filter(func(key string) bool {
|
||||||
if (key == joinedKeys) {
|
if key == joinedKeys {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -51,7 +52,7 @@ var ConsulFuncMap = template.FuncMap{
|
||||||
},
|
},
|
||||||
"Last": func(key string) string {
|
"Last": func(key string) string {
|
||||||
splittedKey := strings.Split(key, "/")
|
splittedKey := strings.Split(key, "/")
|
||||||
return splittedKey[len(splittedKey) -2]
|
return splittedKey[len(splittedKey)-2]
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +65,7 @@ func NewConsulProvider() *ConsulProvider {
|
||||||
return consulProvider
|
return consulProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *ConsulProvider) Provide(configurationChan chan <- *Configuration) {
|
func (provider *ConsulProvider) Provide(configurationChan chan<- *Configuration) {
|
||||||
config := &api.Config{
|
config := &api.Config{
|
||||||
Address: provider.Endpoint,
|
Address: provider.Endpoint,
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
|
@ -106,10 +107,10 @@ func (provider *ConsulProvider) loadConsulConfig() *Configuration {
|
||||||
kvClient = provider.consulClient.KV()
|
kvClient = provider.consulClient.KV()
|
||||||
|
|
||||||
servicesName, _, _ := provider.consulClient.Catalog().Services(nil)
|
servicesName, _, _ := provider.consulClient.Catalog().Services(nil)
|
||||||
for serviceName, _ := range servicesName {
|
for serviceName := range servicesName {
|
||||||
catalogServices, _, _ := provider.consulClient.Catalog().Service(serviceName, "", nil)
|
catalogServices, _, _ := provider.consulClient.Catalog().Service(serviceName, "", nil)
|
||||||
for _, catalogService := range catalogServices {
|
for _, catalogService := range catalogServices {
|
||||||
services= append(services, catalogService)
|
services = append(services, catalogService)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
docker.go
11
docker.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/BurntSushi/ty/fun"
|
"github.com/BurntSushi/ty/fun"
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/cenkalti/backoff"
|
"github.com/cenkalti/backoff"
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -85,7 +86,7 @@ func (provider *DockerProvider) Provide(configurationChan chan<- *Configuration)
|
||||||
// log.Fatalf("Docker connection error")
|
// log.Fatalf("Docker connection error")
|
||||||
}
|
}
|
||||||
if event.Status == "start" || event.Status == "die" {
|
if event.Status == "start" || event.Status == "die" {
|
||||||
log.Debug("Docker event receveived %+v", event)
|
log.Debugf("Docker event receveived %+v", event)
|
||||||
configuration := provider.loadDockerConfig(dockerClient)
|
configuration := provider.loadDockerConfig(dockerClient)
|
||||||
if configuration != nil {
|
if configuration != nil {
|
||||||
configurationChan <- configuration
|
configurationChan <- configuration
|
||||||
|
@ -94,7 +95,7 @@ func (provider *DockerProvider) Provide(configurationChan chan<- *Configuration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
notify := func(err error, time time.Duration) {
|
notify := func(err error, time time.Duration) {
|
||||||
log.Error("Docker connection error %+v, retrying in %s", err, time)
|
log.Errorf("Docker connection error %+v, retrying in %s", err, time)
|
||||||
}
|
}
|
||||||
err := backoff.RetryNotify(operation, backoff.NewExponentialBackOff(), notify)
|
err := backoff.RetryNotify(operation, backoff.NewExponentialBackOff(), notify)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -123,16 +124,16 @@ func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *C
|
||||||
// filter containers
|
// filter containers
|
||||||
filteredContainers := fun.Filter(func(container docker.Container) bool {
|
filteredContainers := fun.Filter(func(container docker.Container) bool {
|
||||||
if len(container.NetworkSettings.Ports) == 0 {
|
if len(container.NetworkSettings.Ports) == 0 {
|
||||||
log.Debug("Filtering container without port %s", container.Name)
|
log.Debugf("Filtering container without port %s", container.Name)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
_, err := strconv.Atoi(container.Config.Labels["traefik.port"])
|
_, err := strconv.Atoi(container.Config.Labels["traefik.port"])
|
||||||
if len(container.NetworkSettings.Ports) > 1 && err != nil {
|
if len(container.NetworkSettings.Ports) > 1 && err != nil {
|
||||||
log.Debug("Filtering container with more than 1 port and no traefik.port label %s", container.Name)
|
log.Debugf("Filtering container with more than 1 port and no traefik.port label %s", container.Name)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if container.Config.Labels["traefik.enable"] == "false" {
|
if container.Config.Labels["traefik.enable"] == "false" {
|
||||||
log.Debug("Filtering disabled container %s", container.Name)
|
log.Debugf("Filtering disabled container %s", container.Name)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -69,18 +69,12 @@ A circuit breaker can also be applied to a backend, preventing high loads on fai
|
||||||
# graceTimeOut = 10
|
# graceTimeOut = 10
|
||||||
|
|
||||||
# Traefik logs file
|
# Traefik logs file
|
||||||
|
# If not defined, logs to stdout
|
||||||
#
|
#
|
||||||
# Optional
|
# Optional
|
||||||
#
|
#
|
||||||
# traefikLogsFile = "log/traefik.log"
|
# traefikLogsFile = "log/traefik.log"
|
||||||
|
|
||||||
# Traefik log to standard output
|
|
||||||
#
|
|
||||||
# Optional
|
|
||||||
# Default: true
|
|
||||||
#
|
|
||||||
# traefikLogsStdout = true
|
|
||||||
|
|
||||||
# Access logs file
|
# Access logs file
|
||||||
#
|
#
|
||||||
# Optional
|
# Optional
|
||||||
|
@ -114,7 +108,6 @@ Like any other reverse proxy, Træfɪk can be configured with a file. You have t
|
||||||
port = ":80"
|
port = ":80"
|
||||||
graceTimeOut = 10
|
graceTimeOut = 10
|
||||||
logLevel = "DEBUG"
|
logLevel = "DEBUG"
|
||||||
traefikLogsStdout = true
|
|
||||||
|
|
||||||
[file]
|
[file]
|
||||||
|
|
||||||
|
@ -153,7 +146,6 @@ traefikLogsStdout = true
|
||||||
port = ":80"
|
port = ":80"
|
||||||
graceTimeOut = 10
|
graceTimeOut = 10
|
||||||
logLevel = "DEBUG"
|
logLevel = "DEBUG"
|
||||||
traefikLogsStdout = true
|
|
||||||
|
|
||||||
[file]
|
[file]
|
||||||
filename = "rules.toml"
|
filename = "rules.toml"
|
||||||
|
|
1
file.go
1
file.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
"gopkg.in/fsnotify.v1"
|
"gopkg.in/fsnotify.v1"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/BurntSushi/ty/fun"
|
"github.com/BurntSushi/ty/fun"
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/gambol99/go-marathon"
|
"github.com/gambol99/go-marathon"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -62,14 +63,14 @@ func (provider *MarathonProvider) Provide(configurationChan chan<- *Configuratio
|
||||||
config.URL = provider.Endpoint
|
config.URL = provider.Endpoint
|
||||||
config.EventsInterface = provider.NetworkInterface
|
config.EventsInterface = provider.NetworkInterface
|
||||||
if client, err := marathon.NewClient(config); err != nil {
|
if client, err := marathon.NewClient(config); err != nil {
|
||||||
log.Error("Failed to create a client for marathon, error: %s", err)
|
log.Errorf("Failed to create a client for marathon, error: %s", err)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
provider.marathonClient = client
|
provider.marathonClient = client
|
||||||
update := make(marathon.EventsChannel, 5)
|
update := make(marathon.EventsChannel, 5)
|
||||||
if provider.Watch {
|
if provider.Watch {
|
||||||
if err := client.AddEventsListener(update, marathon.EVENTS_APPLICATIONS); err != nil {
|
if err := client.AddEventsListener(update, marathon.EVENTS_APPLICATIONS); err != nil {
|
||||||
log.Error("Failed to register for subscriptions, %s", err)
|
log.Errorf("Failed to register for subscriptions, %s", err)
|
||||||
} else {
|
} else {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -94,13 +95,13 @@ func (provider *MarathonProvider) loadMarathonConfig() *Configuration {
|
||||||
|
|
||||||
applications, err := provider.marathonClient.Applications(nil)
|
applications, err := provider.marathonClient.Applications(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to create a client for marathon, error: %s", err)
|
log.Errorf("Failed to create a client for marathon, error: %s", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks, err := provider.marathonClient.AllTasks()
|
tasks, err := provider.marathonClient.AllTasks()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to create a client for marathon, error: %s", err)
|
log.Errorf("Failed to create a client for marathon, error: %s", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
Copyright
|
Copyright
|
||||||
*/
|
*/
|
||||||
package middlewares
|
package middlewares
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"github.com/mailgun/oxy/cbreaker"
|
"github.com/mailgun/oxy/cbreaker"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CircuitBreaker struct {
|
type CircuitBreaker struct {
|
||||||
|
|
69
traefik.go
69
traefik.go
|
@ -2,34 +2,34 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/emilevauge/traefik/middlewares"
|
"github.com/emilevauge/traefik/middlewares"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/mailgun/oxy/cbreaker"
|
||||||
"github.com/mailgun/oxy/forward"
|
"github.com/mailgun/oxy/forward"
|
||||||
"github.com/mailgun/oxy/roundrobin"
|
"github.com/mailgun/oxy/roundrobin"
|
||||||
"github.com/op/go-logging"
|
|
||||||
"github.com/thoas/stats"
|
"github.com/thoas/stats"
|
||||||
"github.com/tylerb/graceful"
|
"github.com/tylerb/graceful"
|
||||||
"github.com/unrolled/render"
|
"github.com/unrolled/render"
|
||||||
"gopkg.in/alecthomas/kingpin.v2"
|
"gopkg.in/alecthomas/kingpin.v2"
|
||||||
|
fmtlog "log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
fmtlog "log"
|
|
||||||
"github.com/mailgun/oxy/cbreaker"
|
|
||||||
"net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
globalConfigFile = kingpin.Arg("conf", "Main configration file.").Default("traefik.toml").String()
|
globalConfigFile = kingpin.Arg("conf", "Main configration file.").Default("traefik.toml").String()
|
||||||
currentConfiguration = new(Configuration)
|
currentConfiguration = new(Configuration)
|
||||||
metrics = stats.New()
|
metrics = stats.New()
|
||||||
log = logging.MustGetLogger("traefik")
|
oxyLogger = &OxyLogger{}
|
||||||
oxyLogger = &OxyLogger{}
|
|
||||||
templatesRenderer = render.New(render.Options{
|
templatesRenderer = render.New(render.Options{
|
||||||
Directory: "templates",
|
Directory: "templates",
|
||||||
Asset: Asset,
|
Asset: Asset,
|
||||||
|
@ -45,7 +45,6 @@ func main() {
|
||||||
var configurationChan = make(chan *Configuration, 10)
|
var configurationChan = make(chan *Configuration, 10)
|
||||||
defer close(configurationChan)
|
defer close(configurationChan)
|
||||||
var providers = []Provider{}
|
var providers = []Provider{}
|
||||||
var format = logging.MustStringFormatter("%{color}%{time:15:04:05.000} %{shortfile:20.20s} %{level:8.8s} %{id:03x} ▶%{color:reset} %{message}")
|
|
||||||
var sigs = make(chan os.Signal, 1)
|
var sigs = make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
@ -56,11 +55,11 @@ func main() {
|
||||||
defer loggerMiddleware.Close()
|
defer loggerMiddleware.Close()
|
||||||
|
|
||||||
// logging
|
// logging
|
||||||
backends := []logging.Backend{}
|
level, err := log.ParseLevel(strings.ToLower(gloablConfiguration.LogLevel))
|
||||||
level, err := logging.LogLevel(gloablConfiguration.LogLevel)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error getting level", err)
|
log.Fatal("Error getting level", err)
|
||||||
}
|
}
|
||||||
|
log.SetLevel(level)
|
||||||
|
|
||||||
if len(gloablConfiguration.TraefikLogsFile) > 0 {
|
if len(gloablConfiguration.TraefikLogsFile) > 0 {
|
||||||
fi, err := os.OpenFile(gloablConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
fi, err := os.OpenFile(gloablConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
|
@ -68,18 +67,12 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error opening file", err)
|
log.Fatal("Error opening file", err)
|
||||||
} else {
|
} else {
|
||||||
logBackend := logging.NewLogBackend(fi, "", 0)
|
log.SetOutput(fi)
|
||||||
logBackendFormatter := logging.NewBackendFormatter(logBackend, logging.GlogFormatter)
|
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true})
|
||||||
backends = append(backends, logBackendFormatter)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true})
|
||||||
}
|
}
|
||||||
if gloablConfiguration.TraefikLogsStdout {
|
|
||||||
logBackend := logging.NewLogBackend(os.Stdout, "", 0)
|
|
||||||
logBackendFormatter := logging.NewBackendFormatter(logBackend, format)
|
|
||||||
backends = append(backends, logBackendFormatter)
|
|
||||||
}
|
|
||||||
logging.SetBackend(backends...)
|
|
||||||
logging.SetLevel(level, "traefik")
|
|
||||||
|
|
||||||
configurationRouter = LoadDefaultConfig(gloablConfiguration)
|
configurationRouter = LoadDefaultConfig(gloablConfiguration)
|
||||||
|
|
||||||
|
@ -87,19 +80,19 @@ func main() {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
configuration := <-configurationChan
|
configuration := <-configurationChan
|
||||||
log.Info("Configuration receveived %+v", configuration)
|
log.Infof("Configuration receveived %+v", configuration)
|
||||||
if configuration == nil {
|
if configuration == nil {
|
||||||
log.Info("Skipping empty configuration")
|
log.Info("Skipping empty configuration")
|
||||||
} else if reflect.DeepEqual(currentConfiguration, configuration) {
|
} else if reflect.DeepEqual(currentConfiguration, configuration) {
|
||||||
log.Info("Skipping same configuration")
|
log.Info("Skipping same configuration")
|
||||||
} else {
|
} else {
|
||||||
newConfigurationRouter, err := LoadConfig(configuration, gloablConfiguration)
|
newConfigurationRouter, err := LoadConfig(configuration, gloablConfiguration)
|
||||||
if (err == nil ){
|
if err == nil {
|
||||||
currentConfiguration = configuration
|
currentConfiguration = configuration
|
||||||
configurationRouter = newConfigurationRouter
|
configurationRouter = newConfigurationRouter
|
||||||
srv.Stop(time.Duration(gloablConfiguration.GraceTimeOut) * time.Second)
|
srv.Stop(time.Duration(gloablConfiguration.GraceTimeOut) * time.Second)
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
}else{
|
} else {
|
||||||
log.Error("Error loading new configuration, aborted ", err)
|
log.Error("Error loading new configuration, aborted ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,7 +122,7 @@ func main() {
|
||||||
|
|
||||||
// start providers
|
// start providers
|
||||||
for _, provider := range providers {
|
for _, provider := range providers {
|
||||||
log.Notice("Starting provider %v %+v", reflect.TypeOf(provider), provider)
|
log.Infof("Starting provider %v %+v", reflect.TypeOf(provider), provider)
|
||||||
currentProvider := provider
|
currentProvider := provider
|
||||||
go func() {
|
go func() {
|
||||||
currentProvider.Provide(configurationChan)
|
currentProvider.Provide(configurationChan)
|
||||||
|
@ -139,7 +132,7 @@ func main() {
|
||||||
goAway := false
|
goAway := false
|
||||||
go func() {
|
go func() {
|
||||||
sig := <-sigs
|
sig := <-sigs
|
||||||
log.Notice("I have to go... %+v", sig)
|
log.Infof("I have to go... %+v", sig)
|
||||||
goAway = true
|
goAway = true
|
||||||
srv.Stop(time.Duration(gloablConfiguration.GraceTimeOut) * time.Second)
|
srv.Stop(time.Duration(gloablConfiguration.GraceTimeOut) * time.Second)
|
||||||
}()
|
}()
|
||||||
|
@ -170,7 +163,7 @@ func main() {
|
||||||
go func() {
|
go func() {
|
||||||
if len(gloablConfiguration.CertFile) > 0 && len(gloablConfiguration.KeyFile) > 0 {
|
if len(gloablConfiguration.CertFile) > 0 && len(gloablConfiguration.KeyFile) > 0 {
|
||||||
err := srv.ListenAndServeTLS(gloablConfiguration.CertFile, gloablConfiguration.KeyFile)
|
err := srv.ListenAndServeTLS(gloablConfiguration.CertFile, gloablConfiguration.KeyFile)
|
||||||
if (err!= nil ){
|
if err != nil {
|
||||||
netOpError, ok := err.(*net.OpError)
|
netOpError, ok := err.(*net.OpError)
|
||||||
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
||||||
log.Fatal("Error creating server: ", err)
|
log.Fatal("Error creating server: ", err)
|
||||||
|
@ -178,7 +171,7 @@ func main() {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := srv.ListenAndServe()
|
err := srv.ListenAndServe()
|
||||||
if (err!= nil ){
|
if err != nil {
|
||||||
netOpError, ok := err.(*net.OpError)
|
netOpError, ok := err.(*net.OpError)
|
||||||
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
if ok && netOpError.Err.Error() != "use of closed network connection" {
|
||||||
log.Fatal("Error creating server: ", err)
|
log.Fatal("Error creating server: ", err)
|
||||||
|
@ -186,9 +179,9 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
log.Notice("Started")
|
log.Info("Started")
|
||||||
<-srv.StopChan()
|
<-srv.StopChan()
|
||||||
log.Notice("Stopped")
|
log.Info("Stopped")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,31 +190,31 @@ func LoadConfig(configuration *Configuration, gloablConfiguration *GlobalConfigu
|
||||||
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
||||||
backends := map[string]http.Handler{}
|
backends := map[string]http.Handler{}
|
||||||
for frontendName, frontend := range configuration.Frontends {
|
for frontendName, frontend := range configuration.Frontends {
|
||||||
log.Debug("Creating frontend %s", frontendName)
|
log.Debugf("Creating frontend %s", frontendName)
|
||||||
fwd, _ := forward.New(forward.Logger(oxyLogger))
|
fwd, _ := forward.New(forward.Logger(oxyLogger))
|
||||||
newRoute := router.NewRoute().Name(frontendName)
|
newRoute := router.NewRoute().Name(frontendName)
|
||||||
for routeName, route := range frontend.Routes {
|
for routeName, route := range frontend.Routes {
|
||||||
log.Debug("Creating route %s %s:%s", routeName, route.Rule, route.Value)
|
log.Debugf("Creating route %s %s:%s", routeName, route.Rule, route.Value)
|
||||||
newRouteReflect := Invoke(newRoute, route.Rule, route.Value)
|
newRouteReflect := Invoke(newRoute, route.Rule, route.Value)
|
||||||
newRoute = newRouteReflect[0].Interface().(*mux.Route)
|
newRoute = newRouteReflect[0].Interface().(*mux.Route)
|
||||||
}
|
}
|
||||||
if backends[frontend.Backend] == nil {
|
if backends[frontend.Backend] == nil {
|
||||||
log.Debug("Creating backend %s", frontend.Backend)
|
log.Debugf("Creating backend %s", frontend.Backend)
|
||||||
lb, _ := roundrobin.New(fwd)
|
lb, _ := roundrobin.New(fwd)
|
||||||
rb, _ := roundrobin.NewRebalancer(lb, roundrobin.RebalancerLogger(oxyLogger))
|
rb, _ := roundrobin.NewRebalancer(lb, roundrobin.RebalancerLogger(oxyLogger))
|
||||||
for serverName, server := range configuration.Backends[frontend.Backend].Servers {
|
for serverName, server := range configuration.Backends[frontend.Backend].Servers {
|
||||||
if url, err := url.Parse(server.Url); err != nil{
|
if url, err := url.Parse(server.Url); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}else{
|
} else {
|
||||||
log.Debug("Creating server %s %s", serverName, url.String())
|
log.Debugf("Creating server %s %s", serverName, url.String())
|
||||||
rb.UpsertServer(url, roundrobin.Weight(server.Weight))
|
rb.UpsertServer(url, roundrobin.Weight(server.Weight))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
backends[frontend.Backend] = rb
|
backends[frontend.Backend] = rb
|
||||||
} else {
|
} else {
|
||||||
log.Debug("Reusing backend %s", frontend.Backend)
|
log.Debugf("Reusing backend %s", frontend.Backend)
|
||||||
}
|
}
|
||||||
// stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(gloablConfiguration.Replay)), stream.Logger(oxyLogger))
|
// stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(gloablConfiguration.Replay)), stream.Logger(oxyLogger))
|
||||||
var negroni = negroni.New()
|
var negroni = negroni.New()
|
||||||
negroni.Use(middlewares.NewCircuitBreaker(backends[frontend.Backend], cbreaker.Logger(oxyLogger)))
|
negroni.Use(middlewares.NewCircuitBreaker(backends[frontend.Backend], cbreaker.Logger(oxyLogger)))
|
||||||
newRoute.Handler(negroni)
|
newRoute.Handler(negroni)
|
||||||
|
@ -246,6 +239,6 @@ func LoadFileConfig(file string) *GlobalConfiguration {
|
||||||
if _, err := toml.DecodeFile(file, configuration); err != nil {
|
if _, err := toml.DecodeFile(file, configuration); err != nil {
|
||||||
log.Fatal("Error reading file ", err)
|
log.Fatal("Error reading file ", err)
|
||||||
}
|
}
|
||||||
log.Debug("Global configuration loaded %+v", configuration)
|
log.Debugf("Global configuration loaded %+v", configuration)
|
||||||
return configuration
|
return configuration
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,18 +18,12 @@
|
||||||
# graceTimeOut = 10
|
# graceTimeOut = 10
|
||||||
|
|
||||||
# Traefik logs file
|
# Traefik logs file
|
||||||
|
# If not defined, logs to stdout
|
||||||
#
|
#
|
||||||
# Optional
|
# Optional
|
||||||
#
|
#
|
||||||
# traefikLogsFile = "log/traefik.log"
|
# traefikLogsFile = "log/traefik.log"
|
||||||
|
|
||||||
# Traefik log to standard output
|
|
||||||
#
|
|
||||||
# Optional
|
|
||||||
# Default: true
|
|
||||||
#
|
|
||||||
# traefikLogsStdout = true
|
|
||||||
|
|
||||||
# Access logs file
|
# Access logs file
|
||||||
#
|
#
|
||||||
# Optional
|
# Optional
|
||||||
|
|
21
web.go
21
web.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/elazarl/go-bindata-assetfs"
|
"github.com/elazarl/go-bindata-assetfs"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -10,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type WebProvider struct {
|
type WebProvider struct {
|
||||||
Address string
|
Address string
|
||||||
CertFile, KeyFile string
|
CertFile, KeyFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ func (provider *WebProvider) Provide(configurationChan chan<- *Configuration) {
|
||||||
configurationChan <- configuration
|
configurationChan <- configuration
|
||||||
GetConfigHandler(rw, r)
|
GetConfigHandler(rw, r)
|
||||||
} else {
|
} else {
|
||||||
log.Error("Error parsing configuration %+v\n", err)
|
log.Errorf("Error parsing configuration %+v", err)
|
||||||
http.Error(rw, fmt.Sprintf("%+v", err), http.StatusBadRequest)
|
http.Error(rw, fmt.Sprintf("%+v", err), http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
@ -46,13 +47,13 @@ func (provider *WebProvider) Provide(configurationChan chan<- *Configuration) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if len(provider.CertFile) > 0 && len(provider.KeyFile) > 0 {
|
if len(provider.CertFile) > 0 && len(provider.KeyFile) > 0 {
|
||||||
err := http.ListenAndServeTLS(provider.Address,provider.CertFile, provider.KeyFile, systemRouter)
|
err := http.ListenAndServeTLS(provider.Address, provider.CertFile, provider.KeyFile, systemRouter)
|
||||||
if (err!= nil ){
|
if err != nil {
|
||||||
log.Fatal("Error creating server: ", err)
|
log.Fatal("Error creating server: ", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := http.ListenAndServe(provider.Address, systemRouter)
|
err := http.ListenAndServe(provider.Address, systemRouter)
|
||||||
if (err!= nil ){
|
if err != nil {
|
||||||
log.Fatal("Error creating server: ", err)
|
log.Fatal("Error creating server: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,7 +81,7 @@ func GetBackendHandler(rw http.ResponseWriter, r *http.Request) {
|
||||||
id := vars["backend"]
|
id := vars["backend"]
|
||||||
if backend, ok := currentConfiguration.Backends[id]; ok {
|
if backend, ok := currentConfiguration.Backends[id]; ok {
|
||||||
templatesRenderer.JSON(rw, http.StatusOK, backend)
|
templatesRenderer.JSON(rw, http.StatusOK, backend)
|
||||||
}else{
|
} else {
|
||||||
http.NotFound(rw, r)
|
http.NotFound(rw, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +95,7 @@ func GetFrontendHandler(rw http.ResponseWriter, r *http.Request) {
|
||||||
id := vars["frontend"]
|
id := vars["frontend"]
|
||||||
if frontend, ok := currentConfiguration.Frontends[id]; ok {
|
if frontend, ok := currentConfiguration.Frontends[id]; ok {
|
||||||
templatesRenderer.JSON(rw, http.StatusOK, frontend)
|
templatesRenderer.JSON(rw, http.StatusOK, frontend)
|
||||||
}else{
|
} else {
|
||||||
http.NotFound(rw, r)
|
http.NotFound(rw, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,7 +105,7 @@ func GetServersHandler(rw http.ResponseWriter, r *http.Request) {
|
||||||
backend := vars["backend"]
|
backend := vars["backend"]
|
||||||
if backend, ok := currentConfiguration.Backends[backend]; ok {
|
if backend, ok := currentConfiguration.Backends[backend]; ok {
|
||||||
templatesRenderer.JSON(rw, http.StatusOK, backend.Servers)
|
templatesRenderer.JSON(rw, http.StatusOK, backend.Servers)
|
||||||
}else{
|
} else {
|
||||||
http.NotFound(rw, r)
|
http.NotFound(rw, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,10 +117,10 @@ func GetServerHandler(rw http.ResponseWriter, r *http.Request) {
|
||||||
if backend, ok := currentConfiguration.Backends[backend]; ok {
|
if backend, ok := currentConfiguration.Backends[backend]; ok {
|
||||||
if server, ok := backend.Servers[server]; ok {
|
if server, ok := backend.Servers[server]; ok {
|
||||||
templatesRenderer.JSON(rw, http.StatusOK, server)
|
templatesRenderer.JSON(rw, http.StatusOK, server)
|
||||||
}else{
|
} else {
|
||||||
http.NotFound(rw, r)
|
http.NotFound(rw, r)
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
http.NotFound(rw, r)
|
http.NotFound(rw, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue