Skip to content

Commit

Permalink
rss example added
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed May 13, 2017
1 parent 298ef85 commit 5ff92ad
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
**/krakend_gorilla*
**/krakend_negroni*
**/krakend_httpcache*
**/krakend_rss*
server.rsa.crt
server.rsa.key
*.json
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all deps test build benchmark coveralls build_gin_example build_dns_example build_mux_example build_gorilla_example build_negroni_example build_httpcache_example
.PHONY: all deps test build benchmark coveralls build_gin_example build_dns_example build_mux_example build_gorilla_example build_negroni_example build_httpcache_example build_rss_example

PACKAGES = $(shell go list ./... | grep -v /examples/)

Expand All @@ -11,6 +11,7 @@ deps:
go get -u github.com/gorilla/mux
go get -u github.com/urfave/negroni
go get -u github.com/clbanning/mxj/x2j
go get -u github.com/mmcdole/gofeed

test:
go fmt ./...
Expand All @@ -20,7 +21,7 @@ test:
benchmark:
go test -bench=. -benchtime=3s $(PACKAGES)

build: build_gin_example build_dns_example build_mux_example build_gorilla_example build_negroni_example build_httpcache_example
build: build_gin_example build_dns_example build_mux_example build_gorilla_example build_negroni_example build_httpcache_example build_rss_example

build_gin_example:
cd examples/gin/ && make && cd ../.. && cp examples/gin/krakend_gin_example* .
Expand All @@ -40,6 +41,9 @@ build_negroni_example:
build_httpcache_example:
cd examples/httpcache/ && make && cd ../.. && cp examples/httpcache/krakend_httpcache_example* .

build_rss_example:
cd examples/rss/ && make && cd ../.. && cp examples/rss/krakend_rss_example* .

coveralls: all
go get github.com/mattn/goveralls
sh coverage.sh --coveralls
20 changes: 20 additions & 0 deletions examples/rss/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.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_rss_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/gin"
go get "github.com/devopsfaith/krakend/logging/gologging"

build:
go build -ldflags="-X github.com/devopsfaith/krakend/core.KrakendVersion=${KRAKEND_VERSION}" -o ${BIN_NAME}
@echo "You can now use ./${BIN_NAME}"
59 changes: 59 additions & 0 deletions examples/rss/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Krakend RSS 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_rss_example
Usage of ./krakend_rss_example:
-c string
Path to the configuration filename (default "/etc/krakend/configuration.json")
-d Enable the debug
-p int
Port of the service

This is an example of a suitable json configuration

{
"version": 1,
"name": "TV shows gateway",
"port": 8080,
"cache_ttl": 3600,
"timeout": "3s",
"endpoints": [
{
"endpoint": "/showrss/{id}",
"backend": [
{
"host": [
"http://showrss.info/"
],
"url_pattern": "/user/schedule/{id}.rss",
"encoding": "rss",
"group": "schedule",
"whitelist": ["items", "title"]
},
{
"host": [
"http://showrss.info/"
],
"url_pattern": "/user/{id}.rss",
"encoding": "rss",
"group": "available",
"whitelist": ["items", "title"]
}
]
}
]
}

You can see the resulting output with this simple curl command (replace `<YOUR_SHOWRSS_USER_ID>` with your own id)

$ curl -i http://127.0.0.1:8080/showrss/<YOUR_SHOWRSS_USER_ID>
56 changes: 56 additions & 0 deletions examples/rss/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"flag"
"log"
"os"

"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/config/viper"
"github.com/devopsfaith/krakend/logging"
"github.com/devopsfaith/krakend/logging/gologging"
"github.com/devopsfaith/krakend/proxy"
"github.com/devopsfaith/krakend/router/gin"
)

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())
}

gin.DefaultFactory(proxy.DefaultFactory(logger), logger).
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 5ff92ad

Please sign in to comment.