-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from circa10a/metrics-and-refactor-shit
Metrics and refactor shit
- Loading branch information
Showing
17 changed files
with
587 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.