helper

package
v0.0.0-...-c7c8920 Latest Latest
Warning

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

Go to latest
Published: May 17, 2020 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyDirectory

func CopyDirectory(sourceDirName, destDirName string) error

CopyDirectory copy the contents in one directory to another.

func CopyFile

func CopyFile(sourcePath, destPath string) error

CopyFile copy one file from source path to dest path.

func Download

func Download(w http.ResponseWriter, path, name string)

Download write a file stream to the webbrowser.

func EnableCrossDomain

func EnableCrossDomain(w http.ResponseWriter, r *http.Request)

EnableCrossDomain enables cross domain. It set the `Access-Control-Allow-Methods` header and the `Access-Control-Allow-Origin` header.

TODO: We should restrict the origin, and may config in `config.toml`.

func FromJSON

func FromJSON(bytes []byte, result interface{}) error

FromJSON convert json bytes to interface{}.

func Get

func Get(url string) ([]byte, error)

Get create a get request to the server.

func MD5

func MD5(str string) string

MD5 convert a string to md5 encrypt string.

func PinYinToString

func PinYinToString(obj interface{}) string

PinYinToString convert pinyin (string or array) to string.

TODO: Just for compatible, and will removed in the future.

func Post

func Post(url string, data url.Values) ([]byte, error)

Post create a post request to the server.

func TimeToString

func TimeToString(time time.Time, format string) string

TimeToString convert a time to string using specific format.

`yyyy-MM-dd HH:mm:ss` => 2020-05-05 20:35:12

func ToJSON

func ToJSON(obj interface{}) ([]byte, error)

ToJSON convert interface{} to json bytes.

func UnZip

func UnZip(filename, path string) error

UnZip unzip filename to the path given.

func Write

func Write(w http.ResponseWriter, args ...interface{})

Write write a string response to the web client.

func WriteJSON

func WriteJSON(w http.ResponseWriter, obj interface{}) error

WriteJSON write a json response to the web client.

func Writef

func Writef(w http.ResponseWriter, format string, args ...interface{})

Writef write a string response to the web client with format string.

func Zip

func Zip(dir, path string) error

Zip create a zip file to the path.

Types

type AuthorityConfigModel

type AuthorityConfigModel struct {
	Enabled bool `toml:"enabled"`
	Expires int  `toml:"expires"`
}

AuthorityConfigModel is the authority config section in `config.toml`;

type ConfigModel

type ConfigModel struct {
	Server    ServerConfigModel    `toml:"server"`
	Database  DatabaseConfigModel  `toml:"database"`
	Authority AuthorityConfigModel `toml:"authority"`
	Upload    UploadConfigModel    `toml:"upload"`
	Remote    RemoteConfigModel    `toml:"remote"`
	Path      PathConfigModel      `toml:"path"`
	Log       LogConfigModel       `toml:"log"`
}

ConfigModel is the structure of file `config.toml`.

func GetConfig

func GetConfig(path string) (config *ConfigModel, err error)

GetConfig read `config.toml` and parse `ConfigModel`.

type DatabaseConfigModel

type DatabaseConfigModel struct {
	Type string `toml:"type"`
	Host string `toml:"host"`
	Port int    `toml:"port"`
	// Connection should not read from config.toml.
	Connection string
	Database   string `toml:"database"`
}

DatabaseConfigModel is the database config section in `config.toml`;

type LogConfigModel

type LogConfigModel struct {
	File string `toml:"file"`
}

LogConfigModel is the log config section in `config.toml`;

type Mongo

type Mongo struct {
	ConnectionString string
	DatabaseName     string
	Client           *mongo.Client
	Database         *mongo.Database
}

Mongo represent a mongo client.

func NewMongo

func NewMongo(connectionString, databaseName string) (*Mongo, error)

NewMongo create a mongo client using connectionString and databaseName.

func (Mongo) Count

func (m Mongo) Count(collectionName string, filter interface{}) (int64, error)

Count get documents count of a collection.

func (Mongo) DeleteAll

func (m Mongo) DeleteAll(collectionName string) (*mongo.DeleteResult, error)

DeleteAll delete all documents.

func (Mongo) DeleteMany

func (m Mongo) DeleteMany(collectionName string, filter interface{}) (*mongo.DeleteResult, error)

DeleteMany delete many documents.

func (Mongo) DeleteOne

func (m Mongo) DeleteOne(collectionName string, filter interface{}) (*mongo.DeleteResult, error)

DeleteOne delete one document.

func (Mongo) DropCollection

func (m Mongo) DropCollection(name string) error

DropCollection drop a collection.

func (Mongo) Find

func (m Mongo) Find(collectionName string, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)

Find find a cursor from a collection.

func (Mongo) FindAll

func (m Mongo) FindAll(collectionName string, results interface{}, opts ...*options.FindOptions) (err error)

FindAll find many documents of a collection.

func (Mongo) FindMany

func (m Mongo) FindMany(collectionName string, filter interface{}, results interface{}, opts ...*options.FindOptions) (err error)

FindMany find many documents of a collection.

func (Mongo) FindOne

func (m Mongo) FindOne(collectionName string, filter interface{}, result interface{}, opts ...*options.FindOneOptions) (find bool, err error)

FindOne find one document from a collection.

func (Mongo) GetCollection

func (m Mongo) GetCollection(name string) (collection *mongo.Collection, err error)

GetCollection get a collection from collectionName.

func (Mongo) InsertMany

func (m Mongo) InsertMany(collectionName string, documents []interface{}) (*mongo.InsertManyResult, error)

InsertMany insert many documents to a collection.

func (Mongo) InsertOne

func (m Mongo) InsertOne(collectionName string, document interface{}) (*mongo.InsertOneResult, error)

InsertOne insert one document to a collection.

func (Mongo) ListCollectionNames

func (m Mongo) ListCollectionNames() (collectionNames []string, err error)

ListCollectionNames list collectionNames of database.

func (Mongo) RunCommand

func (m Mongo) RunCommand(command interface{}) (result *mongo.SingleResult, err error)

RunCommand run a mongo command.

func (Mongo) UpdateMany

func (m Mongo) UpdateMany(collectionName string, filter interface{}, update interface{}) (*mongo.UpdateResult, error)

UpdateMany update many documents.

func (Mongo) UpdateOne

func (m Mongo) UpdateOne(collectionName string, filter interface{}, update interface{}) (*mongo.UpdateResult, error)

UpdateOne update one document.

type PathConfigModel

type PathConfigModel struct {
	PublicDir string `toml:"public_dir"`
	LogDir    string `toml:"log_dir"`
}

PathConfigModel is the authority path section in `config.toml`;

type PinYinModel

type PinYinModel struct {
	// TotalPinYin is the total pinyin.
	TotalPinYin string
	// FirstPinYin is the first letter of pinyin.
	FirstPinYin string
}

PinYinModel is the returned pinyin model.

func ConvertToPinYin

func ConvertToPinYin(text string) (model PinYinModel)

ConvertToPinYin convert Chinese character to Chinese pinyin which is convenient to search using keywords.

type RemoteConfigModel

type RemoteConfigModel struct {
	Enabled       bool `toml:"enabled"`
	WebSocketPort int  `toml:"web_socket_port"`
}

RemoteConfigModel is the remote config section in `config.toml`;

type ServerConfigModel

type ServerConfigModel struct {
	Port string `toml:"port"`
}

ServerConfigModel is the server config section in `config.toml`;

type UploadConfigModel

type UploadConfigModel struct {
	MaxSize int64 `toml:"max_size"`
}

UploadConfigModel is the upload config section in `config.toml`;

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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