Skip to content

Instantly share code, notes, and snippets.

@ben181231
Last active September 7, 2021 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ben181231/2f0a608d2dcd57a9ac4b409b7c8ba6a9 to your computer and use it in GitHub Desktop.
Save ben181231/2f0a608d2dcd57a9ac4b409b7c8ba6a9 to your computer and use it in GitHub Desktop.
Skeleton of Google Cloud Function with wire
module somewhere.else/playgrounds/ping
go 1.17
require github.com/google/wire v0.5.0
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8=
github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
.PHONY: build
build:
@echo "Nothing to build"
.PHONY: deploy
deploy: deploy/Ping
.PHONY: deploy/Ping
deploy/Ping:
gcloud functions deploy Ping \
--region asia-northeast1 \
--runtime go116 \
--trigger-http
package ping
type Model struct {
welcomeMessage string
}
func NewModel() Model {
return Model{
welcomeMessage: "Hello World",
}
}
package ping
import (
"encoding/json"
"net/http"
)
type response struct {
Message string `json:"message,omitempty"`
}
var Ping = func() func(http.ResponseWriter, *http.Request) {
deps := getDeps()
return func(w http.ResponseWriter, _ *http.Request) {
resp := response{deps.model.welcomeMessage}
_ = json.NewEncoder(w).Encode(resp)
}
}()
//go:generate wire
//go:build wireinject
// +build wireinject
package ping
import "github.com/google/wire"
type Deps struct {
model Model
}
var modelSet = wire.NewSet(
NewModel,
wire.Struct(new(Deps), "model"),
)
func getDeps() *Deps {
wire.Build(modelSet)
return &Deps{}
}
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//+build !wireinject
package ping
import (
"github.com/google/wire"
)
// Injectors from wire.go:
func getDeps() *Deps {
model := NewModel()
deps := &Deps{
model: model,
}
return deps
}
// wire.go:
type Deps struct {
model Model
}
var modelSet = wire.NewSet(
NewModel, wire.Struct(new(Deps), "model"),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment