Skip to content

Commit

Permalink
Revert "Merge pull request #45 from Financial-Times/add-api-key-valid…
Browse files Browse the repository at this point in the history
…ation"

This reverts commit a6e7189, reversing
changes made to c95d236.
  • Loading branch information
Sandor Torok committed Jul 14, 2017
1 parent a6e7189 commit 8fa3893
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 456 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
/notifications-push-client
/.env
*.iml
vendor/*/

/.project
34 changes: 15 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
FROM golang:1.8-alpine
FROM alpine:3.5

ENV PROJECT=notifications-push
COPY . /${PROJECT}-sources/
COPY . .git /notifications-push/

RUN apk --no-cache --virtual .build-dependencies add git \
&& ORG_PATH="github.com/Financial-Times" \
&& REPO_PATH="${ORG_PATH}/${PROJECT}" \
&& mkdir -p $GOPATH/src/${ORG_PATH} \
# Linking the project sources in the GOPATH folder
&& ln -s /${PROJECT}-sources $GOPATH/src/${REPO_PATH} \
RUN apk --update add git go libc-dev \
&& export GOPATH=/gopath \
&& REPO_PATH="github.com/Financial-Times/notifications-push" \
&& mkdir -p $GOPATH/src/${REPO_PATH} \
&& mv /notifications-push/* $GOPATH/src/${REPO_PATH} \
&& cd $GOPATH/src/${REPO_PATH} \
&& BUILDINFO_PACKAGE="${ORG_PATH}/${PROJECT}/vendor/${ORG_PATH}/service-status-go/buildinfo." \
&& BUILDINFO_PACKAGE="github.com/Financial-Times/service-status-go/buildinfo." \
&& VERSION="version=$(git describe --tag --always 2> /dev/null)" \
&& DATETIME="dateTime=$(date -u +%Y%m%d%H%M%S)" \
&& REPOSITORY="repository=$(git config --get remote.origin.url)" \
&& REVISION="revision=$(git rev-parse HEAD)" \
&& BUILDER="builder=$(go version)" \
&& LDFLAGS="-X '"${BUILDINFO_PACKAGE}$VERSION"' -X '"${BUILDINFO_PACKAGE}$DATETIME"' -X '"${BUILDINFO_PACKAGE}$REPOSITORY"' -X '"${BUILDINFO_PACKAGE}$REVISION"' -X '"${BUILDINFO_PACKAGE}$BUILDER"'" \
&& echo "Build flags: $LDFLAGS" \
&& echo "Fetching dependencies..." \
&& go get -u github.com/kardianos/govendor \
&& $GOPATH/bin/govendor sync \
&& echo $LDFLAGS \
&& go get -t ./... \
&& go build -ldflags="${LDFLAGS}" \
&& mv ${PROJECT} /${PROJECT}-app \
&& apk del .build-dependencies \
&& mv notifications-push /notifications-push-app \
&& rm -rf /notifications-push \
&& mv /notifications-push-app /notifications-push \
&& apk del go git libc-dev \
&& rm -rf $GOPATH /var/cache/apk/*

WORKDIR /

CMD [ "/notifications-push-app" ]
CMD [ "/notifications-push" ]
37 changes: 9 additions & 28 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"net/http"
_ "net/http/pprof"
"os"
Expand All @@ -11,21 +9,23 @@ import (
"strings"
"time"

"fmt"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"

fthealth "github.com/Financial-Times/go-fthealth/v1a"
queueConsumer "github.com/Financial-Times/message-queue-gonsumer/consumer"
"github.com/Financial-Times/notifications-push/consumer"
"github.com/Financial-Times/notifications-push/dispatcher"
"github.com/Financial-Times/notifications-push/resources"
"github.com/Financial-Times/service-status-go/httphandlers"
"github.com/jawher/mow.cli"
"net"
)

const heartbeatPeriod = 30 * time.Second

func init() {
f := &log.JSONFormatter{
f := &log.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
}

Expand Down Expand Up @@ -76,12 +76,6 @@ func main() {
Desc: "The API base URL where resources are accessible",
EnvVar: "API_BASE_URL",
})
apiKeyValidationEndpoint := app.String(cli.StringOpt{
Name: "api_key_validation_endpoint",
Value: "t800/a",
Desc: "The Mashery ApiKey validation endpoint",
EnvVar: "API_KEY_VALIDATION_ENDPOINT",
})
topic := app.String(cli.StringOpt{
Name: "topic",
Value: "",
Expand Down Expand Up @@ -144,22 +138,9 @@ func main() {
}

queueHandler := consumer.NewMessageQueueHandler(whitelistR, mapper, dispatcher)
consumer := queueConsumer.NewBatchedConsumer(consumerConfig, queueHandler.HandleMessage, &http.Client{})

tr := &http.Transport{
MaxIdleConnsPerHost: 32,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
}
httpClient := &http.Client{
Transport: tr,
Timeout: time.Duration(10 * time.Second),
}

consumer := queueConsumer.NewBatchedConsumer(consumerConfig, queueHandler.HandleMessage, httpClient)
masheryApiKeyValidationUrl := fmt.Sprintf("%s/%s", *apiBaseURL, *apiKeyValidationEndpoint)
go server(":"+strconv.Itoa(*port), *resource, dispatcher, history, consumerConfig, masheryApiKeyValidationUrl, httpClient)
go server(":"+strconv.Itoa(*port), *resource, dispatcher, history, consumerConfig)

pushService := newPushService(dispatcher, consumer)
pushService.start()
Expand All @@ -170,12 +151,12 @@ func main() {
}
}

func server(listen string, resource string, dispatcher dispatcher.Dispatcher, history dispatcher.History, consumerConfig queueConsumer.QueueConfig, masheryApiKeyValidationUrl string, httpClient *http.Client) {
func server(listen string, resource string, dispatcher dispatcher.Dispatcher, history dispatcher.History, consumerConfig queueConsumer.QueueConfig) {
notificationsPushPath := "/" + resource + "/notifications-push"

r := mux.NewRouter()

r.HandleFunc(notificationsPushPath, resources.Push(dispatcher, masheryApiKeyValidationUrl, httpClient)).Methods("GET")
r.HandleFunc(notificationsPushPath, resources.Push(dispatcher)).Methods("GET")
r.HandleFunc("/__history", resources.History(history)).Methods("GET")
r.HandleFunc("/__stats", resources.Stats(dispatcher)).Methods("GET")

Expand Down
42 changes: 22 additions & 20 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
machine:
environment:
PROJECT_GOPATH: "${HOME}/.go_project"
PROJECT_PARENT_PATH: "${PROJECT_GOPATH}/src/github.com/${CIRCLE_PROJECT_USERNAME}"
PROJECT_PATH: "${PROJECT_PARENT_PATH}/${CIRCLE_PROJECT_REPONAME}"
GOPATH: "${HOME}/.go_workspace:${PROJECT_GOPATH}"

checkout:
GODIST: "go1.7.3.linux-amd64.tar.gz"
post:
- mkdir -p "${PROJECT_PARENT_PATH}"
- ln -sf "${HOME}/${CIRCLE_PROJECT_REPONAME}/" "${PROJECT_PATH}"

- mkdir -p download
- test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST
- sudo rm -rf /usr/local/go
- sudo tar -C /usr/local -xzf download/$GODIST
services:
- docker
dependencies:
pre:
- go get -u github.com/kardianos/govendor
override:
- cd $PROJECT_PATH && govendor sync
- cd $PROJECT_PATH && go build -v

- go get github.com/axw/gocov/gocov; go get github.com/matm/gocov-html; go get -u github.com/jstemmer/go-junit-report
test:
pre:
- go get -u github.com/jstemmer/go-junit-report
- go get -u github.com/mattn/goveralls
- cd $PROJECT_PATH && wget https://raw.githubusercontent.com/Financial-Times/cookiecutter-upp-golang/master/coverage.sh && chmod +x coverage.sh
- go get github.com/mattn/goveralls
override:
- mkdir -p $CIRCLE_TEST_REPORTS/golang
- cd $PROJECT_PATH && govendor test -race -v +local | go-junit-report > $CIRCLE_TEST_REPORTS/golang/junit.xml
- cd $PROJECT_PATH && ./coverage.sh
- go test -race -v ./... | go-junit-report > $CIRCLE_TEST_REPORTS/golang/junit.xml
- go test -v -cover -race -coverprofile=$CIRCLE_ARTIFACTS/consumer.out ./consumer
- go test -v -cover -race -coverprofile=$CIRCLE_ARTIFACTS/dispatcher.out ./dispatcher
- go test -v -cover -race -coverprofile=$CIRCLE_ARTIFACTS/resources.out ./resources
- cd $CIRCLE_ARTIFACTS && sed -i '1d' *.out
- |
echo "mode: atomic" > $CIRCLE_ARTIFACTS/overall-coverage.result
- cd $CIRCLE_ARTIFACTS && cat *.out >> overall-coverage.result
- docker build --rm=false -t test/notifications-push .
- docker run -d -p 9200:8080 test/notifications-push; sleep 3
- curl --retry 10 --retry-delay 5 -v http://localhost:9200/__health
post:
- goveralls -coverprofile=$CIRCLE_ARTIFACTS/coverage.out -service=circle-ci -repotoken=$COVERALLS_TOKEN
- goveralls -coverprofile=$CIRCLE_ARTIFACTS/overall-coverage.result -service=circle-ci -repotoken=$COVERALLS_TOKEN
- bash <(curl -s https://codecov.io/bash)
23 changes: 1 addition & 22 deletions resources/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,15 @@ import (
log "github.com/Sirupsen/logrus"
)

const (
apiKeyHeaderField = "X-Api-Key"
apiKeyQueryParam = "apiKey"
)

//ApiKey is provided either as a request param or as a header.
func getApiKey(r *http.Request) string {
apiKey := r.Header.Get(apiKeyHeaderField)
if apiKey != "" {
return apiKey
}

return r.URL.Query().Get(apiKeyQueryParam)
}

// Push handler for push subscribers
func Push(reg dispatcher.Registrar, masheryApiKeyValidationURL string, httpClient *http.Client) func(w http.ResponseWriter, r *http.Request) {
func Push(reg dispatcher.Registrar) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")

apiKey := getApiKey(r)
if isValid, errMsg, errStatusCode := isValidApiKey(apiKey, masheryApiKeyValidationURL, httpClient); !isValid {
http.Error(w, errMsg, errStatusCode)
return
}

cn, ok := w.(http.CloseNotifier)
if !ok {
http.Error(w, "Cannot stream.", http.StatusInternalServerError)
Expand Down
Loading

0 comments on commit 8fa3893

Please sign in to comment.