wa

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2022 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package wa provides a simple web application framework.

Index

Constants

View Source
const (
	AdminID   ID     = 1
	AdminCode string = "admin"
)

Admin.

Variables

View Source
var (
	ErrInvalidBits = ws.Errorf("wa: invalid bits")
	ErrInvalidID   = ws.Errorf("wa: invalid id")
	ErrInvalidRole = ws.Errorf("wa: invalid role")
)

Errors.

Functions

func GenToken added in v0.1.2

func GenToken(uid ID, password string) []byte

GenToken generates user token.

func GroupPathRange added in v0.1.2

func GroupPathRange(path []byte, strict bool) (pmin, pmax []byte)

GroupPathRange returns the group range.

func IsMainGroup added in v0.1.2

func IsMainGroup(g *Group) bool

IsMainGroup is a main group filter.

func NewLogger added in v0.1.2

func NewLogger(config *Config) (*log.Logger, di.CleanUp, error)

NewLogger creates a new logger.

func Register

func Register(m *Module)

Register registers the module.

func Run added in v0.1.2

func Run(injector *di.Injector) error

Run runs the web application.

Types

type AppBIZ added in v0.1.2

type AppBIZ struct {
	// contains filtered or unexported fields
}

AppBIZ represents the app BIZ.

func NewAppBIZ added in v0.1.2

func NewAppBIZ(config *Config, moduleMGR *ModuleMGR, sessionMGR *SessionMGR, userDAO UserDAO) *AppBIZ

NewAppBIZ creates a new app BIZ.

func (*AppBIZ) ChangePassword added in v0.1.2

func (a *AppBIZ) ChangePassword(session Session, oldPassword, newPassword string) error

ChangePassword changes the user password.

func (*AppBIZ) GetModules added in v0.1.2

func (a *AppBIZ) GetModules(session Session) []*Module

GetModules returns the modules.

func (*AppBIZ) Login added in v0.1.2

func (a *AppBIZ) Login(w http.ResponseWriter, code string, password string) (*User, error)

Login login the web application.

func (*AppBIZ) Logout added in v0.1.2

func (a *AppBIZ) Logout(session Session)

Logout logout the web application.

type Bits added in v0.1.2

type Bits uint64

Bits represents a bits value.

func MakeBits added in v0.1.2

func MakeBits(b []byte) (Bits, error)

MakeBits makes bits value from bytes.

func ParseBits added in v0.1.2

func ParseBits(s string) (Bits, error)

ParseBits parses string to bits value.

func (Bits) Bytes added in v0.1.2

func (a Bits) Bytes() []byte

Bytes returns the value as bytes.

func (Bits) MarshalText added in v0.1.2

func (a Bits) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Bits) String added in v0.1.2

func (a Bits) String() string

String returns the value as string.

func (*Bits) UnmarshalText added in v0.1.2

func (a *Bits) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Config added in v0.1.2

type Config struct {
	Root              string
	Home              string
	Addr              string
	CertFile          string
	KeyFile           string
	SessionName       string
	ReadHeaderTimeout int
	ReadTimeout       int
	WriteTimeout      int
	IdleTimeout       int
	MaxHeaderBytes    int
	MaxBodyBytes      int64
	SessionTTL        int
	RoleCacheTTL      int
	UserLockTTL       int
	Modules           []string
	GroupExtra        []string
	UserExtra         []string
	ConfigExtra       map[string]any
}

Config represents the config infomation.

func NewConfig added in v0.1.2

func NewConfig() (*Config, error)

NewConfig creates a new config.

func (*Config) Bool added in v0.1.2

func (a *Config) Bool(key string) bool

Bool returns the extra bool value.

func (*Config) Float added in v0.1.2

func (a *Config) Float(key string) float64

Float returns the extra float value.

func (*Config) Int added in v0.1.2

func (a *Config) Int(key string) int

Int returns the extra int value.

func (*Config) String added in v0.1.2

func (a *Config) String(key string) string

String returns the extra string value.

func (*Config) Value added in v0.1.2

func (a *Config) Value(key string) any

Value returns the extra value.

type Group

type Group struct {
	GID   ID
	PID   ID
	Code  string
	Name  string
	Extra map[string]string
}

Group represents a group.

func (*Group) Check

func (a *Group) Check() error

Check checks if the group is valid.

type GroupDAO added in v0.1.2

type GroupDAO interface {
	// Create creates a new group.
	Create(group *Group) (*Group, error)
	// Delete deletes the group.
	Delete(gid ID) error
	// Update updates the group.
	Update(group *Group) error
	// Get gets the group by id.
	Get(gid ID) (*Group, error)
	// Find returns the group by filter.
	Find(gid ID, filter func(*Group) bool) (*Group, error)
	// Query queries the groups by keyword.
	Query(gid ID, keyword string) ([]*Group, error)
	// CheckGroup checks the group.
	CheckGroup(sgid, gid ID, strict bool, filter func(*Group) bool) error
}

