diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bbb8107..220b8c9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,12 +5,7 @@ - - - - - - + @@ -434,7 +429,7 @@ - + 1500458878821 @@ -618,11 +613,18 @@ - - @@ -635,8 +637,9 @@ + - + @@ -679,13 +682,6 @@ - - - - - - - @@ -1025,5 +1021,13 @@ + + + + + + + + \ No newline at end of file diff --git a/application.go b/application.go index 0272ad7..b9496ac 100644 --- a/application.go +++ b/application.go @@ -22,9 +22,15 @@ type Application struct { configPath string Config Map Db HoTimeDB + Server *http.Server CacheIns sessionLong CacheIns sessionShort CacheIns + http.Handler +} + +func (this *Application) ServeHTTP(w http.ResponseWriter, req *http.Request) { + this.handler(w, req) } //启动实例 @@ -50,7 +56,15 @@ func (this *Application) Run(router Router) { } }() - run(this) + this.Server = &http.Server{} + if !IsRun { + IsRun = true + } + App[this.Port] = this + this.Server.Handler = this + //启动服务 + this.Server.Addr = ":" + this.Port + this.Server.ListenAndServe() } @@ -71,7 +85,7 @@ func (this *Application) SetSession(short CacheIns, Long CacheIns) { func (this *Application) SetDefault(connect func(err ...*Error) *sql.DB) { this.SetConfig() this.connectDbFunc = connect - if this.connectDbFunc!=nil{ + if this.connectDbFunc != nil { this.Db.SetConnect(this.connectDbFunc) } @@ -196,7 +210,7 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) { // 如果token存在就判断token是否在Session中有保存 // 如果有取出token并复制给cookie // 没有保存就生成随机的session - cookie, err := req.Cookie((Config["sessionName"]).(string)) + cookie, err := req.Cookie((this.Config["sessionName"]).(string)) sessionId := Md5(strconv.Itoa(Rand(10))) token := req.FormValue("token") //isFirst:=false @@ -207,7 +221,7 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) { //else{ // isFirst=true; //} - http.SetCookie(w, &http.Cookie{Name: Config["sessionName"].(string), Value: sessionId, Path: "/"}) + http.SetCookie(w, &http.Cookie{Name: this.Config["sessionName"].(string), Value: sessionId, Path: "/"}) } else { sessionId = cookie.Value } @@ -250,13 +264,14 @@ func (this *Application) handler(w http.ResponseWriter, req *http.Request) { } //url赋值 - path := Config.GetString("tpt") + o + path := this.Config.GetString("tpt") + o //判断是否为默认 if path[len(path)-1] == '/' { - defFile := Config["defFile"].([]string) + defFile := this.Config.GetSlice("defFile") + for i := 0; i < len(defFile); i++ { - temp := path + defFile[i] + temp := path + defFile.GetString(i) _, err := os.Stat(temp) if err == nil { diff --git a/example/main.go b/example/main.go index 90b0591..6a527f3 100644 --- a/example/main.go +++ b/example/main.go @@ -50,13 +50,13 @@ func main() { "index": hotime.Ctr{ "test": func(this *hotime.Context) { fmt.Println(this.Db.GetTag()) - x:=this.Db.Action(func(db hotime.HoTimeDB) bool { - - db.Insert("user",hotime.Map{"unickname":"dasdas"}) - - return true - }) - this.Display(5, x) + //x:=this.Db.Action(func(db hotime.HoTimeDB) bool { + // + // db.Insert("user",hotime.Map{"unickname":"dasdas"}) + // + // return true + //}) + this.Display(5, "dsadas") }, "websocket": func(this *hotime.Context) { hdler := websocket.Handler(func(ws *websocket.Conn) { diff --git a/func.go b/func.go index bd5884d..a99f022 100644 --- a/func.go +++ b/func.go @@ -6,7 +6,6 @@ import ( "math" "math/rand" "strings" - "net/http" "sync" "time" ) @@ -183,7 +182,6 @@ func RandX(small int, max int) int { // GetDb() //} - //复制返回数组 func DeepCopyMap(value interface{}) interface{} { if valueMap, ok := value.(Map); ok { @@ -300,23 +298,4 @@ 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 *Application) { - if !IsRun { - http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - port:="80" - if IndexLastStr(req.Host, ":")!=-1{ - port = Substr(req.Host, IndexLastStr(req.Host, ":")+1, len(req.Host)) - } - - if application[port] != nil { - application[port].handler(w, req) - } - }) - IsRun = true - } - - application[a.Port] = a - http.ListenAndServe(":"+a.Port, nil) - -} diff --git a/manage/manage.go b/manage/manage.go index ec1503a..b257cf5 100644 --- a/manage/manage.go +++ b/manage/manage.go @@ -46,13 +46,13 @@ func Run() { "index": hotime.Ctr{ "test": func(this *hotime.Context) { fmt.Println(this.Db.GetTag()) - x := this.Db.Action(func(db hotime.HoTimeDB) bool { - - db.Insert("user", hotime.Map{"unickname": "dasdas"}) - - return true - }) - this.Display(5, x) + //x := this.Db.Action(func(db hotime.HoTimeDB) bool { + // + // db.Insert("user", hotime.Map{"unickname": "dasdas"}) + // + // return true + //}) + this.Display(0, 1) }, "websocket": func(this *hotime.Context) { hdler := websocket.Handler(func(ws *websocket.Conn) { diff --git a/objtoobj.go b/objtoobj.go index e0f9445..6d1ece6 100644 --- a/objtoobj.go +++ b/objtoobj.go @@ -46,6 +46,12 @@ func ObjToSlice(obj interface{}, e ...*Error) Slice { v = obj.(Slice) case []interface{}: v = obj.([]interface{}) + case []string: + v = Slice{} + for i := 0; i < len(obj.([]string)); i++ { + v = append(v, obj.([]string)[i]) + } + default: v = nil err = errors.New("没有合适的转换对象!") @@ -236,7 +242,7 @@ func StrArrayToJsonStr(a string) string { //字符串数组: a1,a2,a3转["a1","a2","a3"] func JsonStrToStrArray(a string) string { a = strings.Replace(a, `"`, "", -1) - if len(a)!=0{ + if len(a) != 0 { a = Substr(a, 1, len(a)-2) } diff --git a/var.go b/var.go index 9e09907..e17e760 100644 --- a/var.go +++ b/var.go @@ -10,7 +10,7 @@ import "sync" var mutex = map[interface{}]*sync.RWMutex{} //安全锁 var mutexer = sync.RWMutex{} //读写锁 var IsRun = false //当前状态 -var application = map[string]*Application{} //整个项目 +var App = map[string]*Application{} //整个项目 //var Db = HoTimeDB{} //数据库实例