diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 7e3eaa3..80baa39 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,32 +5,14 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
@@ -42,23 +24,11 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -66,11 +36,21 @@
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
@@ -86,23 +66,11 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
@@ -110,12 +78,24 @@
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
@@ -123,20 +103,30 @@
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
@@ -164,7 +154,6 @@
-
@@ -172,13 +161,15 @@
-
-
-
+
+
+
+
+
@@ -389,7 +380,10 @@
-
+
+
+
+
1500458878821
@@ -440,11 +434,18 @@
1501670291054
-
+
+ 1501834964768
+
+
+
+ 1501834964768
+
+
-
+
@@ -458,12 +459,12 @@
-
+
-
+
@@ -498,19 +499,13 @@
-
-
-
-
-
-
-
-
-
+
+
+
@@ -526,7 +521,7 @@
-
+
@@ -554,7 +549,7 @@
-
+
@@ -563,7 +558,6 @@
-
@@ -571,7 +565,9 @@
-
+
+
+
@@ -587,7 +583,7 @@
-
+
@@ -615,7 +611,7 @@
-
+
@@ -624,7 +620,6 @@
-
@@ -632,7 +627,9 @@
-
+
+
+
@@ -648,7 +645,7 @@
-
+
@@ -673,7 +670,7 @@
-
+
@@ -682,7 +679,9 @@
-
+
+
+
@@ -701,7 +700,7 @@
-
+
@@ -711,7 +710,7 @@
-
+
@@ -738,7 +737,9 @@
-
+
+
+
@@ -746,7 +747,6 @@
-
@@ -762,7 +762,7 @@
-
+
@@ -787,7 +787,7 @@
-
+
@@ -796,7 +796,9 @@
-
+
+
+
@@ -811,21 +813,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -835,30 +822,20 @@
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
@@ -871,51 +848,87 @@
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/app.go b/app.go
index 4a51ef9..35d79d1 100644
--- a/app.go
+++ b/app.go
@@ -15,7 +15,7 @@ import (
type App struct {
Router
Error
- Port int //端口号
+ Port string //端口号
connectListener func(context Context) bool //所有的访问监听,true按原计划继续使用,false表示有监听器处理
connectDbFunc func(err ...*Error) *sql.DB
configPath string
@@ -27,9 +27,10 @@ type App struct {
}
//启动实例
-func (this *App) Run(router Router, port int) {
+func (this *App) Run(router Router) {
this.Router = router
- this.Port = port
+ //this.Port = port
+ this.Port=this.Config.GetString("port")
if this.connectDbFunc != nil {
this.Db.SetConnect(this.connectDbFunc)
@@ -39,9 +40,9 @@ func (this *App) Run(router Router, port int) {
this.CacheIns = CacheIns(&CacheMemory{Map: Map{}, Time: this.Config.GetInt64("cacheShortTime")})
}
- http.HandleFunc("/", this.handler)
+ //http.HandleFunc("/", this.handler)
+ run(this)
- http.ListenAndServe(":"+ObjToStr(port), nil)
}
//启动实例
diff --git a/context.go b/context.go
index 6dc0523..cabc085 100644
--- a/context.go
+++ b/context.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"time"
+ "errors"
)
type Context struct {
@@ -43,7 +44,13 @@ func (this *Context) Display(statu int, data interface{}) {
resp := Map{"statu": statu}
if statu != 0 {
temp := Map{}
- temp["type"] = 0
+
+ tpe:=this.Config.GetMap("error").GetString(ObjToStr(statu))
+ if tpe==""{
+ this.LastErr.SetError(errors.New("找不到对应的错误码"))
+ }
+
+ temp["type"] = tpe
temp["msg"] = data
resp["result"] = temp
} else {
diff --git a/func.go b/func.go
index 3788609..555f3df 100644
--- a/func.go
+++ b/func.go
@@ -1,13 +1,12 @@
package hotime
import (
- //"reflect"
"crypto/md5"
"encoding/hex"
"math"
"math/rand"
"strings"
- //"fmt"
+ "net/http"
"sync"
"time"
)
@@ -184,6 +183,7 @@ func RandX(small int, max int) int {
// GetDb()
//}
+
//复制返回数组
func DeepCopyMap(value interface{}) interface{} {
if valueMap, ok := value.(Map); ok {
@@ -300,4 +300,20 @@ func Round(f float64, n int) float64 {
// CacheMemIns.Init(Config["cacheConfig"].(Map)["memory"].(CacheConfg).Time)
// CacheDBIns.Init(Config["cacheConfig"].(Map)["db"].(CacheConfg).Time)
//}
+func run(a *App) {
+ if !IsRun {
+ http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
+ port := Substr(req.Host, IndexLastStr(req.Host, ":")+1, len(req.Host))
+ //fmt.Println(port)
+ if application[port] != nil {
+ application[port].handler(w, req)
+ }
+ })
+ IsRun = true
+ }
+
+ application[a.Port] = a
+ http.ListenAndServe(":"+a.Port, nil)
+
+}
diff --git a/obj.go b/obj.go
index 35f8468..56ef7f1 100644
--- a/obj.go
+++ b/obj.go
@@ -1,5 +1,6 @@
package hotime
+//对象封装方便取用
type Obj struct {
Data interface{}
Error
diff --git a/session.go b/session.go
index 6952e31..23ed0cf 100644
--- a/session.go
+++ b/session.go
@@ -1,75 +1,71 @@
package hotime
-
+//session对象
type SessionIns struct {
ShortCache CacheIns
- LongCache CacheIns
- SessionId string
+ LongCache CacheIns
+ SessionId string
Map
Error
}
-func (this *SessionIns)set(){
-
- if this.ShortCache!=nil{
- this.ShortCache.Cache(SESSION_TYPE+this.SessionId,this.Map)
- }
- if this.LongCache!=nil{
- this.LongCache.Cache(SESSION_TYPE+this.SessionId,this.Map)
- }
+func (this *SessionIns) set() {
+ if this.ShortCache != nil {
+ this.ShortCache.Cache(SESSION_TYPE+this.SessionId, this.Map)
+ }
+ if this.LongCache != nil {
+ this.LongCache.Cache(SESSION_TYPE+this.SessionId, this.Map)
+ }
}
+func (this *SessionIns) Session(key string, data ...interface{}) *Obj {
-func(this *SessionIns)Session(key string,data ...interface{})*Obj{
-
- if this.Map==nil{
+ if this.Map == nil {
this.get()
}
- if len(data)!=0{
- if data[0]==nil{
- delete(this.Map,key)
+ if len(data) != 0 {
+ if data[0] == nil {
+ delete(this.Map, key)
this.set()
- }else{
- this.Map[key]=data[0]
+ } else {
+ this.Map[key] = data[0]
this.set()
}
- return &Obj{Data:nil}
+ return &Obj{Data: nil}
}
- return &Obj{Data:this.Map.Get(key)}
+ return &Obj{Data: this.Map.Get(key)}
}
-func (this *SessionIns)get(){
+func (this *SessionIns) get() {
- if this.ShortCache!=nil{
- this.Map=this.ShortCache.Cache(SESSION_TYPE+this.SessionId).ToMap()
- if this.Map!=nil{
+ if this.ShortCache != nil {
+ this.Map = this.ShortCache.Cache(SESSION_TYPE + this.SessionId).ToMap()
+ if this.Map != nil {
return
}
}
- if this.LongCache!=nil{
- this.Map=this.LongCache.Cache(SESSION_TYPE+this.SessionId).ToMap()
- if this.Map!=nil{
- if this.ShortCache!=nil{
- this.ShortCache.Cache(SESSION_TYPE+this.SessionId,this.Map)
+ if this.LongCache != nil {
+ this.Map = this.LongCache.Cache(SESSION_TYPE + this.SessionId).ToMap()
+ if this.Map != nil {
+ if this.ShortCache != nil {
+ this.ShortCache.Cache(SESSION_TYPE+this.SessionId, this.Map)
}
return
}
}
- this.Map=Map{}
- this.ShortCache.Cache(SESSION_TYPE+this.SessionId,this.Map)
+ this.Map = Map{}
+ this.ShortCache.Cache(SESSION_TYPE+this.SessionId, this.Map)
return
}
-
-
-func (this *SessionIns)Init(short CacheIns, long CacheIns){
- this.ShortCache=short
- this.LongCache=long
-}
\ No newline at end of file
+func (this *SessionIns) Init(short CacheIns, long CacheIns) {
+ this.ShortCache = short
+ this.LongCache = long
+}
diff --git a/type.go b/type.go
index 9a1e480..d53c872 100644
--- a/type.go
+++ b/type.go
@@ -4,6 +4,7 @@ package hotime
type Ctr map[string]func(*Context)
type Proj map[string]Ctr
type Router map[string]Proj
+
type CacheIns interface {
set(key string, value interface{}, time int64)
get(key string) interface{}
diff --git a/var.go b/var.go
index e932c77..278daae 100644
--- a/var.go
+++ b/var.go
@@ -9,6 +9,9 @@ import "sync"
//}
var mutex = map[interface{}]*sync.RWMutex{} //安全锁
var mutexer = sync.RWMutex{} //读写锁
+var IsRun = false //当前状态
+var application = map[string]*App{} //整个项目
+
//var Db = HoTimeDB{} //数据库实例
var Config = Map{
@@ -17,12 +20,13 @@ var Config = Map{
"defFile": []string{"index.html", "index.htm"},
"dbHost": "127.0.0.1",
//"dbHost":"localhost",
- "dbName": "test",
- "dbUser": "root",
- "dbPwd": "root",
- "dbPort": "3306",
- "port": "80",
+ "dbName": "test",
+ "dbUser": "root",
+ "dbPwd": "root",
+ "dbPort": "3306",
+ "port": "80",
"cacheShortTime": 60 * 60 * 2,
- "cacheLongTime": 60 * 60 * 24*30,
- "sessionName":"HOTIME",
+ "cacheLongTime": 60 * 60 * 24 * 30,
+ "sessionName": "HOTIME",
+ "error": Map{},
}