Documentation
¶
Index ¶
- Variables
- func AddRuntimeRoutes(app *ship.Ship, config ...RuntimeRouteConfig)
- func ConvertOptsToCliFlags(groups ...string) []cli.Flag
- func DisableBuiltinPrometheusCollector()
- func GetDefaultGateway() (gateway net.IP, err error)
- func GetDefaultIP() (ip string, err error)
- func InInts(v int, vs []int) bool
- func InSlice(v interface{}, vs []interface{}) bool
- func InStrings(s string, ss []string) bool
- func InUints(v uint, vs []uint) bool
- func Init(appName string, configOptions ...interface{})
- func InitApp(appName, version string, options ...interface{})
- func InitApp2(appName, version, logfilesize string, logfilenum int, options ...interface{})
- func InitConfig(app string, options interface{}, version ...string)
- func InitLogging(level, filepath string)
- func InitLogging2(level, filepath, filesize string, filenum int)
- func InitRouter() *ship.Ship
- func LoadCliSource(ctx *cli.Context, groups ...string)
- func LogPanic(name ...string)
- func ReverseStrings(ss []string)
- func StringsEqual(s1, s2 []string) bool
- func Unix(sec, nsec int64) time.Time
- type DB
- type DBPool
- func (p *DBPool) AddDB(index int, writer DB, reader ...DB)
- func (p *DBPool) AddReaderDB(index int, reader DB)
- func (p *DBPool) GetAllDBs() []DBWrapper
- func (p *DBPool) GetDB(key string) DB
- func (p *DBPool) GetReaderDB(key string) DB
- func (p *DBPool) GetWriterDB(key string) DB
- func (p *DBPool) SetIndexer(index func(key string) int)
- type DBWrapper
- type Handler
- type Middleware
- type Route
- type Routes
- type RuntimeRouteConfig
- type ShellConfig
Constants ¶
This section is empty.
Variables ¶
var ( Ef = klog.Ef Tracef = klog.Tracef Debugf = klog.Debugf Infof = klog.Infof Warnf = klog.Warnf Errorf = klog.Errorf Printf = klog.Printf Panicf = klog.Panicf Fatalf = klog.Fatalf RegisterCallOnExit = klog.RegisterCallOnExit )
Predefine some log functions.
Please refer to https://godoc.org/github.com/xgfone/klog
var App = InitRouter()
App is the default global router app.
var DatabaseOpts = []gconf.Opt{ gconf.StrOpt("connection", "The URL connection to the alarm database, user:password@tcp(127.0.0.1:3306)/db").C(false), gconf.IntOpt("maxconnnum", "The maximum number of the connections.").C(false).D(100), }
DatabaseOpts collects the options of the SQL database.
var DefaultRuntimeRouteConfig = RuntimeRouteConfig{ShellConfig: DefaultShellConfig}
DefaultRuntimeRouteConfig is the default RuntimeRouteConfig with DefaultShellConfig.
var DefaultShellConfig = ShellConfig{Shell: "bash", Timeout: time.Minute}
DefaultShellConfig is the default ShellConfig.
var ErrRouteTableNotImplemented = errors.New("route table has not implemented")
ErrRouteTableNotImplemented is returned when the route table has not implemented.
var HistogramBuckets = []float64{.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 1.5, 2}
HistogramBuckets is used to replace the default Histogram buckets.
var LogOpts = []gconf.Opt{
gconf.StrOpt("logfile", "The file path of the log. The default is stdout.").As("log_file"),
gconf.StrOpt("loglevel", "The level of the log, such as debug, info").D("info").As("log_level"),
}
LogOpts collects the options about the log.
Functions ¶
func AddRuntimeRoutes ¶
func AddRuntimeRoutes(app *ship.Ship, config ...RuntimeRouteConfig)
AddRuntimeRoutes adds the runtime routes.
func ConvertOptsToCliFlags ¶ added in v0.9.0
func ConvertOptsToCliFlags(groups ...string) []cli.Flag
ConvertOptsToCliFlags the config group to []cli.Flag. For example,
ConvertOptsToCliFlags() // Convert the options in the DEFAULT group ConvertOptsToCliFlags("group1") // Convert the options in the group named "group1" ConvertOptsToCliFlags("group1.group2") // Convert the options in the group named "group1.group2" ConvertOptsToCliFlags("group1", "group2") // The same as the last.
func DisableBuiltinPrometheusCollector ¶
func DisableBuiltinPrometheusCollector()
DisableBuiltinPrometheusCollector removes the collectors that the default prometheus register registers
func GetDefaultGateway ¶ added in v0.4.0
GetDefaultGateway returns the default gateway.
func GetDefaultIP ¶ added in v0.4.0
GetDefaultIP returns the default ip.
func InSlice ¶ added in v0.7.0
func InSlice(v interface{}, vs []interface{}) bool
InSlice reports whether v is in vs.
func Init ¶ added in v0.7.0
func Init(appName string, configOptions ...interface{})
Init is equal to InitApp(appName, gover.Text(), configOptions...).
func InitApp ¶
func InitApp(appName, version string, options ...interface{})
InitApp initializes the application, which is equal to
InitApp2(appName, version, "100M", 100, options...)
func InitApp2 ¶ added in v0.4.0
InitApp2 initializes the application.
- Register the log options.
- Initialize configuration.
- Initialize the logging.
func InitConfig ¶
InitConfig initliazlies the configuration options.
options may be gconf.Opt, []gconf.Opt, a pointer to the struct variable, or the list of the pointers to the struct variables. For example,
InitConfig("", gconf.StrOpt("optname", "HELP TEXT")) InitConfig("appname", []gconf.Opt{gconf.StrOpt("optname", "HELP TEXT")}) InitConfig("", []interface{}{structPtr1, structPtr2}, "1.0.0") InitConfig("", structPtr, "1.0.0")
func InitLogging ¶
func InitLogging(level, filepath string)
InitLogging is equal to InitLogging2(level, filepath, "100M", 100).
func InitLogging2 ¶ added in v0.2.0
InitLogging2 initializes the logging.
If filepath is empty, it will use Stdout as the writer.
func LoadCliSource ¶ added in v0.9.0
func LoadCliSource(ctx *cli.Context, groups ...string)
LoadCliSource loads the config into the groups from the CLI source.
func ReverseStrings ¶ added in v0.9.0
func ReverseStrings(ss []string)
ReverseStrings reverses the elements in the strings slice.
func StringsEqual ¶ added in v0.9.0
StringsEqual reports whether the two strings is equal.
Types ¶
type DB ¶
type DB interface { Begin() (*sql.Tx, error) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) Close() error Conn(ctx context.Context) (*sql.Conn, error) Driver() driver.Driver Exec(query string, args ...interface{}) (sql.Result, error) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) Ping() error PingContext(ctx context.Context) error Prepare(query string) (*sql.Stmt, error) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) Query(query string, args ...interface{}) (*sql.Rows, error) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) QueryRow(query string, args ...interface{}) *sql.Row QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row SetConnMaxLifetime(d time.Duration) SetMaxIdleConns(n int) SetMaxOpenConns(n int) Stats() sql.DBStats }
DB is an interface to stands for the general sql.DB.
type DBPool ¶
type DBPool struct {
// contains filtered or unexported fields
}
DBPool is a read-write DB pool.
func (*DBPool) AddDB ¶
AddDB adds a db into the pool.
If reader is nil, the reader DB is the same as writer by default.
Noitce: the index is only used to sort and identify whether two DBs are equal.
func (*DBPool) AddReaderDB ¶
AddReaderDB adds a reader db into the pool.
Noitce: the index is only used to sort and identify whether two DBs are equal.
func (*DBPool) GetReaderDB ¶
GetReaderDB returns the reader DB by the key.
If no reader, return the writer.
func (*DBPool) SetIndexer ¶
SetIndexer sets the indexer to get the corresponding DB by the key, that's, it will convert the same key to a constant index forever.
The default indexer only converts the key ending with any character of "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
type Handler ¶
type Handler = ship.Handler
Handler is the type alias of ship.Handler.
func ExecuteShell ¶
func ExecuteShell(handle func(ctx *ship.Context, stdout, stderr []byte, err error) error, config ...ShellConfig) Handler
ExecuteShell returns a handler to execute a SHELL command or script.
The request body is the command to be executed as JSON like this:
{ "cmd": "BASE64_COMMAND", // Optional "script": "BASE64_SCRIPT_FILE_CONTENT", // Optional "shell": "SHELL_COMMAND", // Optional "timeout": "10s" // Optional }
handle is used to handle the result of the command or script. If nil, it will use the default that returns a JSON as the response body like this,
{ "stdout": "BASE64_STD_OUTPUT", "stderr": "BASE64_STD_ERR_OUTPUT", "error": "failure reason. If successfully, it is empty." }
Notice:
- The executed command or script must be encoded by base64.
- If shell is given, it will override the Shell in ShellConfig.
- If timeout is given, it will override the Timeout in ShellConfig.
The returned handler is very dangerous, and should not be called by the non-trusted callers.
func PrometheusHandler ¶
func PrometheusHandler(gatherer ...prometheus.Gatherer) Handler
PrometheusHandler returns a prometheus handler.
if missing gatherer, it is prometheus.DefaultGatherer.
type Middleware ¶
type Middleware = ship.Middleware
Middleware is the type alias of ship.Middleware.
func Prometheus ¶
func Prometheus(namespaceAndSubsystem ...string) Middleware
Prometheus returns a middleware to handle the prometheus metrics.
The first argument is the namespace, and the second is the subsystem. Both of them are optional.
type Routes ¶ added in v0.4.0
type Routes []Route
Routes is a set of Routes.
func GetIPv4Routes ¶ added in v0.4.0
GetIPv4Routes returns the ipv4 routes.
func GetIPv6Routes ¶ added in v0.4.0
GetIPv6Routes returns the ipv6 routes.
func (Routes) DefaultGateway ¶ added in v0.4.0
DefaultGateway returns the default gateway. Return nil if not exist.
type RuntimeRouteConfig ¶
type RuntimeRouteConfig struct { ShellConfig Prefix string IsReady func() bool IsHealthy func() bool }
RuntimeRouteConfig is used to configure the runtime routes.
type ShellConfig ¶
type ShellConfig struct { Shell string // The shell name or path, which is "sh" by default. Timeout time.Duration // The timeout to execute the shell command. }
ShellConfig is used to configure the shell execution.