This commit is contained in:
parent
172f7f3b4f
commit
203c658553
38
conf/wide.go
38
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)
|
||||
|
|
|
@ -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
11
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 首页.
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package notification
|
||||
|
||||
const (
|
||||
EvtGOPATHNotFound = iota // 事件:找不到环境变量 $GOPATH
|
||||
EvtGOROOTNotFound // 事件:找不到环境变量 $GOROOT
|
||||
EvtGocodeNotFount // 事件:找不到 gocode
|
||||
EvtIDEStubNotFound // 事件:找不到 IDE stub
|
||||
)
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue