db

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: Apache-2.0 Imports: 17 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ApiKeyLength = 48
)

Variables

View Source
var (
	Client  *mongo.Client
	Domains *mongo.Collection
	Users   *mongo.Collection
)

Functions

func Connect

func Connect() error

Connect connects to the database.

func Disconnect

func Disconnect()

Disconnect gracefully disconnect from the database.

func GetStat

func GetStat() (d int64, s int64, err error)

GetStat resturns the total number of domains (d), the total number of subdomains (s) and the error (if any).

func Init

func Init() error

Init creates the indexes in domains and users collection and creates an "admin" user if no admin privileged user is exist.

func Insert

func Insert(d string) error

Insert insert the given domain d to the database. Firstly, checks if d is valid. Then split into sub|domain parts. Sharding means, if the document is reached the 16MB limit increase the "shard" field by one.

If domain is invalid, returns fault.ErrInvalidDomain.

If d is publicsuffix, returns fault.ErrPublicSuffix.

func IsNameTaken

func IsNameTaken(name string) (bool, error)

IsNameTaken check whether the given user name is already taken.

Returns fault.ErrNameEmpty if name is empty.

func Lookup

func Lookup(d string, full bool) ([]string, error)

Lookup query the DB and returns a list subdomains. If full is true, return the full hostname, not just the subs.

func Update

func Update() error

Update updates the domains if new publixsuffix is added. used at the begining to update the domains if new publicsuffix rule is added to the list. If the stored domain is not valid after the new rules, create a list of full names, rewrite it with Write() where the new rules will apply, and removes it.

func UserChangeAdmin

func UserChangeAdmin(u *user.User, newValue bool) error

UserChangeAdmin update the admin field for the given user, and change the admin value in user. If user nil, returns fault.ErrUserNil.

If key/name is empty, returns fault.ErrMissingAPIKey/fault.ErrNameEmpty.

If username is taken, returns fault.ErrNameTaken.

func UserChangeKey

func UserChangeKey(u *user.User) error

UserChangeKey update the API key for the given user, and change the key in user.

If user nil, returns fault.ErrUserNil.

If u.Key/u.Name is empty, returns fault.ErrMissingAPIKey/fault.ErrNameEmpty.

If document not modified returns fault.ErrNotModified.

func UserChangeName

func UserChangeName(u *user.User, name string) error

UserChangeName update the name for the given user, and change the name in user.

If user nil, returns fault.ErrUserNil.

If u.Key/u.Name is empty, returns fault.ErrMissingAPIKey/fault.ErrNameEmpty.

If name is taken, returns fault.ErrNameTaken.

func UserCreate

func UserCreate(name string, admin bool) (user.User, error)

UserCreate creates a new user in the users collection and returns it. The API key automatically generated. If admin is true, the new user will be an admin.

If user name is already taken returns fault.ErrNameTaken error.

If name is empty returns fault.ErrNameEmpty.

func UserDelete

func UserDelete(key, name string) error

UserDelete delete user based on key+name.

If user not found, returns fault.ErrNameEmpty error.

If key is empty, returns fault.ErrMissingAPIKey error.

If DeleteOne() deletes 0 user, returns fault.ErrUserNotDeleted.

func UserGetKey

func UserGetKey(key string) (user.User, error)

UserGetKey returns the user from the db based on API key.

If user not found, returns fault.ErrUserNotFound error.

If key is empty, returns fault.ErrMissingAPIKey error.

func UserGetName

func UserGetName(name string) (user.User, error)

UserGetName returns the user from the db based on name.

If user not found, returns fault.ErrUserNotFound error.

If name is empty, returns fault.ErrNameEmpty error.

func UserList

func UserList() ([]user.User, error)

UserList returns a list of every users.

Types

This section is empty.

Jump to

Keyboard shortcuts

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