Documentation
¶
Overview ¶
Session storage
Index ¶
- Variables
- func IsEmptyText(text string) bool
- func NewLineStrings(text ...string) string
- func RegisterRouter(name string, router UssdRouter)
- type BufferedResponse
- func (r *BufferedResponse) Printf(format string, args ...any)
- func (r *BufferedResponse) RenderContinueTemplate(name string, values TemplateValues)
- func (r *BufferedResponse) RenderEndTemplate(name string, values TemplateValues)
- func (r *BufferedResponse) RenderTemplate(name string, values TemplateValues, end bool)
- func (r *BufferedResponse) Write(p []byte) (int, error)
- type Engine
- type Logger
- type MenuOption
- type RouteHandler
- type RouterOption
- type RouterState
- type Storage
- type TemplateValues
- type UssdRequest
- type UssdRouter
- type UssdSession
Constants ¶
This section is empty.
Variables ¶
View Source
var ( WithSessionTimes = func(probeFrequency, timeToEviction time.Duration) RouterOption { return func(r *Engine) error { r.storageFrequency = probeFrequency r.storageEviction = timeToEviction return nil } } DebugMode = func(r *Engine) error { r.Debug = true switch v := r.Log.(type) { case *defaultLogger: v.shutup = false } return nil } WithRouter = func(routerName string) RouterOption { return func(r *Engine) error { if instance, ok := registry.Load(routerName); ok { r.router = instance.(UssdRouter) } else { return ErrRouterNotFound } return nil } } WithTemplateFS = func(fsys fs.FS, root string, funcs template.FuncMap) RouterOption { return func(r *Engine) error { err := fs.WalkDir(fsys, root, func(filepath string, d fs.DirEntry, err error) error { if err != nil { return err } ext := path.Ext(strings.ToLower(d.Name())) if !d.IsDir() && ext == ".tmpl" && len(d.Name()) >= 6 { fd, err := fsys.Open(path.Join(root, filepath)) if err != nil { return err } defer fd.Close() b, err := io.ReadAll(fd) if err != nil { return err } templateName := strings.TrimPrefix(path.Join(root, filepath), root) tmpl, err := template.New(d.Name()).Funcs(funcs).Parse(string(b)) if err != nil { return err } r.templateMap[templateName] = tmpl } return nil }) return err } } )
View Source
var (
ErrRouterNotFound = fmt.Errorf("router not found")
)
Functions ¶
func IsEmptyText ¶
func NewLineStrings ¶
func RegisterRouter ¶
func RegisterRouter(name string, router UssdRouter)
Registers a router. Must be called in package `init`
Types ¶
type BufferedResponse ¶
type BufferedResponse struct {
// contains filtered or unexported fields
}
func (*BufferedResponse) Printf ¶
func (r *BufferedResponse) Printf(format string, args ...any)
func (*BufferedResponse) RenderContinueTemplate ¶
func (r *BufferedResponse) RenderContinueTemplate(name string, values TemplateValues)
func (*BufferedResponse) RenderEndTemplate ¶
func (r *BufferedResponse) RenderEndTemplate(name string, values TemplateValues)
func (*BufferedResponse) RenderTemplate ¶
func (r *BufferedResponse) RenderTemplate(name string, values TemplateValues, end bool)
type Engine ¶
type Engine struct { Log Logger Debug bool NotFound RouteHandler Storage Storage // contains filtered or unexported fields }
The main routing engine (Layer 0 router)
func NewRouterEngine ¶
func NewRouterEngine(options ...RouterOption) (*Engine, error)
func (*Engine) MenuOptions ¶
func (e *Engine) MenuOptions(opts ...*MenuOption)
func (*Engine) RouteFromHttpRequest ¶
func (e *Engine) RouteFromHttpRequest(w http.ResponseWriter, req *http.Request)
type MenuOption ¶
type MenuOption struct {
// contains filtered or unexported fields
}
func NewMenuOption ¶
func NewMenuOption(code string, h RouteHandler, name string, sub ...*MenuOption) *MenuOption
type RouteHandler ¶
type RouteHandler func(request UssdRequest) bool
USSD Request handler. Return true to remain in the same screen context or false to indicate to the routing engine to advance the context.
A screen context confines menu options to a set, allowing the same option values to be used without conflicts.
type RouterOption ¶
type RouterState ¶
type RouterState int
const ( // Read value as input data. // // This can be used to handle custom routing inside a route(d) handler. // // To enter this mode, use any of the UssdRequest.PromptXXX() functions. READ_INPUT RouterState = iota // Read value as option to for routing. // // This is the default mode where the routing engine uses the value read // to match route handlers. // // Use any of the UssdRequest.ContinueXXX() functions to enter this mode. READ_OPTION )
type Storage ¶
type Storage interface { Set(key string, session UssdSession) Get(key string) UssdSession Del(key string) // Vacuum removes sessions that are as old as the given duration Vacuum(duration time.Duration) }
type TemplateValues ¶
type UssdRequest ¶
type UssdRequest interface { // MSISDN MSISDN returns the mobile subscriber identification number // assigned to the user by their network. MSISDN() string // Option Option returns the value entererd by the user after calling any // of the .Continue functions. // // The value is intepreted as an option by the routing engine, which is // used to match handlers. There is almost no need for handlers to use // this value. Option() string // Input Input returns the value entered by the user after calling any of // the .Prompt functions. The value is passed as is to the handler. Input() string // returns the session associated with this request Session() UssdSession // Continue This function causes the next input to be treated as an option // that will be handled by the routing engine to match a handler Continue(text string, args ...any) // Prompt This function causes the next input to be treated as input data, // which can be obtained using the .Input() function. // // The original option is also maintained and can be obtained by calling // the .Option() function. Prompt(text string, args ...any) // PromptWithTemplate Prompt using content from a template. // // Refer to UssdRequest.Prompt() function for more PromptWithTemplate(tmplName string, values TemplateValues) // ContinueWithTemplate Continue using content from a template. // // Refer to UssdRequest.Continue() function for more ContinueWithTemplate(tmplName string, values TemplateValues) // End Ends the session End(text string, args ...any) EndWithTemplate(tmplName string, values TemplateValues) // SetAttribute Set a request attribute. // // Request attributes are only valid for the duration of the request. // To store data for the duration of the session, use UssdRequest.Session() function // to use the session storage. SetAttribute(key string, value any) // GetAttribute Get request attribute GetAttribute(key string) any }
UssdRequest UssdRequest interface to access various states and data from the USSD request session.
type UssdRouter ¶
type UssdRouter interface { // Creates parses the incoming http request and creates a USSD request from it. // // # Session Management // // The router is responsible for providing and attaching a stage. The storage parameter // can be used to store or retrieve the session. // // # Returning responses // // resp *BufferedResponse // should be used to buffer output from the handlers, through the UssdRequest interface. // // The routing engine utilizes this to create a final response back to the client. CreateRequest(resp *BufferedResponse, req *http.Request, storage Storage) (UssdRequest, error) }
Responsible for creating USSD requests
Click to show internal directories.
Click to hide internal directories.