Skip to content

Commit

Permalink
Merge pull request #2 from circa10a/metrics-and-refactor-shit
Browse files Browse the repository at this point in the history
Metrics and refactor shit
  • Loading branch information
circa10a authored Feb 9, 2020
2 parents 56fa8e6 + 8504a19 commit 8665846
Show file tree
Hide file tree
Showing 17 changed files with 587 additions and 148 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
language: go

go:
- '1.13'
services:
- docker

script:
- go vet ./...
- make test

before_deploy: |
if [ ! -f /tmp/tagged ]; then
git config --local user.name "$USERNAME"
Expand All @@ -18,6 +20,7 @@ before_deploy: |
# Update go report card
curl -X POST -F "repo=github.com/${TRAVIS_REPO_SLUG}" https://goreportcard.com/checks
fi
deploy:
provider: releases
api_key:
Expand Down
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM golang:alpine
WORKDIR /go/src/app
COPY . .
ENV USER=go \
UID=1000 \
GID=1000 \
GOOS=linux \
GOARCH=amd64 \
CGO_ENABLED=0

RUN go build -ldflags="-s -w" -o webhook && \
addgroup --gid "$GID" "$USER" && \
adduser \
--disabled-password \
--gecos "" \
--home "$(pwd)" \
--ingroup "$USER" \
--no-create-home \
--uid "$UID" \
"$USER" && \
chown "$UID":"$GID" /go/src/app/webhook

FROM scratch
ENV GIN_MODE=release
COPY --from=0 /etc/passwd /etc/passwd
COPY --from=0 /go/src/app/webhook /
USER 1000
ENTRYPOINT ["/webhook"]
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ GORUN=$(GOCMD) run
GOBUILDFLAGS=-ldflags="-s -w"
PROJECT=circa10a/google-home-aws-news
BINARY=webhook
VERSION=0.1.0
VERSION=0.2.0

# First target for travis ci
test:
$(GOCMD) test -v ./...

build:
$(GOBUILD) -o $(BINARY)
Expand All @@ -15,9 +19,9 @@ run:

compile:
GOOS=linux GOARCH=amd64 go build $(GOBUILDFLAGS) -o bin/$(BINARY)-linux-amd64
GOOS=linux GOARCH=arm go build $(GOBUILDFLAGS) -o bin/$(BINARY)-linux-arm
GOOS=linux GOARCH=arm64 go build $(GOBUILDFLAGS) -o bin/$(BINARY)-linux-arm64
GOOS=darwin GOARCH=amd64 go build $(GOBUILDFLAGS) -o bin/$(BINARY)-darwin-amd64
# GOOS=linux GOARCH=arm go build $(GOBUILDFLAGS) -o bin/$(BINARY)-linux-arm
# GOOS=linux GOARCH=arm64 go build $(GOBUILDFLAGS) -o bin/$(BINARY)-linux-arm64
# GOOS=darwin GOARCH=amd64 go build $(GOBUILDFLAGS) -o bin/$(BINARY)-darwin-amd64

clean:
$(GOCLEAN)
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ _Powered by [go-aws-news](https://github.com/circa10a/go-aws-news)_

- "OK Google, Talk to cloud computing news"

> Note: News items are cache for 8 hours, then renewed with another request
> Note: News items are cached for 8 hours, then renewed with another request
### Example View

Expand All @@ -28,14 +28,24 @@ _Powered by [go-aws-news](https://github.com/circa10a/go-aws-news)_
| GIN MODE | Runs web server in production or debug mode |`GIN_MODE` | NONE | `false` | `release` |
| PORT | Port for web server to listen on | `PORT` | NONE | `false` | `8080` |

## Metrics

Prometheus metrics for usage are available at `/metrics`

## Development

### Build
### Test

```shell
make
```

### Build

```shell
make build
```

### Run

```shell
Expand Down
32 changes: 32 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"time"

awsnews "github.com/circa10a/go-aws-news/news"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)

func createCache(d time.Duration, c time.Duration) *cache.Cache {
return cache.New(d, c)
}

func getNewsFromCache() awsnews.Announcements {
news, found := Cache.Get(CacheKey)
if found {
return news.(awsnews.Announcements)
}
setNewsInCache()
return getNewsFromCache()
}

func setNewsInCache() {
news, err := awsnews.FetchYear(time.Now().Year())
log.Info("News fetched")
if err != nil {
log.Error(err)
}
Cache.Set(CacheKey, news.Last(10), cache.DefaultExpiration)
log.Info("Cache renewed")
}
16 changes: 16 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetNewsFromCache(t *testing.T) {
assert.Equal(t, len(getNewsFromCache()), 10)
}

func TestSetNewsInCache(t *testing.T) {
setNewsInCache()
assert.Equal(t, len(getNewsFromCache()), 10)
}
14 changes: 14 additions & 0 deletions fulfillment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFulfillment(t *testing.T) {
payload := fulfillment()
_, jsonErr := json.Marshal(payload)
assert.NoError(t, jsonErr)
}
80 changes: 80 additions & 0 deletions fulfilment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

// Modeled after google docs
// https://developers.google.com/assistant/conversational/responses#browsing_carousel

// Response is the entire JSON payload response
type Response struct {
Payload Payload `json:"payload"`
}

// Payload is a google defined higher structure, see https://developers.google.com/assistant/conversational/responses#browsing_carousel
type Payload struct {
Google Google `json:"google"`
}

// Google is another google defined higher structure, see https://developers.google.com/assistant/conversational/responses#browsing_carousel
type Google struct {
ExpectUserResponse bool `json:"expectUserResponse"`
RichResponse RichResponse `json:"richResponse,omitempty"`
}

// RichResponse gives UI representation of the news data fetched
type RichResponse struct {
Items []Item `json:"items,omitempty"`
}

// Item provides different interactions
type Item struct {
SimpleResponse *SimpleResponse `json:"simpleResponse,omitempty"`
CarouselBrowse *CarouselBrowse `json:"carouselBrowse,omitempty"`
}

// Simple response provides audio only feedback
type SimpleResponse struct {
TextToSpeech string `json:"textToSpeech"`
}

// CarouselBrowse provides a UI list of items with hyperlinks
type CarouselBrowse struct {
Items []CarouselItem `json:"items"`
}

// CarouselItem is a UI entry for each news item
type CarouselItem struct {
Title string `json:"title"`
OpenURLAction OpenURLAction `json:"openUrlAction"`
Description string `json:"description,omitempty"`
}

// OpenURLAction provides a url to be opened in the client's browser when touched
type OpenURLAction struct {
URL string `json:"url"`
}

// fulfillment builds out the full struct for the JSON response
func fulfillment() *Response {
news := getNewsListItems()

return &Response{
Payload{
Google{
ExpectUserResponse: false,
RichResponse: RichResponse{
Items: []Item{
{
SimpleResponse: &SimpleResponse{
TextToSpeech: defaultNewsStatement(news),
},
},
{
CarouselBrowse: &CarouselBrowse{
Items: news,
},
},
},
},
},
},
}
}
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@ go 1.13

require (
github.com/PuerkitoBio/goquery v1.5.1 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/circa10a/go-aws-news v0.4.0
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gavv/httpexpect v2.0.0+incompatible
github.com/gin-gonic/gin v1.5.0
github.com/golang/protobuf v1.3.3 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/gorilla/websocket v1.4.1 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-runewidth v0.0.8 // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.4.1
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.4.0
github.com/valyala/fasthttp v1.9.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
github.com/zsais/go-gin-prometheus v0.1.0
)
Loading

0 comments on commit 8665846

Please sign in to comment.