Skip to content

Commit

Permalink
🎨 Update Structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nkmr-jp committed May 4, 2022
1 parent fcda756 commit c16f620
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 104 deletions.
19 changes: 6 additions & 13 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
# This file specifies files that are *not* uploaded to Google Cloud
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

node_modules
*_test.go
.go-version
Makefile
*.md
log
.idea
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#See: https://cloud.google.com/functions/docs/2nd-gen/getting-started#pubsub

FUNC_NAME=fetch
ENTRY_POINT=Fetch
PROJECT_ID=$(shell gcloud config get-value project)
PROJECT_NUMBER=$(shell gcloud projects list --filter="project_id:$(PROJECT_ID)" --format='value(project_number)')

start:
export FUNCTION_TARGET=HelloPubSub; go run cmd/main.go
export FUNCTION_TARGET=$(ENTRY_POINT); go run cmd/main.go

init:
gcloud projects add-iam-policy-binding $(PROJECT_ID) \
Expand All @@ -18,7 +19,7 @@ deploy:
--gen2 \
--runtime go116 \
--trigger-topic $(FUNC_NAME)-topic \
--entry-point HelloPubSub \
--entry-point $(ENTRY_POINT) \
--source .

show:
Expand Down
107 changes: 18 additions & 89 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,118 +2,47 @@ package fetch

import (
"context"
"encoding/json"
"fmt"
"log"
"os"

"cloud.google.com/go/logging"
"cloud.google.com/go/pubsub"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/davecgh/go-spew/spew"
"github.com/nkmr-jp/zl"
"go.uber.org/zap"
)

// Entry defines a log entry.
type Entry struct {
Message string `json:"message"`
Severity string `json:"severity,omitempty"`
Trace string `json:"logging.googleapis.com/trace,omitempty"`

// Logs Explorer allows filtering and display of this as `jsonPayload.component`.
Component string `json:"component,omitempty"`
}

func (e Entry) String() string {
if e.Severity == "" {
e.Severity = "INFO"
}
out, err := json.Marshal(e)
if err != nil {
log.Printf("json.Marshal: %v", err)
}
return string(out)
// MessagePublishedData
// See: https://cloud.google.com/eventarc/docs/cloudevents#pubsub
// See: https://googleapis.github.io/google-cloudevents/examples/binary/pubsub/MessagePublishedData-complex.json
type MessagePublishedData struct {
Message pubsub.Message
}

func init() {
functions.CloudEvent("HelloPubSub", helloPubSub)
}

// MessagePublishedData contains the full Pub/Sub message
// See the documentation for more details:
// https://cloud.google.com/eventarc/docs/cloudevents#pubsub
type MessagePublishedData struct {
Message pubsub.Message
initLogger()
functions.CloudEvent("Fetch", Run)
}

func helloPubSub(ctx context.Context, e event.Event) error {
// loggingTest(e)
func Run(ctx context.Context, e event.Event) error {
zl.Info("RUN_GCF_FETCH")
var msg MessagePublishedData

if err := e.DataAs(&msg); err != nil {
return fmt.Errorf("event.DataAs: %v", err)
zl.Error("DATA_AS_ERROR", err)
}

name := string(msg.Message.Data) // Automatically decoded from base64.
name := string(msg.Message.Data)
if name == "" {
name = "World"
}

log.Printf("Hello, %s!", name)

zlTest()
fmt.Printf("Hello, %s!", name)

return nil
}

func zlTest() {
func initLogger() {
if os.Getenv("FUNCTION_TARGET") != "" {
zl.SetOutput(zl.ConsoleOutput)
}
zl.SetLevel(zl.DebugLevel)
zl.SetOutput(zl.ConsoleOutput)
zl.Init()
defer zl.Sync() // flush log buffer

zl.Info("test message", zap.String("param1", "value1"))

zl.Info("test message2",
zap.String("severity", "info"),
zap.String("payload", `{"param1":"val1"}`),
)
}

func loggingTest(e event.Event) {
fmt.Println(json.Marshal(e))
spew.Dump(e)
fmt.Printf("%+v\n", e)
log.Println("This is stderr")
fmt.Println("This is stdout")

// Structured logging can be used to set severity levels.
// See https://cloud.google.com/logging/docs/structured-logging.
fmt.Println(`{"message": "This has ERROR severity", "severity": "error"}`)

log.Println(Entry{
Severity: "NOTICE",
Message: "This is the default display field.",
Component: "arbitrary-property",
})
logging.Notice.String()
l := logging.Entry{
// Timestamp: time.Time{},
Severity: logging.Notice,
Payload: "test1",
Labels: map[string]string{
"key1": "val1",
"key2": "val2",
},
InsertID: "",
HTTPRequest: nil,
Operation: nil,
LogName: "",
Resource: nil,
Trace: "",
SpanID: "",
TraceSampled: false,
SourceLocation: nil,
}
log.Println(l)
}
30 changes: 30 additions & 0 deletions fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fetch_test

import (
"context"
"testing"

"cloud.google.com/go/pubsub"
"github.com/cloudevents/sdk-go/v2/event"
fetch "github.com/nkmr-jp/gcf-fetch"
"github.com/stretchr/testify/assert"
)

func TestFetch(t *testing.T) {
msg := fetch.MessagePublishedData{
Message: pubsub.Message{
Data: []byte("test message"),
},
}

e := event.New()
e.SetDataContentType("application/json")
if err := e.SetData(e.DataContentType(), msg); err != nil {
assert.Fail(t, err.Error())
}
// Send message
if err := fetch.Run(context.Background(), e); err != nil {
assert.Fail(t, err.Error())
}
assert.Equal(t, "Something", "Something")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/cloudevents/sdk-go/v2 v2.6.1
github.com/davecgh/go-spew v1.1.1
github.com/nkmr-jp/zl v0.1.1-0.20220430214813-b1c66a2f0d4c
github.com/stretchr/testify v1.7.0
github.com/thoas/go-funk v0.9.2 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nkmr-jp/zl v0.1.1-0.20220430214813-b1c66a2f0d4c h1:D3AVU+cPRZg0RHlG9J11ZtbKgwCQceFCyH0sMT9uwPM=
github.com/nkmr-jp/zl v0.1.1-0.20220430214813-b1c66a2f0d4c/go.mod h1:ofAIvArYbODxKIia/ftm89RO0fSZdrYHIwVBwf7wKEA=
Expand Down Expand Up @@ -658,6 +660,7 @@ google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscL
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
Expand Down

0 comments on commit c16f620

Please sign in to comment.