From 7fcb7b86d320f6f06497a3b38a4a4862bb16550e Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 17 Apr 2017 22:47:53 +0200 Subject: [PATCH] Extract some code in packages - This will help split stuff in smaller, better tested packages - This moves some stuff like the traefik command to package `cmd` Signed-off-by: Vincent Demeester --- .github/CONTRIBUTING.md | 3 ++- cmd/{ => traefik}/bug.go | 6 ++--- traefik.go => cmd/traefik/traefik.go | 28 ++++++++++----------- cmd/{ => traefik}/version.go | 6 ++--- script/binary | 6 ++++- script/crossbinary | 4 +-- adapters.go => server/adapters.go | 5 +--- configuration.go => server/configuration.go | 2 +- rules.go => server/rules.go | 9 ++++--- rules_test.go => server/rules_test.go | 5 ++-- server.go => server/server.go | 5 +--- web.go => server/web.go | 2 +- 12 files changed, 41 insertions(+), 40 deletions(-) rename cmd/{ => traefik}/bug.go (95%) rename traefik.go => cmd/traefik/traefik.go (91%) rename cmd/{ => traefik}/version.go (92%) rename adapters.go => server/adapters.go (96%) rename configuration.go => server/configuration.go (99%) rename rules.go => server/rules.go (99%) rename rules_test.go => server/rules_test.go (99%) rename server.go => server/server.go (99%) rename web.go => server/web.go (99%) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index fac033067..8a6a788b2 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -67,7 +67,8 @@ $ go generate $ go build # Using gox to build multiple platform $ gox "linux darwin" "386 amd64 arm" \ - -output="dist/traefik_{{.OS}}-{{.Arch}}" + -output="dist/traefik_{{.OS}}-{{.Arch}}" \ + ./cmd/traefik # run other commands like tests ``` diff --git a/cmd/bug.go b/cmd/traefik/bug.go similarity index 95% rename from cmd/bug.go rename to cmd/traefik/bug.go index a081d9595..b4bce78c0 100644 --- a/cmd/bug.go +++ b/cmd/traefik/bug.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "bytes" @@ -39,8 +39,8 @@ var ( ` ) -// NewBugCmd builds a new Bug command -func NewBugCmd(traefikConfiguration interface{}, traefikPointersConfiguration interface{}) *flaeg.Command { +// newBugCmd builds a new Bug command +func newBugCmd(traefikConfiguration interface{}, traefikPointersConfiguration interface{}) *flaeg.Command { //version Command init return &flaeg.Command{ diff --git a/traefik.go b/cmd/traefik/traefik.go similarity index 91% rename from traefik.go rename to cmd/traefik/traefik.go index 909e204e2..3c2b5c53e 100644 --- a/traefik.go +++ b/cmd/traefik/traefik.go @@ -17,11 +17,11 @@ import ( "github.com/containous/staert" "github.com/containous/traefik/acme" "github.com/containous/traefik/cluster" - "github.com/containous/traefik/cmd" "github.com/containous/traefik/log" "github.com/containous/traefik/middlewares" "github.com/containous/traefik/provider/kubernetes" "github.com/containous/traefik/safe" + "github.com/containous/traefik/server" "github.com/containous/traefik/types" "github.com/containous/traefik/version" "github.com/coreos/go-systemd/daemon" @@ -33,8 +33,8 @@ func main() { runtime.GOMAXPROCS(runtime.NumCPU()) //traefik config inits - traefikConfiguration := NewTraefikConfiguration() - traefikPointersConfiguration := NewTraefikDefaultPointersConfiguration() + traefikConfiguration := server.NewTraefikConfiguration() + traefikPointersConfiguration := server.NewTraefikDefaultPointersConfiguration() //traefik Command init traefikCmd := &flaeg.Command{ Name: "traefik", @@ -101,16 +101,16 @@ Complete documentation is available at https://traefik.io`, //init flaeg source f := flaeg.New(traefikCmd, os.Args[1:]) //add custom parsers - f.AddParser(reflect.TypeOf(EntryPoints{}), &EntryPoints{}) - f.AddParser(reflect.TypeOf(DefaultEntryPoints{}), &DefaultEntryPoints{}) + f.AddParser(reflect.TypeOf(server.EntryPoints{}), &server.EntryPoints{}) + f.AddParser(reflect.TypeOf(server.DefaultEntryPoints{}), &server.DefaultEntryPoints{}) f.AddParser(reflect.TypeOf(types.Constraints{}), &types.Constraints{}) f.AddParser(reflect.TypeOf(kubernetes.Namespaces{}), &kubernetes.Namespaces{}) f.AddParser(reflect.TypeOf([]acme.Domain{}), &acme.Domains{}) f.AddParser(reflect.TypeOf(types.Buckets{}), &types.Buckets{}) //add commands - f.AddCommand(cmd.NewVersionCmd()) - f.AddCommand(cmd.NewBugCmd(traefikConfiguration, traefikPointersConfiguration)) + f.AddCommand(newVersionCmd()) + f.AddCommand(newBugCmd(traefikConfiguration, traefikPointersConfiguration)) f.AddCommand(storeconfigCmd) usedCmd, err := f.GetCommand() @@ -168,7 +168,7 @@ Complete documentation is available at https://traefik.io`, os.Exit(0) } -func run(traefikConfiguration *TraefikConfiguration) { +func run(traefikConfiguration *server.TraefikConfiguration) { fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags) // load global configuration @@ -191,7 +191,7 @@ func run(traefikConfiguration *TraefikConfiguration) { } if len(globalConfiguration.EntryPoints) == 0 { - globalConfiguration.EntryPoints = map[string]*EntryPoint{"http": {Address: ":80"}} + globalConfiguration.EntryPoints = map[string]*server.EntryPoint{"http": {Address: ":80"}} globalConfiguration.DefaultEntryPoints = []string{"http"} } @@ -241,9 +241,9 @@ func run(traefikConfiguration *TraefikConfiguration) { log.Infof("Using TOML configuration file %s", traefikConfiguration.ConfigFile) } log.Debugf("Global configuration loaded %s", string(jsonConf)) - server := NewServer(globalConfiguration) - server.Start() - defer server.Close() + svr := server.NewServer(globalConfiguration) + svr.Start() + defer svr.Close() sent, err := daemon.SdNotify(true, "READY=1") if !sent && err != nil { log.Error("Fail to notify", err) @@ -261,13 +261,13 @@ func run(traefikConfiguration *TraefikConfiguration) { } }(t) } - server.Wait() + svr.Wait() log.Info("Shutting down") } // CreateKvSource creates KvSource // TLS support is enable for Consul and Etcd backends -func CreateKvSource(traefikConfiguration *TraefikConfiguration) (*staert.KvSource, error) { +func CreateKvSource(traefikConfiguration *server.TraefikConfiguration) (*staert.KvSource, error) { var kv *staert.KvSource var store store.Store var err error diff --git a/cmd/version.go b/cmd/traefik/version.go similarity index 92% rename from cmd/version.go rename to cmd/traefik/version.go index 09406d1c4..2acb4f940 100644 --- a/cmd/version.go +++ b/cmd/traefik/version.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "fmt" @@ -17,8 +17,8 @@ Go version: {{.GoVersion}} Built: {{.BuildTime}} OS/Arch: {{.Os}}/{{.Arch}}` -// NewVersionCmd builds a new Version command -func NewVersionCmd() *flaeg.Command { +// newVersionCmd builds a new Version command +func newVersionCmd() *flaeg.Command { //version Command init return &flaeg.Command{ diff --git a/script/binary b/script/binary index 24d7baf2d..8ba7cbb71 100755 --- a/script/binary +++ b/script/binary @@ -26,4 +26,8 @@ if [ -z "$DATE" ]; then fi # Build binaries -CGO_ENABLED=0 GOGC=off go build $FLAGS -ldflags "-s -w -X github.com/containous/traefik/version.Version=$VERSION -X github.com/containous/traefik/version.Codename=$CODENAME -X github.com/containous/traefik/version.BuildDate=$DATE" -a -installsuffix nocgo -o dist/traefik . +CGO_ENABLED=0 GOGC=off go build $FLAGS -ldflags "-s -w \ + -X github.com/containous/traefik/version.Version=$VERSION \ + -X github.com/containous/traefik/version.Codename=$CODENAME \ + -X github.com/containous/traefik/version.BuildDate=$DATE" \ + -a -installsuffix nocgo -o dist/traefik ./cmd/traefik diff --git a/script/crossbinary b/script/crossbinary index cf8866529..ade28e8b5 100755 --- a/script/crossbinary +++ b/script/crossbinary @@ -27,7 +27,7 @@ OS_ARCH_ARG=(386 amd64) for OS in ${OS_PLATFORM_ARG[@]}; do for ARCH in ${OS_ARCH_ARG[@]}; do echo "Building binary for $OS/$ARCH..." - GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/containous/traefik/version.Version=$VERSION -X github.com/containous/traefik/version.Codename=$CODENAME -X github.com/containous/traefik/version.BuildDate=$DATE" -o "dist/traefik_$OS-$ARCH" . + GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/containous/traefik/version.Version=$VERSION -X github.com/containous/traefik/version.Codename=$CODENAME -X github.com/containous/traefik/version.BuildDate=$DATE" -o "dist/traefik_$OS-$ARCH" ./cmd/traefik/ done done @@ -38,6 +38,6 @@ OS_ARCH_ARG=(arm arm64) for OS in ${OS_PLATFORM_ARG[@]}; do for ARCH in ${OS_ARCH_ARG[@]}; do echo "Building binary for $OS/$ARCH..." - GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/containous/traefik/version.Version=$VERSION -X github.com/containous/traefik/version.Codename=$CODENAME -X github.com/containous/traefik/version.BuildDate=$DATE" -o "dist/traefik_$OS-$ARCH" . + GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/containous/traefik/version.Version=$VERSION -X github.com/containous/traefik/version.Codename=$CODENAME -X github.com/containous/traefik/version.BuildDate=$DATE" -o "dist/traefik_$OS-$ARCH" ./cmd/traefik/ done done diff --git a/adapters.go b/server/adapters.go similarity index 96% rename from adapters.go rename to server/adapters.go index edf745e85..bf0ee18eb 100644 --- a/adapters.go +++ b/server/adapters.go @@ -1,7 +1,4 @@ -/* -Copyright -*/ -package main +package server import ( "net/http" diff --git a/configuration.go b/server/configuration.go similarity index 99% rename from configuration.go rename to server/configuration.go index f48a5b46a..85a2b82d2 100644 --- a/configuration.go +++ b/server/configuration.go @@ -1,4 +1,4 @@ -package main +package server import ( "crypto/tls" diff --git a/rules.go b/server/rules.go similarity index 99% rename from rules.go rename to server/rules.go index 811ff3292..3ac0fa927 100644 --- a/rules.go +++ b/server/rules.go @@ -1,16 +1,17 @@ -package main +package server import ( "errors" "fmt" - "github.com/BurntSushi/ty/fun" - "github.com/containous/mux" - "github.com/containous/traefik/types" "net" "net/http" "reflect" "sort" "strings" + + "github.com/BurntSushi/ty/fun" + "github.com/containous/mux" + "github.com/containous/traefik/types" ) // Rules holds rule parsing and configuration diff --git a/rules_test.go b/server/rules_test.go similarity index 99% rename from rules_test.go rename to server/rules_test.go index 72c712423..a9995b154 100644 --- a/rules_test.go +++ b/server/rules_test.go @@ -1,11 +1,12 @@ -package main +package server import ( - "github.com/containous/mux" "net/http" "net/url" "reflect" "testing" + + "github.com/containous/mux" ) func TestParseOneRule(t *testing.T) { diff --git a/server.go b/server/server.go similarity index 99% rename from server.go rename to server/server.go index 3b855a9cc..81e9fd3dd 100644 --- a/server.go +++ b/server/server.go @@ -1,7 +1,4 @@ -/* -Copyright -*/ -package main +package server import ( "context" diff --git a/web.go b/server/web.go similarity index 99% rename from web.go rename to server/web.go index e71e75f20..ac1d38fe0 100644 --- a/web.go +++ b/server/web.go @@ -1,4 +1,4 @@ -package main +package server import ( "encoding/json"