wa

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package wa provides a simple web application framework.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidGroupName = errors.New("无效的组别名称")
	ErrInvalidUserCode  = errors.New("无效的用户代码")
	ErrInvalidUserName  = errors.New("无效的用户名称")
	ErrInvalidID        = ws.Errorf("无效的ID")
	ErrAuthFailed       = ws.Errorf("用户认证失败")
	ErrUserLocked       = ws.Errorf("用户已经锁定")
	ErrWeakPassword     = ws.Errorf("密码过于简单")
)

Errors.

Functions

func GenKey

func GenKey() ([]byte, error)

GenKey generates random key.

func GenToken

func GenToken(key []byte, password string) []byte

GenToken generates token by password.

func NewLogger

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

func Run(injector *di.Injector) (err error)

Run runs the web application.

Types

type AppBIZ

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

AppBIZ represents the app BIZ.

func NewAppBIZ

func NewAppBIZ(config *Config, sessionMGR *SessionMGR) *AppBIZ

NewAppBIZ creates a new app BIZ.

func (*AppBIZ) ChangePassword

func (a *AppBIZ) ChangePassword(session *Session, w http.ResponseWriter, oldPassword, newPassword string) error

ChangePassword changes the user password.

func (*AppBIZ) GetClaim

func (a *AppBIZ) GetClaim(session *Session, key string) (string, error)

GetClaim gets the claim.

func (*AppBIZ) GetClaims

func (a *AppBIZ) GetClaims(session *Session) ([]Item[string, string], error)

GetClaims gets the claims.

func (*AppBIZ) GetModules

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

GetModules returns the modules.

func (*AppBIZ) Login

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

Login login the web application.

func (*AppBIZ) Logout

func (a *AppBIZ) Logout(session *Session, w http.ResponseWriter) error

Logout logout the web application.

type ClaimDAO

type ClaimDAO interface {
	// Set sets the claim.
	Set(uid ID, key string, value string) error
	// Get gets the claim.
	Get(uid ID, key string) (string, error)
	// GetKeys gets the claim keys.
	GetKeys() ([]string, error)
	// Query queries the claims.
	Query(uid ID) ([]Item[string, string], error)
}

ClaimDAO represents the claim DAO.

type Config

type Config struct {
	Root string
	Home string
	DictValue
}

Config represents the config infomation.

func NewConfig

func NewConfig() (*Config, error)

NewConfig creates a new config.

type Date

type Date uint32

Date represents a date.

func NewDate

func NewDate(year, month, day int) Date

NewDate creates a new date.

func ParseDate

func ParseDate(s string) (Date, error)

ParseDate parses string to date.

func Today

func Today() Date

Today returns today's date.

func (Date) Day

func (a Date) Day() int

Day returns the day.

func (Date) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (Date) Month

func (a Date) Month() int

Month returns the month.

func (Date) String

func (a Date) String() string

String converts date to string.

func (Date) Time

func (a Date) Time() time.Time

Time converts date to local time.

func (*Date) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Date) Year

func (a Date) Year() int

Year returns the year.

type DateTime

type DateTime uint64

DateTime represents a datetime.

func NewDateTime

func NewDateTime(year, month, day, hour, minute, second int) DateTime

NewDateTime creates a new datetime.

func Now

func Now() DateTime

Now returns now datetime.

func ParseDateTime

func ParseDateTime(s string) (DateTime, error)

ParseDateTime parses string to datetime.

func (DateTime) Day

func (a DateTime) Day() int

Day returns the day.

func (DateTime) Hour

func (a DateTime) Hour() int

Hour returns the hour.

func (DateTime) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (DateTime) Minute

func (a DateTime) Minute() int

Minute returns the minute.

func (DateTime) Month

func (a DateTime) Month() int

Month returns the month.

func (DateTime) Second

func (a DateTime) Second() int

Second returns the second.

func (DateTime) String

func (a DateTime) String() string

String converts date to string.

func (DateTime) Time

func (a DateTime) Time() time.Time

Time converts date to local time.

func (*DateTime) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (DateTime) Year

func (a DateTime) Year() int

Year returns the year.

type DictValue

type DictValue map[string]any

DictValue represents a dict value for config.

func (DictValue) Bool

func (a DictValue) Bool(k string, xs ...bool) bool

Bool returns a bool by k.

func (DictValue) Dict

func (a DictValue) Dict(k string, xs ...DictValue) DictValue

Dict returns a dict by k.

func (DictValue) Float

