Initial commit

This commit is contained in:
2018-12-06 15:18:05 +01:00
commit 2cb1bf1881
23 changed files with 827 additions and 0 deletions

43
service/session/flash.go Normal file
View 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}
}

View 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
}

View 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
}