Initial commit
This commit is contained in:
43
service/container.go
Normal file
43
service/container.go
Normal file
@ -0,0 +1,43 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNotImplemented is the error when a service is accessed
|
||||
// and no implementation was provided
|
||||
ErrNotImplemented = errors.New("service not implemented")
|
||||
)
|
||||
|
||||
// Provider is a provider for a service
|
||||
type Provider func(ctn *Container) (interface{}, error)
|
||||
|
||||
// Container is a simple service container for dependency injection
|
||||
type Container struct {
|
||||
providers map[Name]Provider
|
||||
}
|
||||
|
||||
// Name is a name of a service
|
||||
type Name string
|
||||
|
||||
// Provide registers a provider for the survey.Service
|
||||
func (c *Container) Provide(name Name, provider Provider) {
|
||||
c.providers[name] = provider
|
||||
}
|
||||
|
||||
// Service retrieves a service implementation based on its name
|
||||
func (c *Container) Service(name Name) (interface{}, error) {
|
||||
provider, exists := c.providers[name]
|
||||
if !exists {
|
||||
return nil, ErrNotImplemented
|
||||
}
|
||||
return provider(c)
|
||||
}
|
||||
|
||||
// NewContainer returns a new empty service container
|
||||
func NewContainer() *Container {
|
||||
return &Container{
|
||||
providers: make(map[Name]Provider),
|
||||
}
|
||||
}
|
43
service/session/flash.go
Normal file
43
service/session/flash.go
Normal file
@ -0,0 +1,43 @@
|
||||
package session
|
||||
|
||||
const (
|
||||
// FlashError defines an "error" flash message
|
||||
FlashError FlashType = "error"
|
||||
// FlashWarn defines an "warning" flash message
|
||||
FlashWarn FlashType = "warn"
|
||||
// FlashSuccess defines an "success" flash message
|
||||
FlashSuccess FlashType = "success"
|
||||
// FlashInfo defines an "info" flash message
|
||||
FlashInfo FlashType = "info"
|
||||
)
|
||||
|
||||
// FlashType defines the type of a flash message
|
||||
type FlashType string
|
||||
|
||||
// Flash is a ephemeral message that lives in a session
|
||||
// until it's read
|
||||
type Flash interface {
|
||||
Type() FlashType
|
||||
Message() string
|
||||
}
|
||||
|
||||
// BaseFlash is a base implementation of a flash message
|
||||
type BaseFlash struct {
|
||||
flashType FlashType
|
||||
message string
|
||||
}
|
||||
|
||||
// Type returns the type of the flash
|
||||
func (f *BaseFlash) Type() FlashType {
|
||||
return f.flashType
|
||||
}
|
||||
|
||||
// Message returns the message of the flash
|
||||
func (f *BaseFlash) Message() string {
|
||||
return f.message
|
||||
}
|
||||
|
||||
// NewBaseFlash returns a new BaseFlash
|
||||
func NewBaseFlash(flashType FlashType, message string) *BaseFlash {
|
||||
return &BaseFlash{flashType, message}
|
||||
}
|
38
service/session/service.go
Normal file
38
service/session/service.go
Normal file
@ -0,0 +1,38 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/goweb/service"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ServiceName defines the Session service name
|
||||
const ServiceName service.Name = "session"
|
||||
|
||||
// Service defines the API of a "http session" service
|
||||
type Service interface {
|
||||
Get(http.ResponseWriter, *http.Request) (Session, error)
|
||||
}
|
||||
|
||||
// From retrieves the session service in the given container
|
||||
func From(container *service.Container) (Service, error) {
|
||||
service, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
sessionService, ok := service.(Service)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
return sessionService, nil
|
||||
}
|
||||
|
||||
// Must retrieves the session service in the given container or panic otherwise
|
||||
func Must(container *service.Container) Service {
|
||||
service, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return service
|
||||
}
|
16
service/session/session.go
Normal file
16
service/session/session.go
Normal file
@ -0,0 +1,16 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Session defines the API of a Session
|
||||
type Session interface {
|
||||
Set(string, interface{})
|
||||
Unset(string)
|
||||
Get(string) interface{}
|
||||
AddFlash(flashType FlashType, message string)
|
||||
Flashes(flashTypes ...FlashType) []Flash
|
||||
Save(http.ResponseWriter, *http.Request) error
|
||||
Delete(http.ResponseWriter, *http.Request) error
|
||||
}
|
19
service/template/data.go
Normal file
19
service/template/data.go
Normal file
@ -0,0 +1,19 @@
|
||||
package template
|
||||
|
||||
// Data is some data to inject into the template
|
||||
type Data map[string]interface{}
|
||||
|
||||
// DataExtFunc is some extensions to a template's data
|
||||
type DataExtFunc func(data Data) (Data, error)
|
||||
|
||||
// Extend returns a template's data with the given extensions
|
||||
func Extend(data Data, extensions ...DataExtFunc) (Data, error) {
|
||||
var err error
|
||||
for _, ext := range extensions {
|
||||
data, err = ext(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
38
service/template/service.go
Normal file
38
service/template/service.go
Normal file
@ -0,0 +1,38 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/goweb/service"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ServiceName defines the Tempate service name
|
||||
const ServiceName service.Name = "template"
|
||||
|
||||
// Service is a templating service
|
||||
type Service interface {
|
||||
RenderPage(w http.ResponseWriter, templateName string, data interface{}) error
|
||||
}
|
||||
|
||||
// From retrieves the template service in the given container
|
||||
func From(container *service.Container) (Service, error) {
|
||||
service, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
templateService, ok := service.(Service)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
return templateService, nil
|
||||
}
|
||||
|
||||
// Must retrieves the template service in the given container or panic otherwise
|
||||
func Must(container *service.Container) Service {
|
||||
service, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return service
|
||||
}
|
Reference in New Issue
Block a user