Skip to content

Commit

Permalink
luraproject#10: gorilla/mux example added
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Mar 17, 2017
1 parent a551566 commit 193bc49
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 4 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
examples/gin/krakend_gin*
examples/mux/krakend_mux*
krakend_gin*
krakend_mux*
**/krakend_gin*
**/krakend_mux*
**/krakend_gorilla*
server.rsa.crt
server.rsa.key
*.json
Expand Down
22 changes: 22 additions & 0 deletions examples/gorilla/Makefile
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}"
20 changes: 20 additions & 0 deletions examples/gorilla/README.md
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
78 changes: 78 additions & 0 deletions examples/gorilla/main.go
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)
}

0 comments on commit 193bc49

Please sign in to comment.