wide/main.go

151 lines
3.8 KiB
Go
Raw Normal View History

2014-08-18 17:45:43 +04:00
package main
import (
2014-09-02 18:57:30 +04:00
"flag"
2014-09-07 13:07:25 +04:00
"html/template"
"math/rand"
"net/http"
"runtime"
"strconv"
2014-08-18 17:51:03 +04:00
"github.com/b3log/wide/conf"
"github.com/b3log/wide/editor"
2014-09-15 10:24:40 +04:00
"github.com/b3log/wide/event"
2014-08-18 17:51:03 +04:00
"github.com/b3log/wide/file"
2014-08-25 18:17:02 +04:00
"github.com/b3log/wide/i18n"
2014-09-15 14:03:52 +04:00
"github.com/b3log/wide/notification"
2014-08-18 17:51:03 +04:00
"github.com/b3log/wide/output"
"github.com/b3log/wide/shell"
2014-08-31 13:39:39 +04:00
"github.com/b3log/wide/user"
2014-08-18 17:45:43 +04:00
"github.com/golang/glog"
)
2014-09-02 18:57:30 +04:00
// Wide 中唯一一个 init 函数.
func init() {
2014-09-15 10:24:40 +04:00
// 默认启动参数
2014-09-02 18:57:30 +04:00
flag.Set("logtostderr", "true")
2014-09-16 13:32:55 +04:00
flag.Set("v", "3")
2014-09-15 10:24:40 +04:00
flag.Parse()
2014-09-13 12:50:18 +04:00
2014-09-15 10:24:40 +04:00
// 加载事件处理
event.Load()
2014-09-02 18:57:30 +04:00
2014-09-15 10:24:40 +04:00
// 加载配置
conf.Load()
2014-09-15 14:03:52 +04:00
// 定时检查 Wide 运行环境
conf.CheckEnv()
2014-09-02 18:57:30 +04:00
}
2014-09-12 13:10:58 +04:00
// Wide 首页.
2014-08-18 17:45:43 +04:00
func indexHandler(w http.ResponseWriter, r *http.Request) {
2014-09-16 19:58:52 +04:00
// 创建一个 Wide 会话
2014-09-17 05:41:22 +04:00
wideSession := user.WideSessions.New()
2014-09-16 19:58:52 +04:00
2014-09-02 18:57:30 +04:00
i18n.Load()
2014-09-16 19:58:52 +04:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
"session": wideSession}
2014-08-25 18:17:02 +04:00
2014-09-17 06:11:18 +04:00
httpSession, _ := user.HTTPSession.Get(r, "wide-session")
2014-08-18 17:45:43 +04:00
2014-09-17 07:17:25 +04:00
httpSessionId := httpSession.Values["id"].(string)
// TODO: 写死以 admin 作为用户登录
username := conf.Wide.Users[0].Name
2014-09-16 19:58:52 +04:00
if httpSession.IsNew {
2014-09-01 14:55:11 +04:00
2014-09-17 07:17:25 +04:00
httpSession.Values["username"] = username
httpSessionId = strconv.Itoa(rand.Int())
2014-09-17 05:41:22 +04:00
httpSession.Values["id"] = httpSessionId
2014-09-02 05:56:36 +04:00
// 一天过期
2014-09-16 19:58:52 +04:00
httpSession.Options.MaxAge = 60 * 60 * 24
2014-09-02 05:56:36 +04:00
2014-09-17 07:17:25 +04:00
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), username)
2014-09-01 14:55:11 +04:00
}
2014-08-18 17:45:43 +04:00
2014-09-16 19:58:52 +04:00
httpSession.Save(r, w)
2014-08-18 17:45:43 +04:00
2014-09-17 05:41:22 +04:00
// Wide 会话关联 HTTP 会话
wideSession.HTTPSessionId = httpSession.Values["id"].(string)
2014-09-17 07:17:25 +04:00
wideSessions := user.WideSessions.GetByHTTPSid(httpSessionId)
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
2014-09-01 06:47:59 +04:00
t, err := template.ParseFiles("view/index.html")
2014-08-18 17:45:43 +04:00
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
return
}
2014-08-25 18:17:02 +04:00
t.Execute(w, model)
2014-08-18 17:45:43 +04:00
}
2014-09-17 07:17:25 +04:00
// favicon.ico 请求处理.
func faviconHandler(w http.ResponseWriter, r *http.Request) {
// TODO: favicon.ico 请求处理.
}
2014-09-16 11:06:52 +04:00
// 主程序入口.
2014-08-18 17:45:43 +04:00
func main() {
2014-09-02 18:57:30 +04:00
runtime.GOMAXPROCS(conf.Wide.MaxProcs)
defer glog.Flush()
2014-09-01 06:47:59 +04:00
// 静态资源
2014-08-18 17:45:43 +04:00
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
2014-09-17 07:17:25 +04:00
http.HandleFunc("/favicon.ico", faviconHandler)
2014-08-18 17:45:43 +04:00
2014-09-01 17:40:10 +04:00
// 库资源
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data"))))
2014-09-01 06:47:59 +04:00
// IDE 首页
2014-08-18 17:45:43 +04:00
http.HandleFunc("/", indexHandler)
2014-09-01 06:47:59 +04:00
// 运行相关
2014-08-22 07:57:05 +04:00
http.HandleFunc("/build", output.BuildHandler)
2014-08-18 17:45:43 +04:00
http.HandleFunc("/run", output.RunHandler)
2014-09-05 08:18:50 +04:00
http.HandleFunc("/go/get", output.GoGetHandler)
2014-09-09 13:01:22 +04:00
http.HandleFunc("/go/install", output.GoInstallHandler)
2014-08-18 17:45:43 +04:00
http.HandleFunc("/output/ws", output.WSHandler)
2014-09-01 06:47:59 +04:00
// 文件树
2014-08-18 17:45:43 +04:00
http.HandleFunc("/files", file.GetFiles)
http.HandleFunc("/file", file.GetFile)
http.HandleFunc("/file/save", file.SaveFile)
http.HandleFunc("/file/new", file.NewFile)
http.HandleFunc("/file/remove", file.RemoveFile)
2014-09-01 06:47:59 +04:00
// 编辑器
2014-08-18 17:45:43 +04:00
http.HandleFunc("/editor/ws", editor.WSHandler)
2014-09-06 20:13:25 +04:00
http.HandleFunc("/go/fmt", editor.GoFmtHandler)
2014-09-01 06:47:59 +04:00
http.HandleFunc("/autocomplete", editor.AutocompleteHandler)
2014-09-12 11:55:52 +04:00
http.HandleFunc("/find/decl", editor.FindDeclarationHandler)
http.HandleFunc("/find/usages", editor.FindUsagesHandler)
2014-09-07 13:07:25 +04:00
http.HandleFunc("/html/fmt", editor.HTMLFmtHandler)
2014-09-07 14:13:55 +04:00
http.HandleFunc("/json/fmt", editor.JSONFmtHandler)
2014-08-18 17:45:43 +04:00
2014-09-01 06:47:59 +04:00
// Shell
2014-08-18 17:45:43 +04:00
http.HandleFunc("/shell/ws", shell.WSHandler)
2014-09-05 10:33:43 +04:00
http.HandleFunc("/shell", shell.IndexHandler)
2014-08-18 17:45:43 +04:00
2014-09-15 14:03:52 +04:00
// 通知
http.HandleFunc("/notification/ws", notification.WSHandler)
2014-09-01 06:47:59 +04:00
// 用户
2014-08-31 14:50:38 +04:00
http.HandleFunc("/user/new", user.AddUser)
http.HandleFunc("/user/repos/init", user.InitGitRepos)
2014-09-01 06:47:59 +04:00
// 文档
http.Handle("/doc/", http.StripPrefix("/doc/", http.FileServer(http.Dir("doc"))))
2014-09-08 07:37:34 +04:00
glog.V(0).Infof("Wide is running [%s]", conf.Wide.Server)
2014-08-31 13:23:14 +04:00
2014-08-18 17:45:43 +04:00
err := http.ListenAndServe(conf.Wide.Server, nil)
if err != nil {
glog.Fatal(err)
}
}