gosml

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: Apache-2.0 Imports: 22 Imported by: 0

README

gosml

gosml 做为 [sml GOLANG实现,保留了大部分工具类(Https,SqlTemplate,ManagedQueue,Cron,Scheduler,gdbc),可基于配置快速开发restful接口

Features

** SqlTemplate 快速方便的数据查询模板,模板配置支持存放数据库,或本地文件远程文件。

** Https 链式风格,对各类查询,参数[url参数,表头参数,body参数,文件流]等进行封装方便统一操作

** Cron cron表达示,实现95%以上,并且增加符合国人的书写习惯,如周数字表示等设定

** ManagedQueue 线程队列管理器,支持指定协程数,队列大小,运行任务监控统计、重新下发等

** Scheduler 调度器,支持cron表达款任务,秒级精确,轻松支持万级以上任务调度,支持周期调度,单任务调度,延时调度

** Gdbc 类JDBC操作,支持回调处理,提供常用查询api

Usage

Gdbc

快速使用 gddc

package main

import (
   _ "github.com/go-sql-driver/mysql"
   "github.com/huangwenjimmy/gosml"
   "fmt"
)


func main1(){
   //init sml sqltemplate
   stq:=gosml.Init(gosml.Ds{Id:"defJt",Url:"root:rooxxx@tcp(192.168.1.xxx:3306)/xxxx",DbType:"mysql"})
   stq.Cache(gosml.NewDefaultCacheManager()) //添加缓存
   result_map,_:=stq.Gdbcs["defJt"].QueryForMap("select 1 as a where 1=?",1)
   fmt.Println(result_map) //return map[string]interface{}
   result_list,_:=stq.Gdbcs["defJt"].QueryForList("select * from (select 1 as a union all select 2 as a) t where a=$1",2)
   fmt.Println(result_list)//result []map[string]interface{}
   stq.Gdbcs["defJt"].QueryForCall("select * from sml_if where limit ?",func(row map[string]interface{}) (bool,error){
   		//适用于导出等流式操作,返回bool error供上级判断
   		return true,nil
   	},10000)
   //sml template api支持配置sql参考java-api
   result_multi,_:=stq.Query("test-if",map[string]string{"id":"as","b":"hw","age":"30","time":"2020-04-22T10:06:04Z","dd":"1,2,3","times":"201904,201908"})
   fmt.Println(result_multi)//result    map list page
}
Cron

crontab表达示实现,支持秒级,下次执行时间,表达示内容

  • ss mi HH day-of-mon Mon day-of-w Year[可不选]
  • 0 0/5 * * * ? //从0点,每隔5分钟执行
  • 0 0,30 9-20 * * ? //每天9点到20点,0分和30分执行
  • 0 0 9 1 * ? //每月1号9点整执行
  • 0 40 23 L * ? //每月最后一天23:40分执行
  • 0 0 9 ? * MON //每周一9点整执行
  • 0 0 9 ? * FRIL //每月最后一个周期五9点整执行
  • 0 0 9 1 JUN-DEC * //7月至12月每1号9点整执行
package main

import (
	"github.com/huangwenjimmy/gosml"
	"github.com/huangwenjimmy/gosml/queue"
	"fmt"
)

func main(){
	cp:=queue.NewCronParser("0 1 9 * * ?")
	flag:=cp.Valid(gosml.ConvertStrToDate("20200428090100"))
	fmt.Println(flag) //true
	//trigger
	tri:=queue.NewTrigger("0 1 9 * * ?")
	fmt.Println(tri.GetNext())//当前时间下次执行时间
	fmt.Println(tri.GetNextDate(gosml.ConvertStrToDate("20200428090100")))//指定时间下次执行时间
}
ManagedQueue|Scheduler

调度器+线程管理器结合使用,高效流转各内部环节任务管理

package main

import (
	"github.com/huangwenjimmy/gosml/queue"
	"fmt"
	"time"
)

func main(){
	ds:=queue.NewScheduler("defaultScheduler",10,10000)//创建调度器指定10个协程,10000个队列深度
	ds.InitScheduler()//初始化
	//下发周期任务
	ds.SchedulerJob("3/5 * * * * ?",&DJ{"job1"})//添加结构体任务  implements  queue.Job method ToString|Run
	//下发方法任务
	ds.SchedulerFuncJob("0 0/5 * * * ?","funcJob1",func(){
		fmt.Println("funcJob1",time.Now())
	})
	//执行一次性任务
	//ds.ExecuteTask(task)//task implements queue.Task
	time.Sleep(time.Hour)
}
type DJ struct{
	name string
}
func (dj *DJ) ToString() string{
	return dj.name
}
func (dj *DJ) Run() error{
	fmt.Println(dj.name,time.Now())
	return nil
}
Https

支持常见操作

package main

import (
	"github.com/huangwenjimmy/gosml"
	"fmt"
	"time"
	"strings"
)

func main(){
	//添加表头  basic 认证   指定连接读写超时时间
	https:=gosml.NewGetHttps("https://localhost:16003/bus/sml/query/if-cfg-interMngLike").
	Header("content-type","application/json").
	BasicAuth("user:passwd").
	ConnectTimeout(time.Second).RWTimeout(time.Second).
	Param("ids","10001").Param("name","测试")
	https.Execute()
	//支持对返回结果进行多种操作,获取流,反序列化,下载等
	fmt.Println(https.GetBodyString())//https.GetBodyBytes()|https.GetBodyTo(writer)|https.GetBodyToFile(*File)
	//post-body请求,body支持   string []byte  io.reader
	https=gosml.NewPostBodyHttps("https://localhost:16003/bus/sml/query/if-cfg-interMngLike").Body(`{"ids":"1001","name":"测试"}`)
	https.Execute()
	fmt.Println(https.GetBodyString())
	//post-upload上传  需要指定Multipart  表单参数Param url参数请自行拼接至url  通过gosml.UpFile  可添加多个文件
	https=gosml.NewPostFormHttps("https://localhost:16003/bus/sml/web/file/upload").Multipart().
	Param("filepath","../logs").
	UpFile(&gosml.UpFile{FormName:"file",FileName:"hello1.txt",Input:strings.NewReader("helloworld234天啊\n我是上传的内容")})
	https.Execute()
	fmt.Println(https.Response.Status,https.GetBodyString())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainKey

func ContainKey(m map[string]interface{}, key string) bool

func ContainKeyCaseInsensitive

func ContainKeyCaseInsensitive(m map[string]interface{}, key string) bool

func Contains

func Contains(array []string, ele string) bool

func ConvertDbVals

func ConvertDbVals(tp string, bs []byte) interface{}

func ConvertStrToDate

func ConvertStrToDate(val string) time.Time

func ConvertStringToMap

func ConvertStringToMap(evalStr string) map[string]string

func ConvertStructureAppendMap

func ConvertStructureAppendMap(pointer interface{}, m map[string]interface{})

func ConvertStructureToMap

func ConvertStructureToMap(pointer interface{}) map[string]interface{}

func ConvertToFloat

func ConvertToFloat(i interface{}) float64

func ConvertToInt

func ConvertToInt(i interface{}) int64

func ConvertToString

func ConvertToString(i interface{}) string

func ConvertValueToRequestType

func ConvertValueToRequestType(val string, tp string) interface{}

func EnabledLog

func EnabledLog(enabled bool)

func FromJson

func FromJson(data []byte, v interface{}) error

func IsEmpty

func IsEmpty(i interface{}) bool

func IsNotEmpty

func IsNotEmpty(i interface{}) bool

func MapToStructure

func MapToStructure(m interface{}, rawVal interface{}) error

func OperatorEval

func OperatorEval(operator string, vc string, realV string) bool

func SubStr

func SubStr(source string, start int, end int) string

func ThrowRuntime

func ThrowRuntime(err error)

func ToJson

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

func ToJsonString

func ToJsonString(obj interface{}) string

Types

type Build

type Build struct {
	Name    string
	Options map[string]string
}

type CacheManager

type CacheManager interface {
	Get(k string) []byte
	Put(k string, v []byte, seconds int64)
	Remove(k string)
	Contain(k string) bool
	Clear() int
	ClearKeyStart(kre string) int
	GetKeyStart(kre string) map[string]interface{}
}

type DefaultCacheManager

type DefaultCacheManager struct {
	Data map[string][]byte
	Ttls map[string]int64
	Lock sync.RWMutex
}

func NewDefaultCacheManager

func NewDefaultCacheManager() *DefaultCacheManager

func (*DefaultCacheManager) Clear

func (this *DefaultCacheManager) Clear() int

func (*DefaultCacheManager) ClearKeyStart

func (this *DefaultCacheManager) ClearKeyStart(kre string) int

func (*DefaultCacheManager) Contain

func (this *DefaultCacheManager) Contain(k string) bool

func (*DefaultCacheManager) Get

func (this *DefaultCacheManager) Get(k string) []byte

func (*DefaultCacheManager) GetKeyStart

func (this *DefaultCacheManager) GetKeyStart(kre string) map[string]interface{}

func (*DefaultCacheManager) Put

func (this *DefaultCacheManager) Put(k string, bs []byte, seconds int64)

func (*DefaultCacheManager) Remove

func (this *DefaultCacheManager) Remove(k string)

type Ds

type Ds struct {
	Id      string
	Url     string
	DbType  string
	MaxConn int
	MaxIdle int
}

type EL

type EL interface {
	Eval(evalStr string, context map[string]interface{}) interface{}
}

type Gdbc

type Gdbc struct {
	Db      *sql.DB
	DbType  string
	Convert func(ty string, val []byte) interface{}
}

func (*Gdbc) QueryForCall

func (this *Gdbc) QueryForCall(sql string, hand func(map[string]interface{}) (bool, error), args ...interface{}) error

func (*Gdbc) QueryForList

func (this *Gdbc) QueryForList(sql string, args ...interface{}) ([]map[string]interface{}, error)

func (*Gdbc) QueryForMap

func (this *Gdbc) QueryForMap(sql string, args ...interface{}) (map[string]interface{}, error)

type Https

type Https struct {
	Response *http.Response
	// contains filtered or unexported fields
}

func NewGetHttps

func NewGetHttps(urlStr string) *Https

func NewPostBodyHttps

func NewPostBodyHttps(urlStr string) *Https

func NewPostFormHttps

func NewPostFormHttps(urlStr string) *Https

func (*Https) Auth

func (https *Https) Auth(authType string, credentials string) *Https

func (*Https) BasicAuth

func (https *Https) BasicAuth(credentials string) *Https

func (*Https) Body

func (https *Https) Body(body interface{}) *Https

string []byte io.reader support

func (*Https) ConnectTimeout

func (https *Https) ConnectTimeout(connectTimeout time.Duration) *Https

func (*Https) Execute

func (https *Https) Execute() error

func (*Https) Form

func (https *Https) Form() *Https

func (*Https) GetBodyBytes

func (https *Https) GetBodyBytes() []byte

func (*Https) GetBodyString

func (https *Https) GetBodyString() string

func (*Https) GetBodyTo

func (https *Https) GetBodyTo(writer io.Writer) int64

func (*Https) GetBodyTo1

func (https *Https) GetBodyTo1()

func (*Https) GetBodyToFile

func (https *Https) GetBodyToFile(file *os.File)

file not close

func (*Https) Header

func (https *Https) Header(name string, value string) *Https

func (*Https) Json

func (https *Https) Json() *Https

func (*Https) Method

func (https *Https) Method(method string) *Https

func (*Https) Multipart

func (https *Https) Multipart() *Https

func (*Https) Param

func (https *Https) Param(name string, value string) *Https

func (*Https) RWTimeout

func (https *Https) RWTimeout(rwTimeout time.Duration) *Https

func (*Https) UpFile

func (https *Https) UpFile(upFiles ...*UpFile) *Https

type Pairs

type Pairs map[string]string

type Rst

type Rst struct {
	DbType string
	Sql    string
	Args   []interface{}
}

func ParserSql

func ParserSql(stp *SqlTemplate, gdbc *Gdbc) (*Rst, error)

type Sml

type Sml struct {
	Dss   map[string]*sql.DB
	Gdbcs map[string]*Gdbc
	Cm    CacheManager
}

func Init

func Init(dss ...Ds) *Sml

func (*Sml) Cache

func (this *Sml) Cache(cm CacheManager) *Sml

func (*Sml) GetSqlTemplate

func (this *Sml) GetSqlTemplate(ifId string) (*SqlTemplate, error)

func (*Sml) Query

func (this *Sml) Query(ifId string, params map[string]string) ([]map[string]interface{}, error)

type SmlEL

type SmlEL struct {
}

func (*SmlEL) Eval

func (this *SmlEL) Eval(evalStr string, context map[string]interface{}) bool

type SmlParam

type SmlParam struct {
	Name         string
	Value        interface{}
	Type         string
	DefaultValue string
	Descr        string
}

type SqlTemplate

type SqlTemplate struct {
	Id            string
	Mainsql       string
	ConditionInfo string
	RebuildInfo   string
	CacheMin      int
	Descr         string
	DbId          string
	UpdateTime    time.Time
	SmlParams     []*SmlParam
	Rebuild       *Build
}

func (*SqlTemplate) GetMap

func (this *SqlTemplate) GetMap() map[string]interface{}

func (*SqlTemplate) Init

func (this *SqlTemplate) Init()

func (*SqlTemplate) ReinitParam

func (this *SqlTemplate) ReinitParam(params map[string]string)

type UpFile

type UpFile struct {
	FormName string
	FileName string
	Input    io.Reader
}

type Urls

type Urls struct {
	Url      string
	Schema   string
	Host     string
	Username string
	Password string
	IsFile   bool
	Path     string
	PrePath  string
	LastPath string
	Query    string
	Port     int
	Params   map[string]string
}

func NewUrls

func NewUrls(url string) *Urls

Directories

Path Synopsis
io
net

Jump to

Keyboard shortcuts

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