hotime

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

README

hotime

golang web服务框架 支持数据库db:mysql、sqlite3 支持缓存cache:redis,memory,数据库 自带工具类,上下文,以及session等功能

Documentation

Index

Constants

View Source
const HEAD_SESSION_ADD = "session#"

HEAD_SESSION_ADD session储存头

Variables

View Source
var App = map[string]*Application{} //整个项目
View Source
var Config = Map{
	"mode": 3,
	"codeConfig": Map{
		"admin": "config/app.json",
	},
	"codeConfig1": Map{
		"admin": Map{
			"package": "admin",
			"config":  "config/app.json",
			"rule":    "config/rule.json",
		},
	},
	"db": Map{
		"sqlite": Map{
			"path": "config/data.db",
		},
	},
	"cache": Map{
		"memory": Map{
			"timeout": 60 * 60 * 2,
			"db":      true,
			"session": true,
		},
	},
	"error": Map{
		"1": "内部系统异常",
		"2": "访问权限异常",
		"3": "请求参数异常",
		"4": "数据处理异常",
		"5": "数据结果异常",
	},
	"tpt":         "tpt",
	"defFile":     []string{"index.html", "index.htm"},
	"port":        "80",
	"sessionName": "HOTIME",
}
View Source
var ConfigNote = Map{
	"logLevel":          "默认0,必须,0关闭,1打印,日志等级",
	"logFile":           "无默认,非必须,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置",
	"webConnectLogShow": "默认true,非必须,访问日志如果需要web访问链接、访问ip、访问时间打印,false为关闭true开启此功能",
	"webConnectLogFile": "无默认,非必须,webConnectLogShow开启之后才能使用,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置",
	"mode":              "默认0,非必须,0生产模式,1,测试模式,2开发模式,3内嵌代码模式,在开发模式下会显示更多的数据用于开发测试,并能够辅助研发,自动生成配置文件、代码等功能,web无缓存,数据库不启用缓存",
	"codeConfig": Map{
		"注释": "配置即启用,非必须,默认无",

		"packageName": "默认无,必须,包名称以及应用名,生成代码的配置文件地址,比如config/app.json,数据库有更新时自动更新配置文件以及对应的生成文件",
	},
	"db": Map{
		"注释": "配置即启用,非必须,默认使用sqlite数据库",
		"mysql": Map{
			"注释":       "除prefix及主从数据库slave项,其他全部必须",
			"host":     "默认127.0.0.1,必须,数据库ip地址",
			"name":     "默认test,必须,数据库名称",
			"user":     "默认root,必须,数据库用户名",
			"password": "默认root,必须,数据库密码",
			"port":     "默认3306,必须,数据库端口",
			"prefix":   "默认空,非必须,数据表前缀",
			"slave": Map{
				"注释":       "从数据库配置,mysql里配置slave项即启用主从读写,减少数据库压力",
				"host":     "默认127.0.0.1,必须,数据库ip地址",
				"name":     "默认test,必须,数据库名称",
				"user":     "默认root,必须,数据库用户名",
				"password": "默认root,必须,数据库密码",
				"port":     "默认3306,必须,数据库端口",
			},
		},
		"sqlite": Map{
			"path": "默认config/data.db,必须,数据库位置",
		},
	},
	"cache": Map{
		"注释": "可配置memory,db,redis,默认启用memory,默认优先级为memory>redis>db,memory与数据库缓存设置项一致,缓存数据填充会自动反方向反哺,加入memory缓存过期将自动从redis更新,但memory永远不会更新redis,如果是集群建议不要开启memory,配置即启用",
		"memory": Map{
			"timeout": "默认60 * 60 * 2,非必须,过期时间,超时自动删除",
			"db":      "默认true,非必须,缓存数据库,启用后能减少数据库的读写压力",
			"session": "默认true,非必须,缓存web session,同时缓存session保持的用户缓存",
		},
		"db": Map{
			"timeout": "默认60 * 60 * 24 * 30,非必须,过期时间,超时自动删除",
			"db":      "默认false,非必须,缓存数据库,启用后能减少数据库的读写压力",
			"session": "默认true,非必须,缓存web session,同时缓存session保持的用户缓存",
		},
		"redis": Map{
			"host":     "默认服务ip:127.0.0.1,必须,如果需要使用redis服务时配置,",
			"port":     "默认服务端口:6379,必须,如果需要使用redis服务时配置,",
			"password": "默认密码空,必须,如果需要使用redis服务时配置,默认密码空",
			"timeout":  "默认60 * 60 * 24 * 15,非必须,过期时间,超时自动删除",
			"db":       "默认true,非必须,缓存数据库,启用后能减少数据库的读写压力",
			"session":  "默认true,非必须,缓存web session,同时缓存session保持的用户缓存",
		},
	},
	"error": Map{
		"1":  "内部系统异常,在环境配置,文件访问权限等基础运行环境条件不足造成严重错误时使用",
		"2":  "访问权限异常,没有登录或者登录异常等时候使用",
		"3":  "请求参数异常,request参数不满足要求,比如参数不足,参数类型错误,参数不满足要求等时候使用",
		"4":  "数据处理异常,数据库操作或者三方请求返回的结果非正常结果,比如数据库突然中断等时候使用",
		"5":  "数据结果异常,一般用于无法给出response要求的格式要求下使用,比如response需要的是string格式但你只能提供int数据时",
		"注释": "web服务内置错误提示,自定义异常建议10开始",
	},
	"tpt":              "默认tpt,必须,web静态文件目录,默认为程序目录下tpt目录",
	"defFile":          "默认访问index.html或者index.htm文件,必须,默认访问文件类型",
	"crossDomain":      "默认空 非必须,空字符串为不开启,如果需要跨域设置,auto为智能开启所有网站允许跨域,http://www.baidu.com为指定域允许跨域",
	"modeRouterStrict": "默认false,必须,路由严格模式false,为大小写忽略必须匹配,true必须大小写匹配",
	"sessionName":      "默认HOTIME,必须,设置session的cookie名",
	"port":             "默认80,必须,web服务开启Http端口,0为不启用http服务,默认80",
	"tlsPort":          "默认空,非必须,web服务https端口,0为不启用https服务",
	"tlsKey":           "默认空,非必须,https密钥",
	"tlsCert":          "默认空,非必须,https证书",
}
View Source
var IsRun = false //当前状态
View Source
var MimeMaps = map[string]string{}/* 539 elements not displayed */
View Source
var TptProject = Proj{

	"hotimeCommon": Ctr{
		"info": func(that *Context) {
			hotimeName := that.RouterString[0]
			data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_id").ToCeilInt()})
			str, inData := that.MakeCode.Info(that.RouterString[1], data, that.Db)
			where := Map{"id": that.RouterString[2]}

			if len(inData) == 1 {
				inData["id"] = where["id"]
				where = Map{"AND": inData}
			} else if len(inData) > 1 {
				where["OR"] = inData
				where = Map{"AND": where}
			}

			re := that.Db.Get(that.RouterString[1], str, where)

			if re == nil {
				that.Display(4, "找不到对应信息")
				return
			}

			for k, v := range re {
				column := that.MakeCode.TableColumns[that.RouterString[1]][k]
				if column == nil {
					continue
				}
				if (column["list"] == nil || column.GetBool("list")) && column.GetString("link") != "" {
					re[column.GetString("link")] = that.Db.Get(column.GetString("link"), "id,"+column.GetString("value"), Map{"id": v})
				}
			}

			that.Display(0, re)
		},
		"add": func(that *Context) {
			inData := that.MakeCode.Add(that.RouterString[1], that.Req)
			if inData == nil {
				that.Display(3, "请求参数不足")
				return
			}

			re := that.Db.Insert(that.RouterString[1], inData)

			if re == 0 {
				that.Display(4, "无法插入对应数据")
				return
			}

			if inData.Get("parent_id") != nil && inData.GetString("index") != "" {
				index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")})
				inData["index"] = index.GetString("index") + ObjToStr(re) + ","
				that.Db.Update(that.RouterString[1], Map{"index": inData["index"]}, Map{"id": re})
			} else if inData.GetString("index") != "" {
				inData["index"] = "," + ObjToStr(re) + ","
				that.Db.Update(that.RouterString[1], Map{"index": inData["index"]}, Map{"id": re})
			}

			that.Display(0, re)
		},
		"update": func(that *Context) {
			inData := that.MakeCode.Edit(that.RouterString[1], that.Req)
			if inData == nil {
				that.Display(3, "没有找到要更新的数据")
				return
			}

			if inData.GetString("index") != "" {
				if inData.Get("parent_id") != nil {
					Index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": that.RouterString[2]})
					parentIndex := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")})
					inData["index"] = parentIndex.GetString("index") + that.RouterString[2] + ","

					childNodes := that.Db.Select(that.RouterString[1], "id,`index``", Map{"index[~]": "," + that.RouterString[2] + ","})

					for _, v := range childNodes {
						v["index"] = strings.Replace(v.GetString("index"), Index.GetString("index"), inData.GetString("index"), -1)
						that.Db.Update(that.RouterString[1], Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")})
					}
				} else {
					delete(inData, "index")
				}

			}

			re := that.Db.Update(that.RouterString[1], inData, Map{"id": that.RouterString[2]})

			if re == 0 {
				that.Display(4, "更新数据失败")
				return
			}

			that.Display(0, re)
		},
		"remove": func(that *Context) {
			inData := that.MakeCode.Delete(that.RouterString[1], that.Req)
			if inData == nil {
				that.Display(3, "请求参数不足")
				return
			}
			re := int64(0)

			if inData.Get("parent_id") != nil && inData.GetSlice("index") != nil {
				re = that.Db.Delete(that.RouterString[1], Map{"index[~]": "," + that.RouterString[2] + ","})
			} else {
				re = that.Db.Delete(that.RouterString[1], Map{"id": that.RouterString[2]})
			}

			if re == 0 {
				that.Display(4, "删除数据失败")
				return
			}
			that.Display(0, "删除成功")
		},

		"search": func(that *Context) {
			hotimeName := that.RouterString[0]
			data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_id").ToCeilInt()})

			columnStr, leftJoin, where := that.MakeCode.Search(that.RouterString[1], data, that.Req, that.Db)

			page := ObjToInt(that.Req.FormValue("page"))
			pageSize := ObjToInt(that.Req.FormValue("pageSize"))

			if page < 1 {
				page = 1
			}

			if pageSize <= 0 {
				pageSize = 20
			}

			count := that.Db.Count(that.RouterString[1], leftJoin, where)
			reData := that.Db.Page(page, pageSize).
				PageSelect(that.RouterString[1], leftJoin, columnStr, where)

			for _, v := range reData {
				for k, _ := range v {
					column := that.MakeCode.TableColumns[that.RouterString[1]][k]
					if column == nil {
						continue
					}

					if column["list"] != false && column["name"] == "parent_id" && column.GetString("link") != "" {
						parentC := that.Db.Get(column.GetString("link"), column.GetString("value"), Map{"id": v.GetCeilInt(k)})
						v[column.GetString("link")+"_"+column.GetString("name")+"_"+column.GetString("value")] = ""
						if parentC != nil {
							v[column.GetString("link")+"_"+column.GetString("name")+"_"+column.GetString("value")] = parentC.GetString(column.GetString("value"))
						}
					}

				}
			}

			that.Display(0, Map{"count": count, "data": reData})
		},
	},
	"hotime": Ctr{
		"login": func(this *Context) {
			hotimeName := this.RouterString[0]
			name := this.Req.FormValue("name")
			password := this.Req.FormValue("password")
			if name == "" || password == "" {
				this.Display(3, "参数不足")
				return
			}
			user := this.Db.Get(hotimeName, "*", Map{"AND": Map{"OR": Map{"name": name, "phone": name}, "password": Md5(password)}})
			if user == nil {
				this.Display(5, "登录失败")
				return
			}
			this.Session(hotimeName+"_id", user.GetCeilInt("id"))
			this.Session(hotimeName+"_name", name)
			delete(user, "password")
			this.Display(0, user)
		},
		"logout": func(this *Context) {
			hotimeName := this.RouterString[0]
			this.Session(hotimeName+"_id", nil)
			this.Session(hotimeName+"_name", nil)
			this.Display(0, "退出登录成功")
		},
		"info": func(that *Context) {
			hotimeName := that.RouterString[0]
			data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_id").ToCeilInt()})
			str, inData := that.MakeCode.Info(hotimeName, data, that.Db)
			where := Map{"id": that.Session(hotimeName + "_id").ToCeilInt()}
			if len(inData) == 1 {
				inData["id"] = where["id"]
				where = Map{"AND": inData}
			} else if len(inData) > 1 {
				where["OR"] = inData
				where = Map{"AND": where}
			}
			re := that.Db.Get(hotimeName, str, where)
			if re == nil {
				that.Display(4, "找不到对应信息")
				return
			}
			for k, v := range re {
				column := that.MakeCode.TableColumns[hotimeName][k]
				if column == nil {
					continue
				}
				if (column["list"] == nil || column.GetBool("list")) && column.GetString("link") != "" {

					if column.GetString("link") == "role" {

						re[column.GetString("link")] = that.Db.Get(column.GetString("link"), "id,auth,"+column.GetString("value"), Map{"id": v})

					} else {

						re[column.GetString("link")] = that.Db.Get(column.GetString("link"), "id,"+column.GetString("value"), Map{"id": v})
					}
				}
			}

			that.Display(0, re)
		},
	},
}

Project 管理端项目

Functions

func SetDB

func SetDB(appIns *Application)

SetDB 智能数据库设置

func SetMysqlDB

func SetMysqlDB(appIns *Application, config Map)

func SetSqliteDB

func SetSqliteDB(appIns *Application, config Map)

Types

type Application

type Application struct {
	*code.MakeCode
	MakeCodeRouter Router
	MethodRouter
	Router
	ContextBase
	Error
	Log           *logrus.Logger
	WebConnectLog *logrus.Logger
	Port          string //端口号
	TLSPort       string //ssl访问端口号

	Config Map
	Db     HoTimeDB
	*HoTimeCache
	*http.Server
	http.Handler
	// contains filtered or unexported fields
}

func Init

func Init(config string) Application

Init 初始化application

func (*Application) Run

func (that *Application) Run(router Router)

Run 启动实例

func (*Application) ServeHTTP

func (that *Application) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Application) SetCache

