forked from luraproject/lura
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
luraproject#10: gorilla/mux example added
- Loading branch information
Showing
4 changed files
with
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.PHONY: all deps build | ||
|
||
# This Makefile is a simple example that demonstrates usual steps to build a binary that can be run in the same | ||
# architecture that was compiled in. The "ldflags" in the build assure that any needed dependency is included in the | ||
# binary and no external dependencies are needed to run the service. | ||
|
||
KRAKEND_VERSION=$(shell git describe --always --long --dirty --tags) | ||
BIN_NAME=krakend_gorilla_example_${KRAKEND_VERSION} | ||
|
||
all: deps build | ||
|
||
deps: | ||
go get "github.com/devopsfaith/krakend/config/viper" | ||
go get "github.com/devopsfaith/krakend/proxy" | ||
go get "github.com/devopsfaith/krakend/router/mux" | ||
go get "github.com/devopsfaith/krakend/logging/gologging" | ||
go get "github.com/gorilla/mux" | ||
go get "gopkg.in/unrolled/secure.v1" | ||
|
||
build: | ||
go build -a -ldflags="-X github.com/devopsfaith/krakend/core.KrakendVersion=${KRAKEND_VERSION}" -o ${BIN_NAME} | ||
@echo "You can now use ./${BIN_NAME}" |
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,20 @@ | ||
Krakend GORILLA example | ||
==== | ||
|
||
## Build | ||
|
||
Go 1.8 is a requirement | ||
|
||
$ make | ||
|
||
## Run | ||
|
||
Running it as a common executable, logs are send to the stdOut and some options are available at the CLI | ||
|
||
$ ./krakend_gorilla_example | ||
Usage of ./krakend_gorilla_example: | ||
-c string | ||
Path to the configuration filename (default "/etc/krakend/configuration.json") | ||
-d Enable the debug | ||
-p int | ||
Port of the service |
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,78 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
gorilla "github.com/gorilla/mux" | ||
"gopkg.in/unrolled/secure.v1" | ||
|
||
"github.com/devopsfaith/krakend/config/viper" | ||
"github.com/devopsfaith/krakend/logging/gologging" | ||
"github.com/devopsfaith/krakend/proxy" | ||
"github.com/devopsfaith/krakend/router/mux" | ||
) | ||
|
||
func main() { | ||
port := flag.Int("p", 0, "Port of the service") | ||
logLevel := flag.String("l", "ERROR", "Logging level") | ||
debug := flag.Bool("d", false, "Enable the debug") | ||
configFile := flag.String("c", "/etc/krakend/configuration.json", "Path to the configuration filename") | ||
flag.Parse() | ||
|
||
parser := viper.New() | ||
serviceConfig, err := parser.Parse(*configFile) | ||
if err != nil { | ||
log.Fatal("ERROR:", err.Error()) | ||
} | ||
serviceConfig.Debug = serviceConfig.Debug || *debug | ||
if *port != 0 { | ||
serviceConfig.Port = *port | ||
} | ||
|
||
logger, err := gologging.NewLogger(*logLevel, os.Stdout, "[KRAKEND]") | ||
if err != nil { | ||
log.Fatal("ERROR:", err.Error()) | ||
} | ||
|
||
secureMiddleware := secure.New(secure.Options{ | ||
AllowedHosts: []string{"127.0.0.1:8080", "example.com", "ssl.example.com"}, | ||
SSLRedirect: false, | ||
SSLHost: "ssl.example.com", | ||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, | ||
STSSeconds: 315360000, | ||
STSIncludeSubdomains: true, | ||
STSPreload: true, | ||
FrameDeny: true, | ||
ContentTypeNosniff: true, | ||
BrowserXssFilter: true, | ||
ContentSecurityPolicy: "default-src 'self'", | ||
}) | ||
|
||
routerFactory := mux.NewFactory(mux.Config{ | ||
Engine: gorillaEngine{gorilla.NewRouter()}, | ||
ProxyFactory: proxy.DefaultFactory(logger), | ||
Middlewares: []mux.HandlerMiddleware{secureMiddleware}, | ||
Logger: logger, | ||
HandlerFactory: mux.EndpointHandler, | ||
DebugPattern: "/__debug/{params}", | ||
}) | ||
|
||
routerFactory.New().Run(serviceConfig) | ||
} | ||
|
||
type gorillaEngine struct { | ||
r *gorilla.Router | ||
} | ||
|
||
// Handle implements the mux.Engine interface from the krakend router package | ||
func (g gorillaEngine) Handle(pattern string, handler http.Handler) { | ||
g.r.Handle(pattern, handler) | ||
} | ||
|
||
// ServeHTTP implements the http:Handler interface from the stdlib | ||
func (g gorillaEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
g.r.ServeHTTP(w, r) | ||
} |