Skip to content

Commit

Permalink
Refactor routing to use paths generated from hosts #59
Browse files Browse the repository at this point in the history
Simplified some things
  • Loading branch information
Marcel Ludwig committed Oct 26, 2020
1 parent c934719 commit 1d64bec
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 697 deletions.
4 changes: 2 additions & 2 deletions command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func (r Run) Execute(args Args, config *config.Gateway, logEntry *logrus.Entry)
httpConf = httpConf.Merge(envConf)

// logEntry has still the 'daemon' type which can be used for config related load errors.
entrypointHandlers := runtime.BuildEntrypointHandlers(config, httpConf, logEntry)
srvMux := runtime.NewServerConfiguration(config, httpConf, logEntry)

ctx := ContextWithSignal(context.Background())
serverList, listenCmdShutdown := server.NewServerList(ctx, logEntry.Logger, httpConf, entrypointHandlers)
serverList, listenCmdShutdown := server.NewServerList(ctx, logEntry.Logger, httpConf, srvMux)
for _, srv := range serverList {
srv.Listen()
}
Expand Down
5 changes: 0 additions & 5 deletions config/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,4 @@ type Endpoint struct {
InlineDefinition hcl.Body `hcl:",remain" json:"-"`
Path string `hcl:"path,optional"`
Pattern string `hcl:"path,label"`
Server *Server `hcl:"-"` // parent
}

func (e *Endpoint) String() string {
return e.Server.Name + ": " + e.Pattern
}
40 changes: 0 additions & 40 deletions config/runtime/handler_internal_test.go

This file was deleted.

6 changes: 2 additions & 4 deletions config/runtime/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import (

type Port string

type HostHandlers map[string]*ServerMux

type EntrypointHandlers map[Port]HostHandlers
type Server map[Port]*ServerMux

// ServerMux represents the ServerMux struct.
type ServerMux struct {
Server *config.Server
Mux *Mux
Mux *MuxOptions
}

// HTTPConfig represents the configuration of the ingress HTTP server.
Expand Down
66 changes: 56 additions & 10 deletions config/runtime/mux.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,63 @@
package runtime

import (
"net/http"
"path/filepath"
"strings"

"github.com/avenga/couper/config"
"github.com/avenga/couper/errors"
"github.com/avenga/couper/handler"
"github.com/avenga/couper/utils"
)

// Mux represents a Mux object.
type Mux struct {
API Routes
APIPath string
APIErrTpl *errors.Template
FS Routes
FSPath string
FSErrTpl *errors.Template
SPA Routes
SPAPath string
type MuxOptions struct {
APIErrTpl *errors.Template
APIPath string
EndpointRoutes map[string]http.Handler
FileHandler http.Handler
FileErrTpl *errors.Template
SPARoutes map[string]http.Handler
}

func NewMuxOptions(conf *config.Server) (*MuxOptions, error) {
options := &MuxOptions{
APIErrTpl: errors.DefaultJSON,
FileErrTpl: errors.DefaultHTML,
EndpointRoutes: make(map[string]http.Handler),
SPARoutes: make(map[string]http.Handler),
}

if conf.API != nil {
options.APIPath = utils.JoinPath("/", conf.BasePath, conf.API.BasePath)
for strings.HasSuffix(options.APIPath, "/") {
options.APIPath = options.APIPath[:len(options.APIPath)-1]
}

if conf.API.ErrorFile != "" {
tpl, err := errors.NewTemplateFromFile(conf.Files.ErrorFile)
if err != nil {
return nil, err
}
options.APIErrTpl = tpl
}
}

if conf.Files != nil {
if conf.Files.ErrorFile != "" {
tpl, err := errors.NewTemplateFromFile(conf.Files.ErrorFile)
if err != nil {
return nil, err
}
options.FileErrTpl = tpl
}

absPath, err := filepath.Abs(conf.Files.DocumentRoot)
if err != nil {
return nil, err
}
options.FileHandler = handler.NewFile(conf.Files.BasePath, absPath, options.FileErrTpl)
}

return options, nil
}
116 changes: 0 additions & 116 deletions config/runtime/route.go

This file was deleted.

57 changes: 0 additions & 57 deletions config/runtime/route_internal_test.go

This file was deleted.

Loading

0 comments on commit 1d64bec

Please sign in to comment.