gobase

package module
v0.0.0-...-45758da Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2016 License: Apache-2.0 Imports: 29 Imported by: 0

README

gobase

Documentation

Overview

Copyright 2015 gophper.

Licensed under the Apache License, Version 2.0 (the "License"): you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	LevelCritical = iota
	LevelError
	LevelWarning
	LevelInfo
	LevelDebug
	LevelTrace
)

Log levels to control the logging output.

Variables

View Source
var (
	CacheTable             = NewCache()
	ErrTimeOut       error = errors.New("The cache has been timeout.")
	ErrKeyNotFound   error = errors.New("The key was not found.")
	ErrTypeAssertion error = errors.New("Type assertion error.")
)
View Source
var (
	// 日志
	Log *BaseLog
	//配置文件
	Config     *GoConfig
	SigHandler = make(map[string]interface{})
)

Functions

func CreatePid

func CreatePid()

create pid file

func DelFile

func DelFile(files []os.FileInfo, count int, fileDir string)

获取所有文件 如果文件达到最上限,按时间删除

func FileGetContent

func FileGetContent(file string) (string, error)

get string from text file

func FileMTime

func FileMTime(file string) (int64, error)

get file modified time

func FilePutContent

func FilePutContent(file string, content string) (int, error)

put string to file

func FileSize

func FileSize(file string) (int64, error)

get file size as how many bytes

func GetInt64Str

func GetInt64Str(d int64) string

func GetIntStr

func GetIntStr(d int) string

func GetIpMask

func GetIpMask(start, end string) string

func GetStrFloat64

func GetStrFloat64(s string) float64

func GetStrInt

func GetStrInt(d string) int

func GetStrInt64

func GetStrInt64(d string) int64

func GetStrUint

func GetStrUint(d string) uint

func GetStrUint32

func GetStrUint32(d string) uint32

*

  • 将字符串转换成 uint32 位数字

func GetStrUint64

func GetStrUint64(d string) uint64

*

  • 将字符串转换成 uint64 位数字

func GetTimeAgo

func GetTimeAgo(t int64) (s string)

func GetUint64Str

func GetUint64Str(d uint64) string

func GetUintStr

func GetUintStr(d uint) string

func Help

func Help()

func Ip2long

func Ip2long(ipstr string) (ip uint32)

func IsExist

func IsExist(path string) bool

IsExist returns whether a file or directory exists.

func IsFile

func IsFile(file string) bool

it returns false when it's a directory or does not exist.

func IsPrivateIP

func IsPrivateIP(ip string) bool

判断IP地址是否为预留ip

func Long2ip

func Long2ip(ip uint32) string

func Md5

func Md5(key string) string

func Mkdir

func Mkdir(src string) error

create dir

func NewXorm

func NewXorm(opt *OrmOption) (orm *xorm.Engine, err error)

func Remove

func Remove(file string) error

delete file

func RemoveAll

func RemoveAll(path string) error

RemoveAll删除path指定的文件,或目录及它包含的任何下级对象。它会尝试删除所有东西,除非遇到错误并返回。 如果path指定的对象不存在,RemoveAll会返回nil而不返回错误。

func Rename

func Rename(file string, to string) error

rename file name

func SignalHandle

func SignalHandle(funcs map[string]interface{})

func Version

func Version() string

Types

type BaseLog

type BaseLog struct {
	Loger    *log.Logger
	LogFile  string
	LogLevel int
	LogType  string
}

func NewLog

func NewLog(opt *LogOptions) (l *BaseLog)

func (*BaseLog) Critical

func (l *BaseLog) Critical(v ...interface{})

func (*BaseLog) Criticalf

func (l *BaseLog) Criticalf(format string, v ...interface{})

func (*BaseLog) Debug

func (l *BaseLog) Debug(v ...interface{})

func (*BaseLog) Debugf

func (l *BaseLog) Debugf(format string, v ...interface{})

func (*BaseLog) Error

func (l *BaseLog) Error(v ...interface{})

func (*BaseLog) Errorf

func (l *BaseLog) Errorf(format string, v ...interface{})

func (*BaseLog) Info

func (l *BaseLog) Info(v ...interface{})

func (*BaseLog) Infof

func (l *BaseLog) Infof(format string, v ...interface{})

func (*BaseLog) Panic

func (l *BaseLog) Panic(v ...interface{})

func (*BaseLog) Panicf

func (l *BaseLog) Panicf(format string, v ...interface{})

func (*BaseLog) Printf

func (l *BaseLog) Printf(format string, v ...interface{})

func (*BaseLog) Println

func (l *BaseLog) Println(v ...interface{})

func (*BaseLog) Trace

func (l *BaseLog) Trace(v ...interface{})

func (*BaseLog) Tracef

func (l *BaseLog) Tracef(format string, v ...interface{})

func (*BaseLog) Warn

func (l *BaseLog) Warn(v ...interface{})

func (*BaseLog) Warnf

func (l *BaseLog) Warnf(format string, v ...interface{})

type Cache

type Cache struct {
	Lock *sync.RWMutex
	Len  uint64
	Item map[interface{}]*Object
}

func NewCache

func NewCache() (c *Cache)

新建一个cache

func (*Cache) Cleanup

func (c *Cache) Cleanup(f func(interface{}) bool)

清除cache 参数是一个函数 传递外部函数判断cache里数据,决定是否要删除

func (*Cache) Del

func (c *Cache) Del(key interface{})

删除cache

func (*Cache) Exists

func (c *Cache) Exists(key interface{}) bool

检查cache是否存在

func (*Cache) Get

func (c *Cache) Get(key interface{}) (interface{}, error)

// 从cache里获取

