优化多服务

This commit is contained in:
hoteas 2017-10-27 04:28:47 +00:00
parent fdc4a00176
commit 7423a7dd57
7 changed files with 76 additions and 72 deletions

View File

@ -5,12 +5,7 @@
</component>
<component name="ChangeListManager">
<list default="true" readonly="true" id="03ed5703-81a1-441b-849c-c426276586b7" name="Default" comment="">
<change type="DELETED" beforePath="$PROJECT_DIR$/.idea/libraries/GOPATH__hotime_.xml" afterPath="" />
<change type="DELETED" beforePath="$PROJECT_DIR$/.idea/libraries" afterPath="" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/application.go" afterPath="$PROJECT_DIR$/application.go" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/objtoobj.go" afterPath="$PROJECT_DIR$/objtoobj.go" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/slice.go" afterPath="$PROJECT_DIR$/slice.go" />
</list>
<ignored path=".idea/dataSources.local.xml" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -50,16 +45,6 @@
</provider>
</entry>
</file>
<file leaf-file-name="obj.go" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/obj.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="85">
<caret line="5" column="15" lean-forward="false" selection-start-line="5" selection-start-column="15" selection-end-line="5" selection-end-column="15" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="manage.go" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/manage/manage.go">
<provider selected="true" editor-type-id="text-editor">
@ -80,7 +65,7 @@
</provider>
</entry>
</file>
<file leaf-file-name="upload.go" pinned="false" current-in-tab="true">
<file leaf-file-name="upload.go" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/dri/upload/upload.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="510">
@ -92,6 +77,16 @@
</provider>
</entry>
</file>
<file leaf-file-name="index.html" pinned="false" current-in-tab="true">
<entry file="file://E:/work/hotimejs/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="391">
<caret line="23" column="7" lean-forward="true" selection-start-line="23" selection-start-column="7" selection-end-line="23" selection-end-column="7" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="config.json" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/example/config/config.json">
<provider selected="true" editor-type-id="text-editor">
@ -241,7 +236,7 @@
</component>
<component name="PropertiesComponent">
<property name="settings.editor.selected.configurable" value="preferences.pluginManager" />
<property name="last_opened_file_path" value="E:/desktop/项目/大竹需求平台/golang" />
<property name="last_opened_file_path" value="E:/work/hotimejs/index.html" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="configurable.Global.GOPATH.is.expanded" value="true" />
<property name="configurable.Project.GOPATH.is.expanded" value="true" />
@ -434,7 +429,7 @@
<workItem from="1504488391347" duration="840000" />
<workItem from="1504575702799" duration="11303000" />
<workItem from="1504661068799" duration="698000" />
<workItem from="1508808650888" duration="41000" />
<workItem from="1508808650888" duration="210000" />
</task>
<task id="LOCAL-00001">
<created>1500458878821</created>
@ -618,11 +613,18 @@
<option name="project" value="LOCAL" />
<updated>1504601110529</updated>
</task>
<option name="localTasksCounter" value="27" />
<task id="LOCAL-00027" summary="工具">
<created>1508808698714</created>
<option name="number" value="00027" />
<option name="presentableId" value="LOCAL-00027" />
<option name="project" value="LOCAL" />
<updated>1508808698714</updated>
</task>
<option name="localTasksCounter" value="28" />
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="89580000" />
<option name="totallyTimeSpent" value="89749000" />
</component>
<component name="TodoView">
<todo-panel id="selected-file">
@ -635,8 +637,9 @@
</component>
<component name="ToolWindowManager">
<frame x="55" y="-8" width="1873" height="1084" extended-state="0" />
<editor active="true" />
<layout>
<window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.34528404" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.34528404" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32846716" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="true" content_ui="tabs" />
<window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
@ -679,13 +682,6 @@
<watches-manager />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/error.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="255">
<caret line="15" column="0" lean-forward="false" selection-start-line="15" selection-start-column="0" selection-end-line="15" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/objtoobj.go">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-289">
@ -1025,5 +1021,13 @@
</state>
</provider>
</entry>
<entry file="file://E:/work/hotimejs/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="391">
<caret line="23" column="7" lean-forward="true" selection-start-line="23" selection-start-column="7" selection-end-line="23" selection-end-column="7" />
<folding />
</state>
</provider>
</entry>
</component>
</project>

View File

@ -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 {

View File

@ -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) {

21
func.go
View File

@ -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)
}

View File

@ -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) {

View File

@ -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)
}

2
var.go
View File

@ -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{} //数据库实例