GroupDAO represents the group DAO.

type ID

type ID Bits

ID represents a id.

func MakeID

func MakeID(b []byte) (ID, error)

MakeID makes id value from bytes.

func ParseID

func ParseID(s string) (ID, error)

ParseID parses string to id value.

func (ID) Bytes

func (a ID) Bytes() []byte

Bytes returns the value as bytes.

func (ID) MarshalText

func (a ID) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (ID) String

func (a ID) String() string

String returns the value as string.

func (*ID) UnmarshalText

func (a *ID) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Item added in v0.1.2

type Item[T any] struct {
	Value T
	Text  string
}

Item represents a common value/text item model.

type Module

type Module struct {
	Code  string
	Name  string
	Text  string
	Roles []Item[int] `json:",omitempty"`

	Load   func(*di.Injector, *ws.Router) error `json:"-"`
	Unload func() error                         `json:"-"`
	// contains filtered or unexported fields
}

Module represents a module.

func (*Module) CheckGroup added in v0.1.2

func (a *Module) CheckGroup(session Session, gid ID, strict bool, filter func(*Group) bool) error

CheckGroup checks the group.

func (*Module) CheckRoles added in v0.1.2

func (a *Module) CheckRoles(session Session, roles ...Role) (err error)

CheckRoles checks the roles.

func (*Module) DeleteRoleCache added in v0.1.2

func (a *Module) DeleteRoleCache(uid ID)

DeleteRoleCache deletes the role cache by user id.

type ModuleMGR added in v0.1.2

type ModuleMGR struct {
	// contains filtered or unexported fields
}

ModuleMGR represents the module manager.

func NewModuleMGR added in v0.1.2

func NewModuleMGR(config *Config) (mgr *ModuleMGR, err error)

NewModuleMGR creates a new module manager.

func (*ModuleMGR) GetModule added in v0.1.2

func (a *ModuleMGR) GetModule(code string) *Module

GetModule returns the module by code.

func (*ModuleMGR) GetModules added in v0.1.2

func (a *ModuleMGR) GetModules() []*Module

GetModules returns the modules.

type Role

type Role Bits

Role represents a role.

func MakeRole

func MakeRole(b []byte) (Role, error)

MakeRole makes role value from bytes.

func ParseRole

func ParseRole(s string) (Role, error)

ParseRole parses string to role value.

func (Role) Bytes

func (a Role) Bytes() []byte

Bytes returns the value as bytes.

func (Role) MarshalText

func (a Role) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Role) String

func (a Role) String() string

String returns the value as string.

func (*Role) UnmarshalText

func (a *Role) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type RoleDAO added in v0.1.2

type RoleDAO interface {
	// Get gets the role by user id.
	Get(mcode string, uid ID) (Role, error)
	// Set sets the role by user id.
	Set(mcode string, uid ID, role Role) error
	// Delete deletes the user roles.
	Delete(uid ID) error
}

RoleDAO represents the role DAO.

type Session added in v0.1.2

type Session struct {
	UID ID
	GID ID
}

Session represents a session.

func NewSession added in v0.1.2

func NewSession(sessionMGR *SessionMGR, w http.ResponseWriter, r *http.Request) (session Session, err error)

NewSession creates a new session.

type SessionMGR added in v0.1.2

type SessionMGR struct {
	// contains filtered or unexported fields
}

SessionMGR represents the session manager.

func NewSessionMGR added in v0.1.2

func NewSessionMGR(config *Config, userDAO UserDAO) *SessionMGR

NewSessionMGR creates a new session manager.

func (*SessionMGR) Create added in v0.1.2

func (a *SessionMGR) Create(session Session, w http.ResponseWriter) error

Create creates a new session.

func (*SessionMGR) Delete added in v0.1.2

func (a *SessionMGR) Delete(session Session)

Delete deletes the session.

type User

type User struct {
	UID   ID
	GID   ID
	Code  string
	Name  string
	Extra map[string]string
}

User represents a user.

func (*User) Check

func (a *User) Check() error

Check checks if the user is valid.

type UserDAO added in v0.1.2

type UserDAO interface {
	// Create creates a new user.
	Create(user *User) (*User, error)
	// Delete deletes the user.
	Delete(uid ID) error
	// Update updates the user.
	Update(user *User) error
	// Get gets the user by id.
	Get(uid ID) (*User, error)
	// Find finds the user by code.
	Find(code string) (*User, error)
	// Query queries the users by keyword.
	Query(gid ID, keyword string) ([]*User, error)
	// GetToken gets the user token by id.
	GetToken(uid ID) ([]byte, error)
	// UpdateToken updates the user token.
	UpdateToken(uid ID, token []byte) error
	// UpdateActiveTime updates the user active time.
	UpdateActiveTime(uid ID) error
}

UserDAO represents the user DAO.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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