func (*Cache) Set

func (c *Cache) Set(key, val interface{}, timeout time.Duration) bool

设置cache

type FileRepos

type FileRepos []Repository

func (FileRepos) Len

func (r FileRepos) Len() int

func (FileRepos) Less

func (r FileRepos) Less(i, j int) bool

func (FileRepos) Swap

func (r FileRepos) Swap(i, j int)

type GoConfig

type GoConfig struct {
	ConfFile  string
	Version   string
	PathLevel int
	Item      map[string]interface{}
}

func LoadConfig

func LoadConfig(configFile string) (cfg *GoConfig)

func NewConfig

func NewConfig(file string, level int) (*GoConfig, error)

func (*GoConfig) Array

func (c *GoConfig) Array(key string) ([]interface{}, error)

func (*GoConfig) Bool

func (c *GoConfig) Bool(key string, def bool) (bool, error)

func (*GoConfig) Float64

func (c *GoConfig) Float64(key string, def float64) (float64, error)

func (*GoConfig) Int

func (c *GoConfig) Int(key string, def int) (int, error)

func (*GoConfig) Int64

func (c *GoConfig) Int64(key string, def int64) (int64, error)

func (*GoConfig) Map

func (c *GoConfig) Map(key string) (map[string]interface{}, error)

func (*GoConfig) ReloadConfig

func (c *GoConfig) ReloadConfig() error

func (*GoConfig) String

func (c *GoConfig) String(key string, def string) (string, error)

type IpNetmask

type IpNetmask struct {
	Ipaddr    string //IP地址
	Mask      string //IP掩码
	Subnet    string //网络地址
	Broadcast string //广播地址
	IpStart   string //起始IP
	IpEnd     string //结束IP
}

func GetIpAddrs

func GetIpAddrs(ipaddr, mask string, ok bool) (ipNetmask *IpNetmask, ipRangeAddrs []string)

type LogOptions

type LogOptions struct {
	Type   string // log type: consloe, file, system
	File   string // log file, need type is file
	Level  int    // output log level
	Flag   int    // log flag
	Enable bool
}

type MailMessage

type MailMessage struct {
	From    mail.Address // if From.Address is empty, Config.DefaultFrom will be used
	To      []mail.Address
	Cc      []mail.Address
	Bcc     []mail.Address
	Subject string
	Content string
	S       *Smtp
}

func (*MailMessage) Send

func (self *MailMessage) Send() error

func (*MailMessage) Validate

func (self *MailMessage) Validate() error

Returns the first error

type Mailer

type Mailer struct {
	Subject     string
	Content     string
	To, Cc, Bcc string
}

type ModelOption

type ModelOption struct {
	Orm   OrmOption
	Redis RedisOption
}

type Models

type Models struct {
	Orm          *xorm.Engine
	OrmCache     bool
	OrmCacheTime int64
	Redis        *Redis
	RedisEnable  bool
	RedisPrefix  string
	Driver       string
}
var Dbh *Models

func NewDefaultModels

func NewDefaultModels() (dbh *Models, err error)

func NewModels

func NewModels(opt *ModelOption) (*Models, error)

func (*Models) Close

func (dbh *Models) Close() error

type Object

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

type OrmOption

type OrmOption struct {
	Driver    string
	Proto     string
	Host      string
	User      string
	Password  string
	Name      string
	Ssl       string
	Path      string
	Port      int
	CacheTime int
	Cache     bool
	Debug     bool
}

type Redis

type Redis struct {
	redis.Client
	RedisPrefix string
}

func NewRedis

func NewRedis(opt *RedisOption) (*Redis, error)

func (*Redis) GetRedisKey

func (self *Redis) GetRedisKey(keys ...interface{}) (key string)

func (*Redis) RedisDelete

func (self *Redis) RedisDelete(keys ...interface{}) (bool, error)

func (*Redis) RedisDeleteAll

func (self *Redis) RedisDeleteAll(keys ...interface{}) (s bool, err error)

func (*Redis) RedisExists

func (self *Redis) RedisExists(keys ...interface{}) (bool, error)

func (*Redis) RedisGet

func (self *Redis) RedisGet(keys ...interface{}) (value []byte, err error)

func (*Redis) RedisGetJson

func (self *Redis) RedisGetJson(v interface{}, keys ...interface{}) (err error)

func (*Redis) RedisIncr

func (self *Redis) RedisIncr(keys ...interface{}) (int64, error)

func (*Redis) RedisSet

func (self *Redis) RedisSet(expire int64, value interface{}, keys ...interface{}) (err error)

func (*Redis) RedisSetJson

func (self *Redis) RedisSetJson(expire int64, mp interface{}, keys ...interface{}) (err error)

type RedisOption

type RedisOption struct {
	Host   string
	Auth   string
	Prefix string
	Port   int
	Db     int
	Enable bool
}

type Repository

type Repository struct {
	Name     string
	FileTime int64
}

type Smtp

type Smtp struct {
	SmtpUserName                     string
	SmtpHost, SmtpUser, SmtpPassword string
	SmtpPort                         int
	SmtpAuth, SmtpTLS, SmtpDaemon    bool
	// contains filtered or unexported fields
}

SMTP setup

func NewSmtp

func NewSmtp() *Smtp

func (*Smtp) MailSendServer

func (s *Smtp) MailSendServer()

运行一个goroutine 监听发送邮件任务

func (*Smtp) MailSender

func (s *Smtp) MailSender(subject, content, to, cc, bcc string) (err error)

func (*Smtp) NewMailMessage

func (s *Smtp) NewMailMessage(m *Mailer) *MailMessage

Jump to

Keyboard shortcuts

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