Skip to content

Commit

Permalink
Merge pull request #2 from novalagung/master
Browse files Browse the repository at this point in the history
add example: chi
  • Loading branch information
kpacha authored Jul 22, 2020
2 parents 5c37684 + b25cca8 commit 0123db2
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
24 changes: 24 additions & 0 deletions chi/Makefile
Original file line number Diff line number Diff line change
@@ -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}"
24 changes: 24 additions & 0 deletions chi/README.md
Original file line number Diff line number Diff line change
@@ -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
```
81 changes: 81 additions & 0 deletions chi/main.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0123db2

Please sign in to comment.