func (that *Application) SetCache()

SetCache 设置配置文件路径全路径或者相对路径

func (*Application) SetConfig

func (that *Application) SetConfig(configPath ...string)

SetConfig 设置配置文件路径全路径或者相对路径

func (*Application) SetConnectDB

func (that *Application) SetConnectDB(connect func(err ...*Error) (master, slave *sql.DB))

SetConnectDB 启动实例

func (*Application) SetConnectListener

func (that *Application) SetConnectListener(lis func(this *Context) bool)

SetConnectListener 连接判断,返回true继续传输至控制层,false则停止传输

func (*Application) SetDefault

func (that *Application) SetDefault(connect func(err ...*Error) (*sql.DB, *sql.DB))

SetDefault 默认配置缓存和session实现

type Context

type Context struct {
	*Application
	Resp         http.ResponseWriter
	Req          *http.Request
	RouterString []string
	Config       Map
	Db           *HoTimeDB
	RespData     Map
	CacheIns
	SessionIns
	DataSize   int
	HandlerStr string //复写请求url
}

func (*Context) Display

func (that *Context) Display(statu int, data interface{})

打印

func (*Context) Mtd

func (that *Context) Mtd(router [3]string) Map

Mtd 唯一标志

func (*Context) View

func (that *Context) View()

type Ctr

type Ctr map[string]Method

Ctr 控制器

type Method

type Method func(this *Context)

type MethodRouter

type MethodRouter map[string]Method //直接字符串关联函数

type Proj

type Proj map[string]Ctr

type Router

type Router map[string]Proj

type SessionIns

type SessionIns struct {
	*HoTimeCache
	SessionId string
	Map
	ContextBase
}

session对象

func (*SessionIns) Init

func (that *SessionIns) Init(cache *HoTimeCache)

func (*SessionIns) Session

func (that *SessionIns) Session(key string, data ...interface{}) *Obj

Directories

Path Synopsis
dri
rsa

Jump to

Keyboard shortcuts

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