func (a DictValue) Float(k string, xs ...float64) float64

Float returns a float by k.

func (DictValue) Int

func (a DictValue) Int(k string, xs ...int) int

Int returns a int by k.

func (DictValue) List

func (a DictValue) List(k string, xs ...ListValue) ListValue

List returns a list by k.

func (DictValue) String

func (a DictValue) String(k string, xs ...string) string

String returns a string by k.

type Group

type Group struct {
	GID  ID
	PID  ID
	Code string
	Name string
}

Group represents a group.

func (*Group) Check

func (a *Group) Check() error

Check checks if the group is valid.

type GroupDAO

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) (*Group, error)
	// Get gets the group by id.
	Get(gid ID) (*Group, error)
	// Find finds the group by code.
	Find(code string) (*Group, error)
	// Query queries the groups by keyword.
	Query(gid ID, keyword string) ([]*Group, error)
	// CheckGroup checks the group.
	CheckGroup(pid, gid ID) error
}

GroupDAO represents the group DAO.

type ID

type ID uint64

ID represents a ID.

func MakeID

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

MakeID makes ID from bytes.

func ParseID

func ParseID(s string) (ID, error)

ParseID parses string to ID.

func (ID) Bytes

func (a ID) Bytes() []byte

Bytes returns the ID 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 ID as string.

func (*ID) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

type Item

type Item[K comparable, V any] struct {
	Key   K
	Value V
}

Item represents a common value/text item model.

type ListValue

type ListValue []any

ListValue represents a list value for config.

func (ListValue) Bool

func (a ListValue) Bool(i int, xs ...bool) bool

Bool returns a bool by i.

func (ListValue) Dict

func (a ListValue) Dict(i int, xs ...DictValue) DictValue

Dict returns a dict by i.

func (ListValue) Float

func (a ListValue) Float(i int, xs ...float64) float64

Float returns a float by i.

func (ListValue) Int

func (a ListValue) Int(i int, xs ...int) int

Int returns a int by i.

func (ListValue) List

func (a ListValue) List(i int, xs ...ListValue) ListValue

List returns a list by i.

func (ListValue) String

func (a ListValue) String(i int, xs ...string) string

String returns a string by i.

type Module

type Module struct {
	Code string
	Name string
	Text string

	Load   func(*ws.Router) error `json:"-"`
	Unload func() error           `json:"-"`
}

Module represents a module.

type Session

type Session struct {
	UID ID
	// contains filtered or unexported fields
}

Session represents a session.

func NewSession

func NewSession(sessionMGR *SessionMGR, r *http.Request) (*Session, error)

NewSession creates a new session.

func (*Session) CheckGroup

func (a *Session) CheckGroup(gid ID, strict bool) error

CheckGroup checks the group.

func (*Session) GetClaim

func (a *Session) GetClaim(key string) (string, error)

GetClaim gets the claim.

func (*Session) GetClaims

func (a *Session) GetClaims() ([]Item[string, string], error)

GetClaims gets the claims.

func (*Session) GetUser

func (a *Session) GetUser() (*User, error)

GetUser returns the user.

func (*Session) HasClaim

func (a *Session) HasClaim(key string) (bool, error)

HasClaim returns if has the claim.

type SessionMGR

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

SessionMGR represents the session manager.

func NewSessionMGR

func NewSessionMGR(config *Config, groupDAO GroupDAO, userDAO UserDAO, claimDAO ClaimDAO) *SessionMGR

NewSessionMGR creates a new session manager.

func (*SessionMGR) Create

func (a *SessionMGR) Create(w http.ResponseWriter, uid ID, keep bool) error

Create creates a new session.

func (*SessionMGR) Delete

func (a *SessionMGR) Delete(w http.ResponseWriter, uid ID) error

Delete deletes the session.

type User

type User struct {
	UID    ID
	GID    ID
	Code   string
	Name   string
	Locked bool
}

User represents a user.

func (*User) Check

func (a *User) Check() error

Check checks if the user is valid.

type UserDAO

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) (*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, key string, value string, keyword string) ([]*User, error)
	// SetAuthToken sets the user auth token.
	SetAuthToken(uid ID, token []byte) error
	// GetAuthToken gets the user auth token.
	GetAuthToken(uid ID) ([]byte, error)
	// SetSessionToken sets the user session token.
	SetSessionToken(uid ID, token []byte) error
	// GetSessionToken gets the user session token.
	GetSessionToken(uid ID) ([]byte, 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