1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-22 13:34:03 +03:00

NotFoundHandler

This commit is contained in:
emile 2015-09-10 15:13:35 +02:00
parent e60e955f90
commit 46ef489b03
6 changed files with 61 additions and 25 deletions

View File

@ -1,3 +1,4 @@
* Default configuration values
* Godoc
* Weights
* Licence

View File

@ -1,5 +1,22 @@
package main
type GlobalConfiguration struct {
Port string
GraceTimeOut int64
Docker *DockerProvider
File *FileProvider
Web *WebProvider
Marathon *MarathonProvider
}
func NewGlobalConfiguration() *GlobalConfiguration {
globalConfiguration := new(GlobalConfiguration)
// default values
globalConfiguration.Port = ":8080"
globalConfiguration.GraceTimeOut = 10
return globalConfiguration
}
type Backend struct {
Servers map[string]Server

View File

@ -11,11 +11,12 @@ import (
)
type MarathonProvider struct {
Watch bool
Endpoint string
marathonClient marathon.Marathon
Domain string
Filename string
Watch bool
Endpoint string
marathonClient marathon.Marathon
Domain string
Filename string
NetworkInterface string
}
var MarathonFuncMap = template.FuncMap{
@ -40,7 +41,7 @@ var MarathonFuncMap = template.FuncMap{
func (provider *MarathonProvider) Provide(configurationChan chan <- *Configuration) {
config := marathon.NewDefaultConfig()
config.URL = provider.Endpoint
config.EventsInterface = "docker0"
config.EventsInterface = provider.NetworkInterface
if client, err := marathon.NewClient(config); err != nil {
log.Println("Failed to create a client for marathon, error: %s", err)
return

9
templates/notFound.tmpl Normal file
View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>træfik</title>
</head>
<body>
Ohhhh man, this is bad...
</body>
</html>

View File

@ -5,7 +5,6 @@ import (
"github.com/mailgun/oxy/forward"
"github.com/mailgun/oxy/roundrobin"
"github.com/tylerb/graceful"
"net"
"net/http"
"net/url"
"os"
@ -18,14 +17,6 @@ import (
"github.com/gorilla/handlers"
)
type FileConfiguration struct {
Port string
Docker *DockerProvider
File *FileProvider
Web *WebProvider
Marathon *MarathonProvider
}
var srv *graceful.Server
var configurationRouter *mux.Router
var currentConfiguration = new(Configuration)
@ -33,10 +24,11 @@ var configurationChan = make(chan *Configuration)
var providers = []Provider{}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
globalConfigFile := "træfik.toml"
configurationRouter = LoadDefaultConfig()
go func() {
for {
configuration := <-configurationChan
@ -88,7 +80,7 @@ func main() {
sig := <-sigs
log.Println("I have to go...", sig)
goAway = true
srv.Stop(10 * time.Second)
srv.Stop(time.Duration(configuration.GraceTimeOut) * time.Second)
}()
for {
@ -96,28 +88,37 @@ func main() {
break
}
srv = &graceful.Server{
Timeout: 10 * time.Second,
Timeout: time.Duration(configuration.GraceTimeOut) * time.Second,
NoSignalHandling: true,
ConnState: func(conn net.Conn, state http.ConnState) {
// conn has a new state
},
Server: &http.Server{
Addr: configuration.Port,
Handler: configurationRouter,
},
}
go srv.ListenAndServe()
go func() {
srv.ListenAndServe()
}()
log.Println("Started")
<-srv.StopChan()
log.Println("Stopped")
}
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
renderer.HTML(w, http.StatusNotFound, "notFound", nil)
}
func LoadDefaultConfig() *mux.Router {
router := mux.NewRouter()
router.NotFoundHandler = handlers.CombinedLoggingHandler(os.Stdout, http.HandlerFunc(notFoundHandler))
return router
}
func LoadConfig(configuration *Configuration) *mux.Router {
router := mux.NewRouter()
router.NotFoundHandler = handlers.CombinedLoggingHandler(os.Stdout, http.HandlerFunc(notFoundHandler))
backends := map[string]http.Handler{}
for routeName, route := range configuration.Routes {
log.Println("Creating route", routeName)
@ -144,6 +145,10 @@ func LoadConfig(configuration *Configuration) *mux.Router {
}
for _, muxRoute := range newRoutes {
muxRoute.Handler(handlers.CombinedLoggingHandler(os.Stdout, backends[route.Backend]))
err := muxRoute.GetError()
if err != nil {
log.Println("Error building route", err)
}
}
}
return router
@ -157,10 +162,11 @@ func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
}
func LoadFileConfig(file string) *FileConfiguration {
configuration := new(FileConfiguration)
func LoadFileConfig(file string) *GlobalConfiguration {
configuration := NewGlobalConfiguration()
if _, err := toml.DecodeFile(file, configuration); err != nil {
log.Fatal("Error reading file:", err)
}
log.Printf("Global configuration loaded %+v\n", configuration)
return configuration
}

View File

@ -1,4 +1,5 @@
port = ":8001"
graceTimeOut = 10
#[docker]
#endpoint = "unix:///var/run/docker.sock"
@ -11,6 +12,7 @@ endpoint = "http://127.0.0.1:8080"
watch = true
domain = "toto.fr"
filename = "marathon.tmpl"
networkInterface = "eth0"
[web]
address = ":8010"