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 <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2017-04-17 22:47:53 +02:00
parent 9c9015a7b1
commit 7fcb7b86d3
12 changed files with 41 additions and 40 deletions

View file

@ -67,7 +67,8 @@ $ go generate
$ go build $ go build
# Using gox to build multiple platform # Using gox to build multiple platform
$ gox "linux darwin" "386 amd64 arm" \ $ gox "linux darwin" "386 amd64 arm" \
-output="dist/traefik_{{.OS}}-{{.Arch}}" -output="dist/traefik_{{.OS}}-{{.Arch}}" \
./cmd/traefik
# run other commands like tests # run other commands like tests
``` ```

View file

@ -1,4 +1,4 @@
package cmd package main
import ( import (
"bytes" "bytes"
@ -39,8 +39,8 @@ var (
` `
) )
// NewBugCmd builds a new Bug command // newBugCmd builds a new Bug command
func NewBugCmd(traefikConfiguration interface{}, traefikPointersConfiguration interface{}) *flaeg.Command { func newBugCmd(traefikConfiguration interface{}, traefikPointersConfiguration interface{}) *flaeg.Command {
//version Command init //version Command init
return &flaeg.Command{ return &flaeg.Command{

View file

@ -17,11 +17,11 @@ import (
"github.com/containous/staert" "github.com/containous/staert"
"github.com/containous/traefik/acme" "github.com/containous/traefik/acme"
"github.com/containous/traefik/cluster" "github.com/containous/traefik/cluster"
"github.com/containous/traefik/cmd"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/containous/traefik/middlewares" "github.com/containous/traefik/middlewares"
"github.com/containous/traefik/provider/kubernetes" "github.com/containous/traefik/provider/kubernetes"
"github.com/containous/traefik/safe" "github.com/containous/traefik/safe"
"github.com/containous/traefik/server"
"github.com/containous/traefik/types" "github.com/containous/traefik/types"
"github.com/containous/traefik/version" "github.com/containous/traefik/version"
"github.com/coreos/go-systemd/daemon" "github.com/coreos/go-systemd/daemon"
@ -33,8 +33,8 @@ func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
//traefik config inits //traefik config inits
traefikConfiguration := NewTraefikConfiguration() traefikConfiguration := server.NewTraefikConfiguration()
traefikPointersConfiguration := NewTraefikDefaultPointersConfiguration() traefikPointersConfiguration := server.NewTraefikDefaultPointersConfiguration()
//traefik Command init //traefik Command init
traefikCmd := &flaeg.Command{ traefikCmd := &flaeg.Command{
Name: "traefik", Name: "traefik",
@ -101,16 +101,16 @@ Complete documentation is available at https://traefik.io`,
//init flaeg source //init flaeg source
f := flaeg.New(traefikCmd, os.Args[1:]) f := flaeg.New(traefikCmd, os.Args[1:])
//add custom parsers //add custom parsers
f.AddParser(reflect.TypeOf(EntryPoints{}), &EntryPoints{}) f.AddParser(reflect.TypeOf(server.EntryPoints{}), &server.EntryPoints{})
f.AddParser(reflect.TypeOf(DefaultEntryPoints{}), &DefaultEntryPoints{}) f.AddParser(reflect.TypeOf(server.DefaultEntryPoints{}), &server.DefaultEntryPoints{})
f.AddParser(reflect.TypeOf(types.Constraints{}), &types.Constraints{}) f.AddParser(reflect.TypeOf(types.Constraints{}), &types.Constraints{})
f.AddParser(reflect.TypeOf(kubernetes.Namespaces{}), &kubernetes.Namespaces{}) f.AddParser(reflect.TypeOf(kubernetes.Namespaces{}), &kubernetes.Namespaces{})
f.AddParser(reflect.TypeOf([]acme.Domain{}), &acme.Domains{}) f.AddParser(reflect.TypeOf([]acme.Domain{}), &acme.Domains{})
f.AddParser(reflect.TypeOf(types.Buckets{}), &types.Buckets{}) f.AddParser(reflect.TypeOf(types.Buckets{}), &types.Buckets{})
//add commands //add commands
f.AddCommand(cmd.NewVersionCmd()) f.AddCommand(newVersionCmd())
f.AddCommand(cmd.NewBugCmd(traefikConfiguration, traefikPointersConfiguration)) f.AddCommand(newBugCmd(traefikConfiguration, traefikPointersConfiguration))
f.AddCommand(storeconfigCmd) f.AddCommand(storeconfigCmd)
usedCmd, err := f.GetCommand() usedCmd, err := f.GetCommand()
@ -168,7 +168,7 @@ Complete documentation is available at https://traefik.io`,
os.Exit(0) os.Exit(0)
} }
func run(traefikConfiguration *TraefikConfiguration) { func run(traefikConfiguration *server.TraefikConfiguration) {
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags) fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
// load global configuration // load global configuration
@ -191,7 +191,7 @@ func run(traefikConfiguration *TraefikConfiguration) {
} }
if len(globalConfiguration.EntryPoints) == 0 { 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"} globalConfiguration.DefaultEntryPoints = []string{"http"}
} }
@ -241,9 +241,9 @@ func run(traefikConfiguration *TraefikConfiguration) {
log.Infof("Using TOML configuration file %s", traefikConfiguration.ConfigFile) log.Infof("Using TOML configuration file %s", traefikConfiguration.ConfigFile)
} }
log.Debugf("Global configuration loaded %s", string(jsonConf)) log.Debugf("Global configuration loaded %s", string(jsonConf))
server := NewServer(globalConfiguration) svr := server.NewServer(globalConfiguration)
server.Start() svr.Start()
defer server.Close() defer svr.Close()
sent, err := daemon.SdNotify(true, "READY=1") sent, err := daemon.SdNotify(true, "READY=1")
if !sent && err != nil { if !sent && err != nil {
log.Error("Fail to notify", err) log.Error("Fail to notify", err)
@ -261,13 +261,13 @@ func run(traefikConfiguration *TraefikConfiguration) {
} }
}(t) }(t)
} }
server.Wait() svr.Wait()
log.Info("Shutting down") log.Info("Shutting down")
} }
// CreateKvSource creates KvSource // CreateKvSource creates KvSource
// TLS support is enable for Consul and Etcd backends // 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 kv *staert.KvSource
var store store.Store var store store.Store
var err error var err error

View file

@ -1,4 +1,4 @@
package cmd package main
import ( import (
"fmt" "fmt"
@ -17,8 +17,8 @@ Go version: {{.GoVersion}}
Built: {{.BuildTime}} Built: {{.BuildTime}}
OS/Arch: {{.Os}}/{{.Arch}}` OS/Arch: {{.Os}}/{{.Arch}}`
// NewVersionCmd builds a new Version command // newVersionCmd builds a new Version command
func NewVersionCmd() *flaeg.Command { func newVersionCmd() *flaeg.Command {
//version Command init //version Command init
return &flaeg.Command{ return &flaeg.Command{

View file

@ -26,4 +26,8 @@ if [ -z "$DATE" ]; then
fi fi
# Build binaries # 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

View file

@ -27,7 +27,7 @@ OS_ARCH_ARG=(386 amd64)
for OS in ${OS_PLATFORM_ARG[@]}; do for OS in ${OS_PLATFORM_ARG[@]}; do
for ARCH in ${OS_ARCH_ARG[@]}; do for ARCH in ${OS_ARCH_ARG[@]}; do
echo "Building binary for $OS/$ARCH..." 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
done done
@ -38,6 +38,6 @@ OS_ARCH_ARG=(arm arm64)
for OS in ${OS_PLATFORM_ARG[@]}; do for OS in ${OS_PLATFORM_ARG[@]}; do
for ARCH in ${OS_ARCH_ARG[@]}; do for ARCH in ${OS_ARCH_ARG[@]}; do
echo "Building binary for $OS/$ARCH..." 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
done done

View file

@ -1,7 +1,4 @@
/* package server
Copyright
*/
package main
import ( import (
"net/http" "net/http"

View file

@ -1,4 +1,4 @@
package main package server
import ( import (
"crypto/tls" "crypto/tls"

View file

@ -1,16 +1,17 @@
package main package server
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/BurntSushi/ty/fun"
"github.com/containous/mux"
"github.com/containous/traefik/types"
"net" "net"
"net/http" "net/http"
"reflect" "reflect"
"sort" "sort"
"strings" "strings"
"github.com/BurntSushi/ty/fun"
"github.com/containous/mux"
"github.com/containous/traefik/types"
) )
// Rules holds rule parsing and configuration // Rules holds rule parsing and configuration

View file

@ -1,11 +1,12 @@
package main package server
import ( import (
"github.com/containous/mux"
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
"testing" "testing"
"github.com/containous/mux"
) )
func TestParseOneRule(t *testing.T) { func TestParseOneRule(t *testing.T) {

View file

@ -1,7 +1,4 @@
/* package server
Copyright
*/
package main
import ( import (
"context" "context"

View file

@ -1,4 +1,4 @@
package main package server
import ( import (
"encoding/json" "encoding/json"