From 203c6585537519245314fb17b4f262e43db743b1 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 15 Sep 2014 14:24:40 +0800 Subject: [PATCH] #49 --- conf/wide.go | 38 +++++++++++++++++++++++++++++++++++ event/events.go | 38 +++++++++++++++++++++++++++++++++++ main.go | 11 +++++++--- notification/events.go | 8 -------- notification/notifications.go | 3 ++- 5 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 event/events.go delete mode 100644 notification/events.go diff --git a/conf/wide.go b/conf/wide.go index 12440f3..c2a463e 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -10,6 +10,7 @@ import ( "runtime" "strings" + "github.com/b3log/wide/event" _ "github.com/b3log/wide/i18n" "github.com/b3log/wide/util" "github.com/golang/glog" @@ -37,6 +38,38 @@ type conf struct { var Wide conf var rawWide conf +// 检查 Wide 运行环境. +// 如果是特别严重的问题(比如 $GOPATH 不存在)则退出进程。另一些不太严重的问题(比如 gocode 不存在)则放入全局通知队列。 +func (*conf) CheckEnv() { + if "" == os.Getenv("GOPATH") { + glog.Fatal("Not found $GOPATH") + os.Exit(-1) + } + + if "" == os.Getenv("GOROOT") { + glog.Fatal("Not found $GOROOT") + + os.Exit(-1) + } + + gocode := Wide.GetGocode() + cmd := exec.Command(gocode, "close") + _, err := cmd.Output() + if nil != err { + event.EventQueue <- event.EvtGocodeNotFount + glog.Warning("Not found gocode") + } + + ide_stub := Wide.GetIDEStub() + cmd = exec.Command(ide_stub, "version") + _, err = cmd.Output() + if nil != err { + glog.Info(err) + event.EventQueue <- event.EvtIDEStubNotFound + glog.Warning("Not found ide_stub") + } +} + // 获取 username 指定的用户的工作空间路径. func (*conf) GetUserWorkspace(username string) string { for _, user := range Wide.Users { @@ -71,6 +104,7 @@ func (*conf) GetIDEStub() string { } } +// 保存 Wide 配置. func Save() bool { // 只有 Users 是可以通过界面修改的,其他属性只能手工维护 wide.json 配置文件 rawWide.Users = Wide.Users @@ -93,7 +127,11 @@ func Save() bool { return true } +// 加载 Wide 配置. func Load() { + // 检查 Wide 运行环境 + Wide.CheckEnv() + bytes, _ := ioutil.ReadFile("conf/wide.json") err := json.Unmarshal(bytes, &Wide) diff --git a/event/events.go b/event/events.go new file mode 100644 index 0000000..0dfd4e2 --- /dev/null +++ b/event/events.go @@ -0,0 +1,38 @@ +// 事件处理. +package event + +import ( + "github.com/golang/glog" +) + +const ( + EvtGOPATHNotFound = iota // 事件:找不到环境变量 $GOPATH + EvtGOROOTNotFound // 事件:找不到环境变量 $GOROOT + EvtGocodeNotFount // 事件:找不到 gocode + EvtIDEStubNotFound // 事件:找不到 IDE stub +) + +// 全局事件队列. +// 入队的事件将分发到每个用户的通知队列. +var EventQueue = make(chan int, 10) + +// 用户事件队列. +// 入队的事件将翻译为通知,并通过通知通道推送到前端. +var UserEventQueue map[string]chan int + +// 加载事件处理. +func Load() { + go func() { + for { + // 获取事件 + event := <-EventQueue + + glog.V(5).Info("收到全局事件 [%d]", event) + + // 将事件分发到每个用户的事件队列里 + for _, userQueue := range UserEventQueue { + userQueue <- event + } + } + }() +} diff --git a/main.go b/main.go index 85b1032..b2e7866 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/b3log/wide/conf" "github.com/b3log/wide/editor" + "github.com/b3log/wide/event" "github.com/b3log/wide/file" "github.com/b3log/wide/i18n" "github.com/b3log/wide/output" @@ -20,12 +21,16 @@ import ( // Wide 中唯一一个 init 函数. func init() { + // 默认启动参数 flag.Set("logtostderr", "true") flag.Set("v", "1") - - conf.Load() - flag.Parse() + + // 加载事件处理 + event.Load() + + // 加载配置 + conf.Load() } // Wide 首页. diff --git a/notification/events.go b/notification/events.go deleted file mode 100644 index 37b60c4..0000000 --- a/notification/events.go +++ /dev/null @@ -1,8 +0,0 @@ -package notification - -const ( - EvtGOPATHNotFound = iota // 事件:找不到环境变量 $GOPATH - EvtGOROOTNotFound // 事件:找不到环境变量 $GOROOT - EvtGocodeNotFount // 事件:找不到 gocode - EvtIDEStubNotFound // 事件:找不到 IDE stub -) diff --git a/notification/notifications.go b/notification/notifications.go index 16d09df..56aceb2 100644 --- a/notification/notifications.go +++ b/notification/notifications.go @@ -1,5 +1,5 @@ // 通知. -package notifications +package notification import ( "net/http" @@ -22,6 +22,7 @@ type Notification struct { Message string } +// 通知通道. var notificationWS = map[string]*websocket.Conn{} func WSHandler(w http.ResponseWriter, r *http.Request) {