diff --git a/chi/Makefile b/chi/Makefile new file mode 100644 index 0000000..0efdcb0 --- /dev/null +++ b/chi/Makefile @@ -0,0 +1,24 @@ +.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_chi_example_${KRAKEND_VERSION} + +all: deps build + +deps: + go get "github.com/devopsfaith/krakend/config" + go get "github.com/devopsfaith/krakend/logging" + go get "github.com/devopsfaith/krakend/proxy" + go get "github.com/devopsfaith/krakend/router" + go get "github.com/devopsfaith/krakend/router/chi" + go get "github.com/go-chi/chi" + go get "github.com/go-chi/chi/middleware" + go get "github.com/go-chi/cors" + +build: + go build -a -ldflags="-X github.com/devopsfaith/krakend/core.KrakendVersion=${KRAKEND_VERSION}" -o ${BIN_NAME} + @echo "You can now use ./${BIN_NAME}" diff --git a/chi/README.md b/chi/README.md new file mode 100644 index 0000000..c1cd76f --- /dev/null +++ b/chi/README.md @@ -0,0 +1,24 @@ +Krakend Chi example +==== + +## Build + +Go 1.8 is a requirement + +```bash +$ make +``` + +## Run + +Running it as a common executable, logs are send to the stdOut and some options are available at the CLI + +```bash +$ ./krakend_chi_example + Usage of ./krakend_chi_example: + -c string + Path to the configuration filename (default "/etc/krakend/configuration.json") + -d Enable the debug + -p int + Port of the service +``` diff --git a/chi/main.go b/chi/main.go new file mode 100644 index 0000000..76038f2 --- /dev/null +++ b/chi/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "flag" + "log" + "net/http" + "os" + + "github.com/devopsfaith/krakend/config" + "github.com/devopsfaith/krakend/logging" + "github.com/devopsfaith/krakend/proxy" + "github.com/devopsfaith/krakend/router" + krakendchi "github.com/devopsfaith/krakend/router/chi" + "github.com/go-chi/chi" + "github.com/go-chi/chi/middleware" + "github.com/go-chi/cors" +) + +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 := config.NewParser() + 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 := logging.NewLogger(*logLevel, os.Stdout, "[KRAKEND]") + if err != nil { + log.Fatal("ERROR:", err.Error()) + } + + mws := []func(http.Handler) http.Handler{ + cors.Handler(cors.Options{ + AllowedOrigins: []string{"*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: false, + MaxAge: 300, // Maximum value not ignored by any of major browsers + }), + middleware.Throttle(20), + middleware.Logger, + } + + routerFactory := krakendchi.NewFactory(krakendchi.Config{ + Engine: chi.NewRouter(), + ProxyFactory: customProxyFactory{logger, proxy.DefaultFactory(logger)}, + Middlewares: mws, + Logger: logger, + HandlerFactory: func(cfg *config.EndpointConfig, prxy proxy.Proxy) http.HandlerFunc { + return krakendchi.NewEndpointHandler(cfg, prxy) + }, + RunServer: router.RunServer, + }) + + routerFactory.New().Run(serviceConfig) +} + +// customProxyFactory adds a logging middleware wrapping the internal factory +type customProxyFactory struct { + logger logging.Logger + factory proxy.Factory +} + +// New implements the Factory interface +func (cf customProxyFactory) New(cfg *config.EndpointConfig) (p proxy.Proxy, err error) { + p, err = cf.factory.New(cfg) + if err == nil { + p = proxy.NewLoggingMiddleware(cf.logger, cfg.Endpoint)(p) + } + return +}