This commit is contained in:
Liang Ding 2014-09-15 14:24:40 +08:00
parent 172f7f3b4f
commit 203c658553
5 changed files with 86 additions and 12 deletions

View File

@ -10,6 +10,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/b3log/wide/event"
_ "github.com/b3log/wide/i18n" _ "github.com/b3log/wide/i18n"
"github.com/b3log/wide/util" "github.com/b3log/wide/util"
"github.com/golang/glog" "github.com/golang/glog"
@ -37,6 +38,38 @@ type conf struct {
var Wide conf var Wide conf
var rawWide 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 指定的用户的工作空间路径. // 获取 username 指定的用户的工作空间路径.
func (*conf) GetUserWorkspace(username string) string { func (*conf) GetUserWorkspace(username string) string {
for _, user := range Wide.Users { for _, user := range Wide.Users {
@ -71,6 +104,7 @@ func (*conf) GetIDEStub() string {
} }
} }
// 保存 Wide 配置.
func Save() bool { func Save() bool {
// 只有 Users 是可以通过界面修改的,其他属性只能手工维护 wide.json 配置文件 // 只有 Users 是可以通过界面修改的,其他属性只能手工维护 wide.json 配置文件
rawWide.Users = Wide.Users rawWide.Users = Wide.Users
@ -93,7 +127,11 @@ func Save() bool {
return true return true
} }
// 加载 Wide 配置.
func Load() { func Load() {
// 检查 Wide 运行环境
Wide.CheckEnv()
bytes, _ := ioutil.ReadFile("conf/wide.json") bytes, _ := ioutil.ReadFile("conf/wide.json")
err := json.Unmarshal(bytes, &Wide) err := json.Unmarshal(bytes, &Wide)

38
event/events.go Normal file
View File

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

11
main.go
View File

@ -10,6 +10,7 @@ import (
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
"github.com/b3log/wide/editor" "github.com/b3log/wide/editor"
"github.com/b3log/wide/event"
"github.com/b3log/wide/file" "github.com/b3log/wide/file"
"github.com/b3log/wide/i18n" "github.com/b3log/wide/i18n"
"github.com/b3log/wide/output" "github.com/b3log/wide/output"
@ -20,12 +21,16 @@ import (
// Wide 中唯一一个 init 函数. // Wide 中唯一一个 init 函数.
func init() { func init() {
// 默认启动参数
flag.Set("logtostderr", "true") flag.Set("logtostderr", "true")
flag.Set("v", "1") flag.Set("v", "1")
conf.Load()
flag.Parse() flag.Parse()
// 加载事件处理
event.Load()
// 加载配置
conf.Load()
} }
// Wide 首页. // Wide 首页.

View File

@ -1,8 +0,0 @@
package notification
const (
EvtGOPATHNotFound = iota // 事件:找不到环境变量 $GOPATH
EvtGOROOTNotFound // 事件:找不到环境变量 $GOROOT
EvtGocodeNotFount // 事件:找不到 gocode
EvtIDEStubNotFound // 事件:找不到 IDE stub
)

View File

@ -1,5 +1,5 @@
// 通知. // 通知.
package notifications package notification
import ( import (
"net/http" "net/http"
@ -22,6 +22,7 @@ type Notification struct {
Message string Message string
} }
// 通知通道.
var notificationWS = map[string]*websocket.Conn{} var notificationWS = map[string]*websocket.Conn{}
func WSHandler(w http.ResponseWriter, r *http.Request) { func WSHandler(w http.ResponseWriter, r *http.Request) {