dependency-injection

command module
v0.0.0-...-ac68d48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

Dependency Injection Example

Source Code

// ezcx/examples/webhook-quickstart is a refactoring of the Google Cloud provided
// Go webhook quickstart: https://cloud.google.com/dialogflow/cx/docs/quick/webhook

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/yaq-cc/ezcx"
)

var (
	PORT = os.Getenv("PORT")
)

func main() {
	ctx := context.Background()
	lg := log.Default()
	lg.Println(PORT)
	server := ezcx.NewServer(ctx, ":"+PORT, lg)
	deps := NewDependencies()
	server.HandleCx("/confirm", deps.cxConfirm)
	server.HandleCx("/hello", cxHello(deps))
	server.ListenAndServe(ctx)
}

// Dependencies represents access to resources.  The contained resources
// should be safe for concurrent access and use by multiple goroutines.
// An example of this would be *sql.DB which is a handle for the clients
// underlying connection pool.
//
// In general, dependencies should provide access to state - but contained
// dependencies should be stateless - i.e. they're meant to provide access
// to state stored separately.
type Dependencies struct{}

func NewDependencies() *Dependencies {
	return new(Dependencies)
}

// Structural approach.
func (d *Dependencies) cxConfirm(res *ezcx.WebhookResponse, req *ezcx.WebhookRequest) error {
	params := req.GetSessionParameters()

	size := params["size"]
	color := params["color"]

	res.AddTextResponse(
		fmt.Sprintf("You can pick up your order for a %s %s shirt in 5 days.",
			size, color),
	)

	params["cancel-period"] = "2"
	res.AddSessionParameters(params)

	return nil
}

// Functional approach via closure.
func cxHello(d *Dependencies) ezcx.HandlerFunc {
	return func(res *ezcx.WebhookResponse, req *ezcx.WebhookRequest) error {
		res.AddTextResponse("It's ... really this easy.")
		return nil
	}
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL