Compare commits
20 Commits
18f2ffaaa9
...
plugin
Author | SHA1 | Date | |
---|---|---|---|
ad5c02bd80 | |||
fe90d81b5d | |||
9343970672 | |||
b73723b5f5 | |||
985ce3eba3 | |||
f48503f2b6 | |||
5d53e32316 | |||
76dea96a46 | |||
636b2dbf8f | |||
42aba649c8 | |||
67e87adf89 | |||
60d9fde890 | |||
bc82c8b59c | |||
682d70ed30 | |||
fa49bcf0c7 | |||
e4293b7b8b | |||
7a88321a69 | |||
35bd6d65bb | |||
4d82e29793 | |||
b2a83e3022 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1 +1,3 @@
|
||||
/coverage
|
||||
/coverage
|
||||
/bin
|
||||
/.vscode
|
5
Makefile
5
Makefile
@ -1,6 +1,9 @@
|
||||
test:
|
||||
go clean -testcache
|
||||
go test -v ./...
|
||||
go test -v -race ./...
|
||||
|
||||
watch:
|
||||
modd
|
||||
|
||||
deps:
|
||||
go get -u golang.org/x/tools/cmd/godoc
|
||||
|
52
cqrs/bus_test.go
Normal file
52
cqrs/bus_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
package cqrs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testCommandRequest struct {
|
||||
Foo string
|
||||
}
|
||||
|
||||
func TestSimpleCommandExec(t *testing.T) {
|
||||
dispatcher := NewDispatcher()
|
||||
|
||||
handlerCalled := false
|
||||
|
||||
handleTestCommand := func(ctx context.Context, cmd Command) error {
|
||||
handlerCalled = true
|
||||
return nil
|
||||
}
|
||||
|
||||
dispatcher.RegisterCommand(
|
||||
MatchCommandRequest(&testCommandRequest{}),
|
||||
CommandHandlerFunc(handleTestCommand),
|
||||
)
|
||||
|
||||
cmd := &testCommandRequest{
|
||||
Foo: "bar",
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
result, err := dispatcher.Exec(ctx, cmd)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if result == nil {
|
||||
t.Error("result should not be nil")
|
||||
}
|
||||
|
||||
if result.Command() == nil {
|
||||
t.Error("result.Command() should not be nil")
|
||||
}
|
||||
|
||||
if e, g := result.Command().Request(), cmd; e != g {
|
||||
t.Errorf("result.Command().Request(): expected '%v', got '%v'", e, g)
|
||||
}
|
||||
|
||||
if e, g := handlerCalled, true; e != g {
|
||||
t.Errorf("handlerCalled: expected '%v', got '%v'", e, g)
|
||||
}
|
||||
}
|
61
cqrs/command.go
Normal file
61
cqrs/command.go
Normal file
@ -0,0 +1,61 @@
|
||||
package cqrs
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CommandID string
|
||||
|
||||
type CommandRequest interface {
|
||||
}
|
||||
|
||||
type Command interface {
|
||||
ID() CommandID
|
||||
Request() CommandRequest
|
||||
}
|
||||
|
||||
type BaseCommand struct {
|
||||
id CommandID
|
||||
req CommandRequest
|
||||
}
|
||||
|
||||
func (c *BaseCommand) ID() CommandID {
|
||||
return c.id
|
||||
}
|
||||
|
||||
func (c *BaseCommand) Request() CommandRequest {
|
||||
return c.req
|
||||
}
|
||||
|
||||
func NewBaseCommand(req CommandRequest) *BaseCommand {
|
||||
id := CommandID(uuid.New().String())
|
||||
return &BaseCommand{id, req}
|
||||
}
|
||||
|
||||
type CommandResult interface {
|
||||
Command() Command
|
||||
}
|
||||
|
||||
type BaseCommandResult struct {
|
||||
cmd Command
|
||||
}
|
||||
|
||||
func (r *BaseCommandResult) Command() Command {
|
||||
return r.cmd
|
||||
}
|
||||
|
||||
func NewBaseCommandResult(cmd Command) *BaseCommandResult {
|
||||
return &BaseCommandResult{cmd}
|
||||
}
|
||||
|
||||
type CommandHandlerFunc func(context.Context, Command) error
|
||||
|
||||
func (h CommandHandlerFunc) Handle(ctx context.Context, cmd Command) error {
|
||||
return h(ctx, cmd)
|
||||
}
|
||||
|
||||
type CommandHandler interface {
|
||||
Handle(context.Context, Command) error
|
||||
}
|
92
cqrs/dispatcher.go
Normal file
92
cqrs/dispatcher.go
Normal file
@ -0,0 +1,92 @@
|
||||
package cqrs
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Dispatcher struct {
|
||||
reg *Registry
|
||||
cmdFactory CommandFactory
|
||||
cmdResultFactory CommandResultFactory
|
||||
qryFactory QueryFactory
|
||||
qryResultFactory QueryResultFactory
|
||||
}
|
||||
|
||||
func (d *Dispatcher) Exec(ctx context.Context, req CommandRequest) (CommandResult, error) {
|
||||
cmd, err := d.cmdFactory(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hdlr, mdlwrs, err := d.reg.MatchCommand(cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(mdlwrs) > 0 {
|
||||
for i := len(mdlwrs) - 1; i >= 0; i-- {
|
||||
hdlr = mdlwrs[i](hdlr)
|
||||
}
|
||||
}
|
||||
|
||||
if err := hdlr.Handle(ctx, cmd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := d.cmdResultFactory(cmd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (d *Dispatcher) Query(ctx context.Context, req QueryRequest) (QueryResult, error) {
|
||||
qry, err := d.qryFactory(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hdlr, mdlwrs, err := d.reg.MatchQuery(qry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(mdlwrs) > 0 {
|
||||
for i := len(mdlwrs) - 1; i >= 0; i-- {
|
||||
hdlr = mdlwrs[i](hdlr)
|
||||
}
|
||||
}
|
||||
|
||||
data, err := hdlr.Handle(ctx, qry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := d.qryResultFactory(qry, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (d *Dispatcher) RegisterCommand(match MatchFunc, hdlr CommandHandler, mdlwrs ...CommandMiddleware) {
|
||||
d.reg.RegisterCommand(match, hdlr, mdlwrs...)
|
||||
}
|
||||
|
||||
func (d *Dispatcher) RegisterQuery(match MatchFunc, hdlr QueryHandler, mdlwrs ...QueryMiddleware) {
|
||||
d.reg.RegisterQuery(match, hdlr, mdlwrs...)
|
||||
}
|
||||
|
||||
func NewDispatcher(funcs ...OptionFunc) *Dispatcher {
|
||||
opt := CreateOption(funcs...)
|
||||
|
||||
return &Dispatcher{
|
||||
reg: NewRegistry(),
|
||||
cmdFactory: opt.CommandFactory,
|
||||
cmdResultFactory: opt.CommandResultFactory,
|
||||
qryFactory: opt.QueryFactory,
|
||||
qryResultFactory: opt.QueryResultFactory,
|
||||
}
|
||||
}
|
9
cqrs/error.go
Normal file
9
cqrs/error.go
Normal file
@ -0,0 +1,9 @@
|
||||
package cqrs
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrHandlerNotFound = errors.New("handler not found")
|
||||
ErrUnexpectedRequest = errors.New("unexpected request")
|
||||
ErrUnexpectedData = errors.New("unexpected data")
|
||||
)
|
33
cqrs/match.go
Normal file
33
cqrs/match.go
Normal file
@ -0,0 +1,33 @@
|
||||
package cqrs
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type MatchFunc func(interface{}) (bool, error)
|
||||
|
||||
func MatchCommandRequest(req interface{}) MatchFunc {
|
||||
reqType := reflect.TypeOf(req)
|
||||
|
||||
return func(raw interface{}) (bool, error) {
|
||||
cmd, ok := raw.(Command)
|
||||
if !ok {
|
||||
return false, ErrUnexpectedRequest
|
||||
}
|
||||
|
||||
return reflect.TypeOf(cmd.Request()) == reqType, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MatchQueryRequest(req interface{}) MatchFunc {
|
||||
reqType := reflect.TypeOf(req)
|
||||
|
||||
return func(raw interface{}) (bool, error) {
|
||||
cmd, ok := raw.(Query)
|
||||
if !ok {
|
||||
return false, ErrUnexpectedRequest
|
||||
}
|
||||
|
||||
return reflect.TypeOf(cmd.Request()) == reqType, nil
|
||||
}
|
||||
}
|
42
cqrs/match_test.go
Normal file
42
cqrs/match_test.go
Normal file
@ -0,0 +1,42 @@
|
||||
package cqrs
|
||||
|
||||
import "testing"
|
||||
|
||||
type testType struct{}
|
||||
|
||||
func TestMatchCommandRequestType(t *testing.T) {
|
||||
match := MatchCommandRequest(&testType{})
|
||||
|
||||
cmd := NewBaseCommand(&testType{})
|
||||
|
||||
matches, err := match(cmd)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if e, g := true, matches; e != g {
|
||||
t.Errorf("match(&testType{}): expected '%v', got '%v'", e, g)
|
||||
}
|
||||
|
||||
cmd = NewBaseCommand(nil)
|
||||
|
||||
matches, err = match(cmd)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if e, g := false, matches; e != g {
|
||||
t.Errorf("match(nil): expected '%v', got '%v'", e, g)
|
||||
}
|
||||
|
||||
cmd = NewBaseCommand("test")
|
||||
|
||||
matches, err = match(cmd)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if e, g := false, matches; e != g {
|
||||
t.Errorf("match(\"test\"): expected '%v', got '%v'", e, g)
|
||||
}
|
||||
}
|
64
cqrs/middleware_test.go
Normal file
64
cqrs/middleware_test.go
Normal file
@ -0,0 +1,64 @@
|
||||
package cqrs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBasicCommandMiddleware(t *testing.T) {
|
||||
dispatcher := NewDispatcher()
|
||||
|
||||
handlerCalled := false
|
||||
|
||||
handleTestCommand := func(ctx context.Context, cmd Command) error {
|
||||
handlerCalled = true
|
||||
return nil
|
||||
}
|
||||
|
||||
middlewareCalled := false
|
||||
|
||||
commandMiddleware := func(next CommandHandler) CommandHandler {
|
||||
fn := func(ctx context.Context, cmd Command) error {
|
||||
middlewareCalled = true
|
||||
return next.Handle(ctx, cmd)
|
||||
}
|
||||
|
||||
return CommandHandlerFunc(fn)
|
||||
}
|
||||
|
||||
dispatcher.RegisterCommand(
|
||||
MatchCommandRequest(&testCommandRequest{}),
|
||||
CommandHandlerFunc(handleTestCommand),
|
||||
commandMiddleware,
|
||||
)
|
||||
|
||||
cmd := &testCommandRequest{
|
||||
Foo: "bar",
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
result, err := dispatcher.Exec(ctx, cmd)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if result == nil {
|
||||
t.Error("result should not be nil")
|
||||
}
|
||||
|
||||
if result.Command() == nil {
|
||||
t.Error("result.Command() should not be nil")
|
||||
}
|
||||
|
||||
if e, g := result.Command().Request(), cmd; e != g {
|
||||
t.Errorf("result.Command().Request(): expected '%v', got '%v'", e, g)
|
||||
}
|
||||
|
||||
if e, g := middlewareCalled, true; e != g {
|
||||
t.Errorf("middlewareCalled: expected '%v', got '%v'", e, g)
|
||||
}
|
||||
|
||||
if e, g := handlerCalled, true; e != g {
|
||||
t.Errorf("handlerCalled: expected '%v', got '%v'", e, g)
|
||||
}
|
||||
}
|
86
cqrs/option.go
Normal file
86
cqrs/option.go
Normal file
@ -0,0 +1,86 @@
|
||||
package cqrs
|
||||
|
||||
type Option struct {
|
||||
CommandFactory CommandFactory
|
||||
QueryFactory QueryFactory
|
||||
CommandResultFactory CommandResultFactory
|
||||
QueryResultFactory QueryResultFactory
|
||||
}
|
||||
|
||||
type CommandFactory func(CommandRequest) (Command, error)
|
||||
type QueryFactory func(QueryRequest) (Query, error)
|
||||
type CommandResultFactory func(Command) (CommandResult, error)
|
||||
type QueryResultFactory func(Query, interface{}) (QueryResult, error)
|
||||
|
||||
type OptionFunc func(*Option)
|
||||
|
||||
func DefaultOption() *Option {
|
||||
funcs := []OptionFunc{
|
||||
WithBaseQueryFactory(),
|
||||
WithBaseCommandFactory(),
|
||||
WithBaseQueryResultFactory(),
|
||||
WithBaseCommandResultFactory(),
|
||||
}
|
||||
|
||||
opt := &Option{}
|
||||
|
||||
for _, fn := range funcs {
|
||||
fn(opt)
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
func CreateOption(funcs ...OptionFunc) *Option {
|
||||
opt := DefaultOption()
|
||||
|
||||
for _, fn := range funcs {
|
||||
fn(opt)
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
func WithCommandFactory(factory CommandFactory) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.CommandFactory = factory
|
||||
}
|
||||
}
|
||||
|
||||
func WithQueryFactory(factory QueryFactory) OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.QueryFactory = factory
|
||||
}
|
||||
}
|
||||
|
||||
func WithBaseQueryFactory() OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.QueryFactory = func(req QueryRequest) (Query, error) {
|
||||
return NewBaseQuery(req), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithBaseCommandFactory() OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.CommandFactory = func(req CommandRequest) (Command, error) {
|
||||
return NewBaseCommand(req), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithBaseQueryResultFactory() OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.QueryResultFactory = func(qry Query, data interface{}) (QueryResult, error) {
|
||||
return NewBaseQueryResult(qry, data), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithBaseCommandResultFactory() OptionFunc {
|
||||
return func(opt *Option) {
|
||||
opt.CommandResultFactory = func(cmd Command) (CommandResult, error) {
|
||||
return NewBaseCommandResult(cmd), nil
|
||||
}
|
||||
}
|
||||
}
|
13
cqrs/provider.go
Normal file
13
cqrs/provider.go
Normal file
@ -0,0 +1,13 @@
|
||||
package cqrs
|
||||
|
||||
import "gitlab.com/wpetit/goweb/service"
|
||||
|
||||
// ServiceProvider returns a service.Provider for the
|
||||
// the cqrs service
|
||||
func ServiceProvider() service.Provider {
|
||||
dispatcher := NewDispatcher()
|
||||
|
||||
return func(container *service.Container) (interface{}, error) {
|
||||
return dispatcher, nil
|
||||
}
|
||||
}
|
66
cqrs/query.go
Normal file
66
cqrs/query.go
Normal file
@ -0,0 +1,66 @@
|
||||
package cqrs
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type QueryID string
|
||||
|
||||
type QueryRequest interface{}
|
||||
|
||||
type Query interface {
|
||||
ID() QueryID
|
||||
Request() QueryRequest
|
||||
}
|
||||
|
||||
type BaseQuery struct {
|
||||
id QueryID
|
||||
req QueryRequest
|
||||
}
|
||||
|
||||
func (q *BaseQuery) ID() QueryID {
|
||||
return q.id
|
||||
}
|
||||
|
||||
func (q *BaseQuery) Request() QueryRequest {
|
||||
return q.req
|
||||
}
|
||||
|
||||
func NewBaseQuery(req QueryRequest) *BaseQuery {
|
||||
id := QueryID(uuid.New().String())
|
||||
return &BaseQuery{id, req}
|
||||
}
|
||||
|
||||
type QueryResult interface {
|
||||
Query() Query
|
||||
Data() interface{}
|
||||
}
|
||||
|
||||
type BaseQueryResult struct {
|
||||
qry Query
|
||||
data interface{}
|
||||
}
|
||||
|
||||
func (r *BaseQueryResult) Query() Query {
|
||||
return r.qry
|
||||
}
|
||||
|
||||
func (r *BaseQueryResult) Data() interface{} {
|
||||
return r.data
|
||||
}
|
||||
|
||||
func NewBaseQueryResult(qry Query, data interface{}) *BaseQueryResult {
|
||||
return &BaseQueryResult{qry, data}
|
||||
}
|
||||
|
||||
type QueryHandlerFunc func(context.Context, Query) (interface{}, error)
|
||||
|
||||
func (h QueryHandlerFunc) Handle(ctx context.Context, qry Query) (interface{}, error) {
|
||||
return h(ctx, qry)
|
||||
}
|
||||
|
||||
type QueryHandler interface {
|
||||
Handle(context.Context, Query) (interface{}, error)
|
||||
}
|
68
cqrs/registry.go
Normal file
68
cqrs/registry.go
Normal file
@ -0,0 +1,68 @@
|
||||
package cqrs
|
||||
|
||||
type CommandMiddleware func(next CommandHandler) CommandHandler
|
||||
type QueryMiddleware func(next QueryHandler) QueryHandler
|
||||
|
||||
type Registry struct {
|
||||
commands []*commandHandler
|
||||
queries []*queryHandler
|
||||
}
|
||||
|
||||
type commandHandler struct {
|
||||
Match MatchFunc
|
||||
Handler CommandHandler
|
||||
Middlewares []CommandMiddleware
|
||||
}
|
||||
|
||||
type queryHandler struct {
|
||||
Match MatchFunc
|
||||
Handler QueryHandler
|
||||
Middlewares []QueryMiddleware
|
||||
}
|
||||
|
||||
func (r *Registry) MatchCommand(cmd Command) (CommandHandler, []CommandMiddleware, error) {
|
||||
// TODO cache matching to avoid unnecessary ops
|
||||
for _, reg := range r.commands {
|
||||
matches, err := reg.Match(cmd)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if matches {
|
||||
return reg.Handler, reg.Middlewares, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil, ErrHandlerNotFound
|
||||
}
|
||||
|
||||
func (r *Registry) MatchQuery(qry Query) (QueryHandler, []QueryMiddleware, error) {
|
||||
// TODO cache matching to avoid unnecessary ops
|
||||
for _, reg := range r.queries {
|
||||
matches, err := reg.Match(qry)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if matches {
|
||||
return reg.Handler, reg.Middlewares, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil, ErrHandlerNotFound
|
||||
}
|
||||
|
||||
func (r *Registry) RegisterCommand(match MatchFunc, hdlr CommandHandler, mdlwrs ...CommandMiddleware) {
|
||||
r.commands = append(r.commands, &commandHandler{match, hdlr, mdlwrs})
|
||||
}
|
||||
|
||||
func (r *Registry) RegisterQuery(match MatchFunc, hdlr QueryHandler, mdlwrs ...QueryMiddleware) {
|
||||
r.queries = append(r.queries, &queryHandler{match, hdlr, mdlwrs})
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{
|
||||
commands: make([]*commandHandler, 0),
|
||||
queries: make([]*queryHandler, 0),
|
||||
}
|
||||
}
|
34
cqrs/service.go
Normal file
34
cqrs/service.go
Normal file
@ -0,0 +1,34 @@
|
||||
package cqrs
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
)
|
||||
|
||||
// ServiceName defines the cqrs service name
|
||||
const ServiceName service.Name = "cqrs"
|
||||
|
||||
// From retrieves the cqrs service in the given container
|
||||
func From(container *service.Container) (*Dispatcher, error) {
|
||||
raw, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
srv, ok := raw.(*Dispatcher)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// Must retrieves the cqrs service in the given container or panic otherwise
|
||||
func Must(container *service.Container) *Dispatcher {
|
||||
service, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return service
|
||||
}
|
1
example/pluggable/.gitignore
vendored
Normal file
1
example/pluggable/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/bin
|
11
example/pluggable/Makefile
Normal file
11
example/pluggable/Makefile
Normal file
@ -0,0 +1,11 @@
|
||||
build: extension
|
||||
go build -o ./bin/app ./
|
||||
|
||||
extension:
|
||||
go build -o ./bin/myplugin.so -buildmode=plugin ./myplugin
|
||||
|
||||
watch:
|
||||
modd
|
||||
|
||||
run:
|
||||
./bin/app
|
5
example/pluggable/hook/initializable.go
Normal file
5
example/pluggable/hook/initializable.go
Normal file
@ -0,0 +1,5 @@
|
||||
package hook
|
||||
|
||||
type Initializable interface {
|
||||
HookInit() error
|
||||
}
|
44
example/pluggable/main.go
Normal file
44
example/pluggable/main.go
Normal file
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/example/pluggable/hook"
|
||||
"gitlab.com/wpetit/goweb/plugin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
reg := plugin.NewRegistry()
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := reg.LoadAll(ctx, "./bin/*.so")
|
||||
if err != nil {
|
||||
log.Fatal(errors.WithStack(err))
|
||||
}
|
||||
|
||||
for _, ext := range reg.Plugins() {
|
||||
log.Printf("Loaded plugin '%s', version '%s'", ext.PluginName(), ext.PluginVersion())
|
||||
}
|
||||
|
||||
// Iterate over plugins
|
||||
err = reg.Each(func(p plugin.Plugin) error {
|
||||
h, ok := p.(hook.Initializable)
|
||||
if !ok {
|
||||
// Skip non initializable plugins
|
||||
return nil
|
||||
}
|
||||
|
||||
// Initialize plugin
|
||||
if err := h.HookInit(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(errors.WithStack(err))
|
||||
}
|
||||
}
|
6
example/pluggable/modd.conf
Normal file
6
example/pluggable/modd.conf
Normal file
@ -0,0 +1,6 @@
|
||||
**/*.go
|
||||
modd.conf
|
||||
Makefile {
|
||||
prep: make build
|
||||
prep: make run
|
||||
}
|
11
example/pluggable/myplugin/main.go
Normal file
11
example/pluggable/myplugin/main.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/wpetit/goweb/plugin"
|
||||
)
|
||||
|
||||
func RegisterPlugin(ctx context.Context) (plugin.Plugin, error) {
|
||||
return &MyPlugin{}, nil
|
||||
}
|
19
example/pluggable/myplugin/plugin.go
Normal file
19
example/pluggable/myplugin/plugin.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
type MyPlugin struct{}
|
||||
|
||||
func (e *MyPlugin) PluginName() string {
|
||||
return "my.plugin"
|
||||
}
|
||||
|
||||
func (e *MyPlugin) PluginVersion() string {
|
||||
return "0.0.0"
|
||||
}
|
||||
|
||||
func (e *MyPlugin) HookInit() error {
|
||||
log.Println("MyPlugin initialized !")
|
||||
|
||||
return nil
|
||||
}
|
7
go.mod
7
go.mod
@ -3,16 +3,17 @@ module gitlab.com/wpetit/goweb
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.0
|
||||
cdr.dev/slog v1.3.0
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/go-chi/chi v4.0.2+incompatible
|
||||
github.com/go-playground/locales v0.12.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.16.0 // indirect
|
||||
github.com/google/uuid v1.1.1
|
||||
github.com/gorilla/securecookie v1.1.1
|
||||
github.com/gorilla/sessions v1.2.0
|
||||
github.com/leodido/go-urn v1.1.0 // indirect
|
||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
|
||||
github.com/pkg/errors v0.8.1
|
||||
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
|
||||
github.com/pkg/errors v0.9.1
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1
|
||||
)
|
||||
|
213
go.sum
213
go.sum
@ -1,33 +1,246 @@
|
||||
cdr.dev/slog v1.3.0 h1:MYN1BChIaVEGxdS7I5cpdyMC0+WfJfK8BETAfzfLUGQ=
|
||||
cdr.dev/slog v1.3.0/go.mod h1:C5OL99WyuOK8YHZdYY57dAPN1jK2WJlCdq2VP6xeQns=
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
|
||||
cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU=
|
||||
cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY=
|
||||
cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc=
|
||||
cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0=
|
||||
cloud.google.com/go v0.49.0/go.mod h1:hGvAdzcWNbyuxS3nWhD7H2cIJxjRRTRLQVB0bdputVY=
|
||||
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
|
||||
cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE=
|
||||
cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=
|
||||
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
|
||||
github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0=
|
||||
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
|
||||
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
|
||||
github.com/alecthomas/chroma v0.7.0 h1:z+0HgTUmkpRDRz0SRSdMaqOLfJV4F+N1FPDZUZIDUzw=
|
||||
github.com/alecthomas/chroma v0.7.0/go.mod h1:1U/PfCsTALWWYHDnsIQkxEBM0+6LLe0v8+RSVMOwxeY=
|
||||
github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
|
||||
github.com/alecthomas/kong v0.1.17-0.20190424132513-439c674f7ae0/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI=
|
||||
github.com/alecthomas/kong v0.2.1-0.20190708041108-0548c6b1afae/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI=
|
||||
github.com/alecthomas/kong-hcl v0.1.8-0.20190615233001-b21fea9723c8/go.mod h1:MRgZdU3vrFd05IQ89AxUZ0aYdF39BYoNFa324SodPCA=
|
||||
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E=
|
||||
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
|
||||
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
|
||||
github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk=
|
||||
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
|
||||
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
|
||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc=
|
||||
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
|
||||
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
|
||||
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 h1:uHTyIjqVhYRhLbJ8nIiOJHkEZZ+5YoOsAbD3sk82NiE=
|
||||
github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.2-0.20191216170541-340f1ebe299e/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
|
||||
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
|
||||
github.com/gorilla/csrf v1.6.0/go.mod h1:7tSf8kmjNYr7IWDCYhd3U8Ck34iQ/Yw5CJu7bAkHEGI=
|
||||
github.com/gorilla/handlers v1.4.1/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
|
||||
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
|
||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||
github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ=
|
||||
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
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/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
|
||||
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
|
||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
|
||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
|
||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
|
||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
go.opencensus.io v0.22.2 h1:75k/FF0Q2YM8QYo07VPddOLBslDt1MZOdEslOHvmzAs=
|
||||
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g=
|
||||
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||
golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek=
|
||||
golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY=
|
||||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
|
||||
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
|
||||
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
|
||||
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk=
|
||||
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449 h1:gSbV7h1NRL2G1xTg/owz62CST1oJBmxy4QpMMregXVQ=
|
||||
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
|
||||
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
|
||||
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
|
||||
google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
|
||||
google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||
google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8=
|
||||
google.golang.org/genproto v0.0.0-20191115194625-c23dd37a84c9/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc=
|
||||
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
|
||||
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
|
||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||
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/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
||||
|
@ -20,7 +20,9 @@ func Example_usage() {
|
||||
|
||||
// On expose le service "template" avec l'implémentation
|
||||
// basée sur le moteur de rendu HTML de la librairie standard
|
||||
container.Provide(template.ServiceName, html.ServiceProvider("./templates"))
|
||||
container.Provide(template.ServiceName, html.ServiceProvider(
|
||||
html.NewDirectoryLoader("./templates"),
|
||||
))
|
||||
|
||||
router := chi.NewRouter()
|
||||
|
||||
|
109
logger/logger.go
Normal file
109
logger/logger.go
Normal file
@ -0,0 +1,109 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"cdr.dev/slog/sloggers/slogjson"
|
||||
|
||||
"cdr.dev/slog/sloggers/sloghuman"
|
||||
|
||||
"cdr.dev/slog"
|
||||
)
|
||||
|
||||
type Format string
|
||||
|
||||
const (
|
||||
FormatHuman Format = "human"
|
||||
FormatJSON Format = "json"
|
||||
)
|
||||
|
||||
var defaultLogger slog.Logger // nolint: gochecknoglobals
|
||||
|
||||
type Field = slog.Field
|
||||
type Map = slog.Map
|
||||
type Level = slog.Level
|
||||
|
||||
const (
|
||||
LevelCritical Level = slog.LevelCritical
|
||||
LevelError Level = slog.LevelError
|
||||
LevelWarn Level = slog.LevelWarn
|
||||
LevelInfo Level = slog.LevelInfo
|
||||
LevelDebug Level = slog.LevelDebug
|
||||
)
|
||||
|
||||
func init() { // nolint: gochecknoinits
|
||||
defaultLogger = Make(FormatHuman, os.Stdout)
|
||||
}
|
||||
|
||||
func Make(f Format, w io.Writer) slog.Logger {
|
||||
var logger slog.Logger
|
||||
|
||||
switch f {
|
||||
case FormatHuman:
|
||||
logger = sloghuman.Make(w)
|
||||
case FormatJSON:
|
||||
logger = slogjson.Make(w)
|
||||
default:
|
||||
panic(errors.Errorf("unknown logger format '%s'", f))
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
func Debug(ctx context.Context, msg string, fields ...Field) {
|
||||
slog.Helper()
|
||||
defaultLogger.Debug(ctx, msg, fields...)
|
||||
}
|
||||
|
||||
func Info(ctx context.Context, msg string, fields ...Field) {
|
||||
slog.Helper()
|
||||
defaultLogger.Info(ctx, msg, fields...)
|
||||
}
|
||||
|
||||
func Warn(ctx context.Context, msg string, fields ...Field) {
|
||||
slog.Helper()
|
||||
defaultLogger.Warn(ctx, msg, fields...)
|
||||
}
|
||||
|
||||
func Error(ctx context.Context, msg string, fields ...Field) {
|
||||
slog.Helper()
|
||||
defaultLogger.Error(ctx, msg, fields...)
|
||||
}
|
||||
|
||||
func Critical(ctx context.Context, msg string, fields ...Field) {
|
||||
slog.Helper()
|
||||
defaultLogger.Critical(ctx, msg, fields...)
|
||||
}
|
||||
|
||||
func Fatal(ctx context.Context, msg string, fields ...Field) {
|
||||
slog.Helper()
|
||||
defaultLogger.Fatal(ctx, msg, fields...)
|
||||
}
|
||||
|
||||
func F(name string, value interface{}) Field {
|
||||
return slog.F(name, value)
|
||||
}
|
||||
|
||||
func M(fields ...Field) Map {
|
||||
return slog.M(fields...)
|
||||
}
|
||||
|
||||
func E(err error) Field {
|
||||
return slog.Error(err)
|
||||
}
|
||||
|
||||
func SetLevel(level Level) {
|
||||
defaultLogger = defaultLogger.Leveled(level)
|
||||
}
|
||||
|
||||
func SetFormat(format Format) {
|
||||
defaultLogger = Make(format, os.Stdout)
|
||||
}
|
||||
|
||||
func With(ctx context.Context, fields ...Field) context.Context {
|
||||
return slog.With(ctx, fields...)
|
||||
}
|
@ -41,3 +41,9 @@ func Must(ctx context.Context) *service.Container {
|
||||
func ServiceContainer(container *service.Container) goweb.Middleware {
|
||||
return middleware.WithValue(KeyServiceContainer, container)
|
||||
}
|
||||
|
||||
// WithContainer returns a new context.Context with the given *service.Container
|
||||
// attached.
|
||||
func WithContainer(ctx context.Context, container *service.Container) context.Context {
|
||||
return context.WithValue(ctx, KeyServiceContainer, container)
|
||||
}
|
||||
|
16
plugin/error.go
Normal file
16
plugin/error.go
Normal file
@ -0,0 +1,16 @@
|
||||
package plugin
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrInvalidRegisterFunc is returned when the plugin package
|
||||
// could not find the expected RegisterPlugin func in the loaded
|
||||
// plugin.
|
||||
ErrInvalidRegisterFunc = errors.New("invalid register func")
|
||||
// ErrInvalidPlugin is returned when a loaded plugin does
|
||||
// not match the expected interface.
|
||||
ErrInvalidPlugin = errors.New("invalid plugin")
|
||||
// ErrPluginNotFound is returned when the given plugin could
|
||||
// not be found in the registry.
|
||||
ErrPluginNotFound = errors.New("plugin not found")
|
||||
)
|
6
plugin/plugin.go
Normal file
6
plugin/plugin.go
Normal file
@ -0,0 +1,6 @@
|
||||
package plugin
|
||||
|
||||
type Plugin interface {
|
||||
PluginName() string
|
||||
PluginVersion() string
|
||||
}
|
117
plugin/registry.go
Normal file
117
plugin/registry.go
Normal file
@ -0,0 +1,117 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Registry struct {
|
||||
plugins map[string]Plugin
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (r *Registry) Add(plg Plugin) {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
r.plugins[plg.PluginName()] = plg
|
||||
}
|
||||
|
||||
func (r *Registry) Get(name string) (Plugin, error) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
plg, exists := r.plugins[name]
|
||||
if !exists {
|
||||
return nil, errors.WithStack(ErrPluginNotFound)
|
||||
}
|
||||
|
||||
return plg, nil
|
||||
}
|
||||
|
||||
func (r *Registry) Load(ctx context.Context, path string) (Plugin, error) {
|
||||
p, err := plugin.Open(path)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
registerFuncSymbol, err := p.Lookup("RegisterPlugin")
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
register, ok := registerFuncSymbol.(func(context.Context) (Plugin, error))
|
||||
if !ok {
|
||||
return nil, errors.WithStack(ErrInvalidRegisterFunc)
|
||||
}
|
||||
|
||||
plg, err := register(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
if plg == nil {
|
||||
return nil, errors.WithStack(ErrInvalidPlugin)
|
||||
}
|
||||
|
||||
r.Add(plg)
|
||||
|
||||
return plg, nil
|
||||
}
|
||||
|
||||
func (r *Registry) LoadAll(ctx context.Context, pattern string) ([]Plugin, error) {
|
||||
extensions := make([]Plugin, 0)
|
||||
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
for _, m := range matches {
|
||||
ext, err := r.Load(ctx, m)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
extensions = append(extensions, ext)
|
||||
}
|
||||
|
||||
return extensions, nil
|
||||
}
|
||||
|
||||
func (r *Registry) Plugins() []Plugin {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
plugins := make([]Plugin, 0, len(r.plugins))
|
||||
for _, p := range r.plugins {
|
||||
plugins = append(plugins, p)
|
||||
}
|
||||
|
||||
return plugins
|
||||
}
|
||||
|
||||
type IteratorFunc func(plg Plugin) error
|
||||
|
||||
func (r *Registry) Each(iterator IteratorFunc) error {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
for _, p := range r.plugins {
|
||||
if err := iterator(p); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{
|
||||
plugins: make(map[string]Plugin),
|
||||
}
|
||||
}
|
94
plugin/registry_test.go
Normal file
94
plugin/registry_test.go
Normal file
@ -0,0 +1,94 @@
|
||||
package plugin_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/plugin"
|
||||
)
|
||||
|
||||
type testPlugin struct {
|
||||
name string
|
||||
version string
|
||||
}
|
||||
|
||||
func (p *testPlugin) PluginName() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *testPlugin) PluginVersion() string {
|
||||
return p.version
|
||||
}
|
||||
|
||||
func (p *testPlugin) Foo() string {
|
||||
return "bar"
|
||||
}
|
||||
|
||||
func TestRegistryEach(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
reg := plugin.NewRegistry()
|
||||
|
||||
plugins := []*testPlugin{
|
||||
{"plugin.a", "0.0.0"},
|
||||
{"plugin.b", "0.0.1"},
|
||||
}
|
||||
|
||||
for _, p := range plugins {
|
||||
reg.Add(p)
|
||||
}
|
||||
|
||||
total := 0
|
||||
|
||||
err := reg.Each(func(p plugin.Plugin) error {
|
||||
total++
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(errors.WithStack(err))
|
||||
}
|
||||
|
||||
if e, g := len(plugins), total; e != g {
|
||||
t.Errorf("total: expected '%v', got '%v'", e, g)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
reg := plugin.NewRegistry()
|
||||
|
||||
plugins := []*testPlugin{
|
||||
{"plugin.a", "0.0.0"},
|
||||
{"plugin.b", "0.0.1"},
|
||||
}
|
||||
|
||||
for _, p := range plugins {
|
||||
reg.Add(p)
|
||||
}
|
||||
|
||||
for _, p := range plugins {
|
||||
plugin, err := reg.Get(p.name)
|
||||
if err != nil {
|
||||
t.Error(errors.WithStack(err))
|
||||
}
|
||||
|
||||
if e, g := p.name, plugin.PluginName(); e != g {
|
||||
t.Errorf("plugin.PluginName(): expected '%v', got '%v'", e, g)
|
||||
}
|
||||
|
||||
if e, g := p.version, plugin.PluginVersion(); e != g {
|
||||
t.Errorf("plugin.PluginVersion(): expected '%v', got '%v'", e, g)
|
||||
}
|
||||
}
|
||||
|
||||
p, err := reg.Get("plugin.c")
|
||||
if !errors.Is(err, plugin.ErrPluginNotFound) {
|
||||
t.Errorf("err: expected '%v', got '%v'", plugin.ErrPluginNotFound, err)
|
||||
}
|
||||
|
||||
if p != nil {
|
||||
t.Errorf("reg.Get(\"plugin.c\"): expected '%v', got '%v'", nil, p)
|
||||
}
|
||||
}
|
7
service/build/info.go
Normal file
7
service/build/info.go
Normal file
@ -0,0 +1,7 @@
|
||||
package build
|
||||
|
||||
type Info struct {
|
||||
ProjectVersion string
|
||||
GitRef string
|
||||
BuildDate string
|
||||
}
|
43
service/build/service.go
Normal file
43
service/build/service.go
Normal file
@ -0,0 +1,43 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName service.Name = "build"
|
||||
)
|
||||
|
||||
func ServiceProvider(projectVersion, gitRef, buildDate string) service.Provider {
|
||||
info := &Info{projectVersion, gitRef, buildDate}
|
||||
|
||||
return func(ctn *service.Container) (interface{}, error) {
|
||||
return info, nil
|
||||
}
|
||||
}
|
||||
|
||||
// From retrieves the build informations in the given service container
|
||||
func From(container *service.Container) (*Info, error) {
|
||||
service, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
srv, ok := service.(*Info)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// Must retrieves the user repository in the given service container or panic otherwise
|
||||
func Must(container *service.Container) *Info {
|
||||
srv, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
@ -1,5 +1,12 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
"gitlab.com/wpetit/goweb/service/build"
|
||||
)
|
||||
|
||||
// Data is some data to inject into the template
|
||||
type Data map[string]interface{}
|
||||
|
||||
@ -17,3 +24,14 @@ func Extend(data Data, extensions ...DataExtFunc) (Data, error) {
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func WithBuildInfo(w http.ResponseWriter, r *http.Request, ctn *service.Container) DataExtFunc {
|
||||
return func(data Data) (Data, error) {
|
||||
info, err := build.From(ctn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data["BuildInfo"] = info
|
||||
return data, nil
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@ -13,6 +14,7 @@ const ServiceName service.Name = "template"
|
||||
// Service is a templating service
|
||||
type Service interface {
|
||||
RenderPage(w http.ResponseWriter, templateName string, data interface{}) error
|
||||
Render(w io.Writer, templateName string, data interface{}) error
|
||||
}
|
||||
|
||||
// From retrieves the template service in the given container
|
||||
|
@ -10,20 +10,23 @@ import (
|
||||
// CreateCookieSessionStore creates and returns a new cookie session store
|
||||
// with random authentication and encryption keys
|
||||
func CreateCookieSessionStore(authKeySize, encryptKeySize int, defaultOptions *sessions.Options) (sessions.Store, error) {
|
||||
authenticationKey, err := generateRandomBytes(authKeySize)
|
||||
authenticationKey, err := GenerateRandomBytes(authKeySize)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error while generating cookie authentication key")
|
||||
}
|
||||
encryptionKey, err := generateRandomBytes(encryptKeySize)
|
||||
|
||||
encryptionKey, err := GenerateRandomBytes(encryptKeySize)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error while generating cookie encryption key")
|
||||
}
|
||||
|
||||
store := sessions.NewCookieStore(authenticationKey, encryptionKey)
|
||||
store.Options = defaultOptions
|
||||
|
||||
return store, nil
|
||||
}
|
||||
|
||||
func generateRandomBytes(n int) ([]byte, error) {
|
||||
func GenerateRandomBytes(n int) ([]byte, error) {
|
||||
b := make([]byte, n)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
|
56
template/html/loader.go
Normal file
56
template/html/loader.go
Normal file
@ -0,0 +1,56 @@
|
||||
package html
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Loader interface {
|
||||
Load(*TemplateService) error
|
||||
}
|
||||
|
||||
type DirectoryLoader struct {
|
||||
rootDir string
|
||||
}
|
||||
|
||||
func (l *DirectoryLoader) Load(srv *TemplateService) error {
|
||||
blockFiles, err := filepath.Glob(filepath.Join(l.rootDir, "blocks", "*.tmpl"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, f := range blockFiles {
|
||||
blockContent, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
blockName := filepath.Base(f)
|
||||
srv.AddBlock(blockName, string(blockContent))
|
||||
}
|
||||
|
||||
layoutFiles, err := filepath.Glob(filepath.Join(l.rootDir, "layouts", "*.tmpl"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Generate our templates map from our layouts/ and blocks/ directories
|
||||
for _, f := range layoutFiles {
|
||||
templateData, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
templateName := filepath.Base(f)
|
||||
|
||||
if err := srv.LoadLayout(templateName, string(templateData)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewDirectoryLoader(rootDir string) *DirectoryLoader {
|
||||
return &DirectoryLoader{rootDir}
|
||||
}
|
@ -6,6 +6,7 @@ import "html/template"
|
||||
type Options struct {
|
||||
Helpers template.FuncMap
|
||||
PoolSize int
|
||||
DevMode bool
|
||||
}
|
||||
|
||||
// OptionFunc configures options for the template service
|
||||
@ -48,6 +49,14 @@ func WithPoolSize(size int) OptionFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// WithDevMode configures the template service
|
||||
// to use the development mode (auto reload of templates).
|
||||
func WithDevMode(enabled bool) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.DevMode = enabled
|
||||
}
|
||||
}
|
||||
|
||||
func defaultOptions() *Options {
|
||||
options := &Options{}
|
||||
funcs := []OptionFunc{
|
||||
|
@ -4,9 +4,9 @@ import "gitlab.com/wpetit/goweb/service"
|
||||
|
||||
// ServiceProvider returns a service.Provider for the
|
||||
// the HTML template service implementation
|
||||
func ServiceProvider(templateDir string, funcs ...OptionFunc) service.Provider {
|
||||
templateService := NewTemplateService(funcs...)
|
||||
err := templateService.LoadTemplatesDir(templateDir)
|
||||
func ServiceProvider(loader Loader, funcs ...OptionFunc) service.Provider {
|
||||
templateService := NewTemplateService(loader, funcs...)
|
||||
err := templateService.Load()
|
||||
return func(container *service.Container) (interface{}, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -1,91 +1,79 @@
|
||||
package html
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/oxtoacart/bpool"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrLayoutNotFound = errors.New("layout not found")
|
||||
)
|
||||
|
||||
// TemplateService is a template/html based templating service
|
||||
type TemplateService struct {
|
||||
templates map[string]*template.Template
|
||||
pool *bpool.BufferPool
|
||||
helpers template.FuncMap
|
||||
mutex sync.RWMutex
|
||||
blocks map[string]string
|
||||
layouts map[string]*template.Template
|
||||
loader Loader
|
||||
pool *bpool.BufferPool
|
||||
helpers template.FuncMap
|
||||
devMode bool
|
||||
}
|
||||
|
||||
func (t *TemplateService) LoadLayout(name string, layout string, blocks []string) error {
|
||||
func (t *TemplateService) AddBlock(name string, blockContent string) {
|
||||
t.mutex.Lock()
|
||||
defer t.mutex.Unlock()
|
||||
|
||||
t.blocks[name] = blockContent
|
||||
}
|
||||
|
||||
func (t *TemplateService) LoadLayout(name string, layoutContent string) error {
|
||||
tmpl := template.New(name)
|
||||
tmpl.Funcs(t.helpers)
|
||||
|
||||
for _, b := range blocks {
|
||||
t.mutex.RLock()
|
||||
for _, b := range t.blocks {
|
||||
if _, err := tmpl.Parse(b); err != nil {
|
||||
t.mutex.RUnlock()
|
||||
return err
|
||||
}
|
||||
}
|
||||
t.mutex.RUnlock()
|
||||
|
||||
tmpl, err := tmpl.Parse(layout)
|
||||
tmpl, err := tmpl.Parse(layoutContent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.templates[name] = tmpl
|
||||
t.mutex.Lock()
|
||||
t.layouts[name] = tmpl
|
||||
t.mutex.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadTemplatesDir loads the templates used by the service
|
||||
func (t *TemplateService) LoadTemplatesDir(templatesDir string) error {
|
||||
|
||||
layoutFiles, err := filepath.Glob(filepath.Join(templatesDir, "layouts", "*.tmpl"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
blockFiles, err := filepath.Glob(filepath.Join(templatesDir, "blocks", "*.tmpl"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
blockTemplates := make([]string, 0, len(blockFiles))
|
||||
|
||||
for _, f := range blockFiles {
|
||||
templateData, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
blockTemplates = append(blockTemplates, string(templateData))
|
||||
}
|
||||
|
||||
// Generate our templates map from our layouts/ and blocks/ directories
|
||||
for _, f := range layoutFiles {
|
||||
templateData, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
templateName := filepath.Base(f)
|
||||
|
||||
if err := t.LoadLayout(templateName, string(templateData), blockTemplates); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
// Load parses templates via the configured template loader.
|
||||
func (t *TemplateService) Load() error {
|
||||
return t.loader.Load(t)
|
||||
}
|
||||
|
||||
// RenderPage renders a template to the given http.ResponseWriter
|
||||
func (t *TemplateService) RenderPage(w http.ResponseWriter, templateName string, data interface{}) error {
|
||||
if t.devMode {
|
||||
if err := t.loader.Load(t); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the template exists in the map.
|
||||
tmpl, ok := t.templates[templateName]
|
||||
tmpl, ok := t.layouts[templateName]
|
||||
if !ok {
|
||||
return fmt.Errorf("the template '%s' does not exist", templateName)
|
||||
return ErrLayoutNotFound
|
||||
}
|
||||
|
||||
// Create a buffer to temporarily write to and check if any errors were encountered.
|
||||
@ -103,15 +91,45 @@ func (t *TemplateService) RenderPage(w http.ResponseWriter, templateName string,
|
||||
|
||||
}
|
||||
|
||||
// Render renders a layout to the given io.Writer.
|
||||
func (t *TemplateService) Render(w io.Writer, layoutName string, data interface{}) error {
|
||||
if t.devMode {
|
||||
if err := t.loader.Load(t); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the template exists in the map.
|
||||
tmpl, ok := t.layouts[layoutName]
|
||||
if !ok {
|
||||
return ErrLayoutNotFound
|
||||
}
|
||||
|
||||
// Create a buffer to temporarily write to and check if any errors were encountered.
|
||||
buf := t.pool.Get()
|
||||
defer t.pool.Put(buf)
|
||||
|
||||
if err := tmpl.ExecuteTemplate(buf, layoutName, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := buf.WriteTo(w)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// NewTemplateService returns a new Service
|
||||
func NewTemplateService(funcs ...OptionFunc) *TemplateService {
|
||||
func NewTemplateService(loader Loader, funcs ...OptionFunc) *TemplateService {
|
||||
options := defaultOptions()
|
||||
for _, f := range funcs {
|
||||
f(options)
|
||||
}
|
||||
return &TemplateService{
|
||||
templates: make(map[string]*template.Template),
|
||||
pool: bpool.NewBufferPool(options.PoolSize),
|
||||
helpers: options.Helpers,
|
||||
blocks: make(map[string]string),
|
||||
layouts: make(map[string]*template.Template),
|
||||
pool: bpool.NewBufferPool(options.PoolSize),
|
||||
helpers: options.Helpers,
|
||||
loader: loader,
|
||||
devMode: options.DevMode,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user