-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
185 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
[entryPoints] | ||
[entryPoints.http] | ||
address = ":8000" | ||
|
||
[api] | ||
|
||
[log] | ||
logLevel = "DEBUG" | ||
|
||
[providers] | ||
[providers.rest] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
whoami1: | ||
image: containous/whoami | ||
ports: | ||
- "8881:80" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package integration | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/containous/traefik/config" | ||
"github.com/containous/traefik/integration/try" | ||
"github.com/go-check/check" | ||
checker "github.com/vdemeester/shakers" | ||
) | ||
|
||
type RestSuite struct{ BaseSuite } | ||
|
||
func (s *RestSuite) SetUpSuite(c *check.C) { | ||
s.createComposeProject(c, "rest") | ||
|
||
s.composeProject.Start(c) | ||
} | ||
|
||
func (s *RestSuite) TestSimpleConfiguration(c *check.C) { | ||
cmd, display := s.traefikCmd(withConfigFile("fixtures/rest/simple.toml")) | ||
|
||
defer display(c) | ||
err := cmd.Start() | ||
c.Assert(err, checker.IsNil) | ||
defer cmd.Process.Kill() | ||
|
||
// Expected a 404 as we did not configure anything. | ||
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusNotFound)) | ||
c.Assert(err, checker.IsNil) | ||
|
||
config := config.Configuration{ | ||
Routers: map[string]*config.Router{ | ||
"router1": { | ||
EntryPoints: []string{"http"}, | ||
Middlewares: []string{}, | ||
Service: "service1", | ||
Rule: "PathPrefix:/", | ||
}, | ||
}, | ||
Services: map[string]*config.Service{ | ||
"service1": { | ||
LoadBalancer: &config.LoadBalancerService{ | ||
Servers: []config.Server{ | ||
{ | ||
URL: "http://" + s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress + ":80", | ||
Weight: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
json, err := json.Marshal(config) | ||
c.Assert(err, checker.IsNil) | ||
|
||
request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8080/api/providers/rest", bytes.NewReader(json)) | ||
c.Assert(err, checker.IsNil) | ||
|
||
response, err := http.DefaultClient.Do(request) | ||
c.Assert(err, checker.IsNil) | ||
c.Assert(response.StatusCode, checker.Equals, http.StatusOK) | ||
|
||
err = try.GetRequest("http://127.0.0.1:8080/api/providers/rest/routers", 1000*time.Millisecond, try.BodyContains("PathPrefix:/")) | ||
c.Assert(err, checker.IsNil) | ||
|
||
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusOK)) | ||
c.Assert(err, checker.IsNil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package rest | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/containous/mux" | ||
"github.com/containous/traefik/config" | ||
"github.com/containous/traefik/log" | ||
"github.com/containous/traefik/provider" | ||
"github.com/containous/traefik/safe" | ||
"github.com/unrolled/render" | ||
) | ||
|
||
var _ provider.Provider = (*Provider)(nil) | ||
|
||
// Provider is a provider.Provider implementation that provides a Rest API. | ||
type Provider struct { | ||
configurationChan chan<- config.Message | ||
EntryPoint string `description:"EntryPoint" export:"true"` | ||
} | ||
|
||
var templatesRenderer = render.New(render.Options{Directory: "nowhere"}) | ||
|
||
// Init the provider. | ||
func (p *Provider) Init() error { | ||
return nil | ||
} | ||
|
||
// Append add rest provider routes on a router. | ||
func (p *Provider) Append(systemRouter *mux.Router) { | ||
systemRouter. | ||
Methods(http.MethodPut). | ||
Path("/api/providers/{provider}"). | ||
HandlerFunc(func(response http.ResponseWriter, request *http.Request) { | ||
|
||
vars := mux.Vars(request) | ||
if vars["provider"] != "rest" { | ||
response.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprint(response, "Only 'rest' provider can be updated through the REST API") | ||
return | ||
} | ||
|
||
configuration := new(config.Configuration) | ||
body, _ := ioutil.ReadAll(request.Body) | ||
err := json.Unmarshal(body, configuration) | ||
if err == nil { | ||
p.configurationChan <- config.Message{ProviderName: "rest", Configuration: configuration} | ||
err := templatesRenderer.JSON(response, http.StatusOK, configuration) | ||
if err != nil { | ||
log.WithoutContext().Error(err) | ||
} | ||
} else { | ||
log.WithoutContext().Errorf("Error parsing configuration %+v", err) | ||
http.Error(response, fmt.Sprintf("%+v", err), http.StatusBadRequest) | ||
} | ||
}) | ||
} | ||
|
||
// Provide allows the provider to provide configurations to traefik | ||
// using the given configuration channel. | ||
func (p *Provider) Provide(configurationChan chan<- config.Message, pool *safe.Pool) error { | ||
p.configurationChan = configurationChan | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters