wide/main.go

398 lines
10 KiB
Go
Raw Normal View History

2014-08-18 17:45:43 +04:00
package main
import (
2014-09-21 16:31:36 +04:00
"encoding/json"
2014-09-02 18:57:30 +04:00
"flag"
2014-09-07 13:07:25 +04:00
"html/template"
"math/rand"
"mime"
2014-09-07 13:07:25 +04:00
"net/http"
"runtime"
"strconv"
2014-09-24 10:36:34 +04:00
"time"
2014-09-07 13:07:25 +04:00
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"
2014-09-17 10:35:48 +04:00
"github.com/b3log/wide/session"
2014-08-18 17:51:03 +04:00
"github.com/b3log/wide/shell"
2014-09-21 16:31:36 +04:00
"github.com/b3log/wide/util"
2014-08-18 17:45:43 +04:00
"github.com/golang/glog"
)
2014-10-13 08:22:50 +04:00
const (
2014-10-27 11:00:02 +03:00
Ver = "1.0.1" // Wide 版本
CodeMirrorVer = "4.7" // 编辑器版本
2014-10-13 08:22:50 +04:00
)
2014-10-10 12:49:31 +04:00
2014-09-02 18:57:30 +04:00
// Wide 中唯一一个 init 函数.
func init() {
2014-10-24 06:10:59 +04:00
// TODO: 默认启动参数
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
2014-09-19 17:10:04 +04:00
// 定时检查运行环境
2014-09-22 19:13:07 +04:00
conf.FixedTimeCheckEnv()
// 定时保存配置
conf.FixedTimeSave()
2014-09-23 17:03:44 +04:00
// 定时检查无效会话
session.FixedTimeRelease()
2014-09-02 18:57:30 +04:00
}
2014-09-21 16:31:36 +04:00
// 登录.
func loginHandler(w http.ResponseWriter, r *http.Request) {
2014-10-12 18:58:08 +04:00
if "GET" == r.Method {
2014-09-21 16:31:36 +04:00
// 展示登录页面
2014-10-23 17:43:35 +04:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(conf.Wide.Locale),
"locale": conf.Wide.Locale, "ver": Ver}
2014-09-21 16:31:36 +04:00
2014-10-24 07:41:23 +04:00
t, err := template.ParseFiles("views/login.html")
2014-09-21 16:31:36 +04:00
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
return
}
t.Execute(w, model)
return
}
// 非 GET 请求当作是登录请求
succ := false
data := map[string]interface{}{"succ": &succ}
defer util.RetJSON(w, r, data)
2014-09-23 18:29:53 +04:00
args := struct {
Username string
Password string
}{}
2014-09-21 16:31:36 +04:00
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
glog.Error(err)
succ = true
return
}
for _, user := range conf.Wide.Users {
2014-09-23 18:29:53 +04:00
if user.Name == args.Username && user.Password == args.Password {
2014-09-21 16:31:36 +04:00
succ = true
}
}
if !succ {
return
}
// 创建 HTTP 会话
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-09-23 18:29:53 +04:00
httpSession.Values["username"] = args.Username
2014-09-21 16:31:36 +04:00
httpSession.Values["id"] = strconv.Itoa(rand.Int())
2014-10-13 08:22:50 +04:00
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
2014-09-21 16:31:36 +04:00
httpSession.Save(r, w)
2014-09-23 18:29:53 +04:00
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), args.Username)
2014-09-21 16:31:36 +04:00
}
2014-10-15 17:50:28 +04:00
// 退出(登出).
func logoutHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-10-20 14:02:41 +04:00
httpSession.Options.MaxAge = -1
2014-10-15 17:50:28 +04:00
httpSession.Save(r, w)
}
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-17 10:35:48 +04:00
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-08-18 17:45:43 +04:00
2014-09-16 19:58:52 +04:00
if httpSession.IsNew {
2014-10-20 14:02:41 +04:00
http.Redirect(w, r, "/login", http.StatusFound)
2014-09-01 14:55:11 +04:00
2014-09-21 16:31:36 +04:00
return
2014-09-01 14:55:11 +04:00
}
2014-08-18 17:45:43 +04:00
2014-10-13 08:22:50 +04:00
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
2014-09-16 19:58:52 +04:00
httpSession.Save(r, w)
2014-08-18 17:45:43 +04:00
2014-09-17 10:35:48 +04:00
// 创建一个 Wide 会话
wideSession := session.WideSessions.New(httpSession)
2014-09-21 16:31:36 +04:00
username := httpSession.Values["username"].(string)
2014-10-23 17:43:35 +04:00
locale := conf.Wide.GetUser(username).Locale
2014-09-24 07:00:33 +04:00
wideSessions := session.WideSessions.GetByUsername(username)
2014-09-23 18:29:53 +04:00
userConf := conf.Wide.GetUser(username)
2014-10-23 17:43:35 +04:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
2014-10-13 10:40:55 +04:00
"session": wideSession, "latestSessionContent": userConf.LatestSessionContent,
2014-10-27 11:00:02 +03:00
"pathSeparator": conf.PathSeparator, "codeMirrorVer": CodeMirrorVer}
2014-09-23 18:29:53 +04:00
2014-09-17 07:17:25 +04:00
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
2014-10-24 07:41:23 +04:00
t, err := template.ParseFiles("views/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-10-12 18:00:26 +04:00
// 单个文件资源请求处理.
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
})
2014-09-17 07:17:25 +04:00
}
2014-10-19 14:41:01 +04:00
// 起始页请求处理.
func startHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
2014-10-20 14:02:41 +04:00
http.Redirect(w, r, "/login", http.StatusFound)
2014-10-19 14:41:01 +04:00
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
httpSession.Save(r, w)
username := httpSession.Values["username"].(string)
2014-10-23 17:43:35 +04:00
locale := conf.Wide.GetUser(username).Locale
2014-10-21 13:41:42 +04:00
userWorkspace := conf.Wide.GetUserWorkspace(username)
2014-10-19 14:41:01 +04:00
2014-10-23 17:43:35 +04:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
2014-10-21 13:41:42 +04:00
"username": username, "workspace": userWorkspace, "ver": Ver}
2014-10-19 14:41:01 +04:00
2014-10-24 07:41:23 +04:00
t, err := template.ParseFiles("views/start.html")
2014-10-19 14:41:01 +04:00
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
return
}
t.Execute(w, model)
}
2014-10-21 18:48:33 +04:00
// 键盘快捷键页请求处理.
func keyboardShortcutsHandler(w http.ResponseWriter, r *http.Request) {
2014-10-23 17:43:35 +04:00
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
httpSession.Save(r, w)
username := httpSession.Values["username"].(string)
locale := conf.Wide.GetUser(username).Locale
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale}
2014-10-21 18:48:33 +04:00
2014-10-24 07:41:23 +04:00
t, err := template.ParseFiles("views/keyboard_shortcuts.html")
2014-10-21 18:48:33 +04:00
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
return
}
t.Execute(w, model)
}
2014-10-20 18:27:19 +04:00
// 关于页请求处理.
func aboutHandler(w http.ResponseWriter, r *http.Request) {
2014-10-23 17:43:35 +04:00
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
httpSession.Save(r, w)
username := httpSession.Values["username"].(string)
locale := conf.Wide.GetUser(username).Locale
2014-10-23 19:14:34 +04:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale, "ver": Ver,
"goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version()}
2014-10-20 18:27:19 +04:00
2014-10-24 07:41:23 +04:00
t, err := template.ParseFiles("views/about.html")
2014-10-20 18:27:19 +04:00
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
return
}
t.Execute(w, model)
}
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)
initMime()
2014-09-02 18:57:30 +04:00
defer glog.Flush()
2014-10-12 18:00:26 +04:00
// IDE
http.HandleFunc("/login", handlerWrapper(loginHandler))
2014-10-15 17:50:28 +04:00
http.HandleFunc("/logout", handlerWrapper(logoutHandler))
2014-10-12 18:00:26 +04:00
http.HandleFunc("/", handlerWrapper(indexHandler))
2014-10-19 14:41:01 +04:00
http.HandleFunc("/start", handlerWrapper(startHandler))
2014-10-20 18:27:19 +04:00
http.HandleFunc("/about", handlerWrapper(aboutHandler))
2014-10-21 18:48:33 +04:00
http.HandleFunc("/keyboard_shortcuts", handlerWrapper(keyboardShortcutsHandler))
2014-10-12 18:00:26 +04:00
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-10-12 18:00:26 +04:00
serveSingle("/favicon.ico", "./static/favicon.ico")
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-23 05:45:56 +04:00
// 会话
2014-09-24 10:36:34 +04:00
http.HandleFunc("/session/ws", handlerWrapper(session.WSHandler))
http.HandleFunc("/session/save", handlerWrapper(session.SaveContent))
2014-08-18 17:45:43 +04:00
2014-09-01 06:47:59 +04:00
// 运行相关
2014-09-24 10:36:34 +04:00
http.HandleFunc("/build", handlerWrapper(output.BuildHandler))
http.HandleFunc("/run", handlerWrapper(output.RunHandler))
http.HandleFunc("/stop", handlerWrapper(output.StopHandler))
2014-10-27 18:02:15 +03:00
http.HandleFunc("/go/test", handlerWrapper(output.GoTestHandler))
2014-09-24 10:36:34 +04:00
http.HandleFunc("/go/get", handlerWrapper(output.GoGetHandler))
http.HandleFunc("/go/install", handlerWrapper(output.GoInstallHandler))
http.HandleFunc("/output/ws", handlerWrapper(output.WSHandler))
2014-08-18 17:45:43 +04:00
2014-09-01 06:47:59 +04:00
// 文件树
2014-09-24 10:36:34 +04:00
http.HandleFunc("/files", handlerWrapper(file.GetFiles))
http.HandleFunc("/file", handlerWrapper(file.GetFile))
http.HandleFunc("/file/save", handlerWrapper(file.SaveFile))
http.HandleFunc("/file/new", handlerWrapper(file.NewFile))
http.HandleFunc("/file/remove", handlerWrapper(file.RemoveFile))
2014-10-11 10:59:38 +04:00
http.HandleFunc("/file/search/text", handlerWrapper(file.SearchText))
2014-08-18 17:45:43 +04:00
2014-09-01 06:47:59 +04:00
// 编辑器
2014-09-24 10:36:34 +04:00
http.HandleFunc("/editor/ws", handlerWrapper(editor.WSHandler))
http.HandleFunc("/go/fmt", handlerWrapper(editor.GoFmtHandler))
http.HandleFunc("/autocomplete", handlerWrapper(editor.AutocompleteHandler))
2014-10-10 07:18:52 +04:00
http.HandleFunc("/exprinfo", handlerWrapper(editor.GetExprInfoHandler))
2014-09-24 10:36:34 +04:00
http.HandleFunc("/find/decl", handlerWrapper(editor.FindDeclarationHandler))
http.HandleFunc("/find/usages", handlerWrapper(editor.FindUsagesHandler))
http.HandleFunc("/html/fmt", handlerWrapper(editor.HTMLFmtHandler))
http.HandleFunc("/json/fmt", handlerWrapper(editor.JSONFmtHandler))
2014-08-18 17:45:43 +04:00
2014-09-01 06:47:59 +04:00
// Shell
2014-09-24 10:36:34 +04:00
http.HandleFunc("/shell/ws", handlerWrapper(shell.WSHandler))
http.HandleFunc("/shell", handlerWrapper(shell.IndexHandler))
2014-08-18 17:45:43 +04:00
2014-09-15 14:03:52 +04:00
// 通知
2014-09-24 10:36:34 +04:00
http.HandleFunc("/notification/ws", handlerWrapper(notification.WSHandler))
2014-09-15 14:03:52 +04:00
2014-09-01 06:47:59 +04:00
// 用户
2014-09-24 10:36:34 +04:00
http.HandleFunc("/user/new", handlerWrapper(session.AddUser))
http.HandleFunc("/user/repos/init", handlerWrapper(session.InitGitRepos))
2014-08-31 14:50:38 +04:00
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)
}
}
2014-09-24 07:58:02 +04:00
2014-09-25 09:37:59 +04:00
// HTTP Handler 包装,完成共性处理.
//
// 共性处理:
2014-09-25 09:29:04 +04:00
//
2014-10-08 09:54:16 +04:00
// 1. panic recover
// 2. 请求计时
2014-09-24 10:36:34 +04:00
func handlerWrapper(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
handler := panicRecover(f)
handler = stopwatch(handler)
2014-10-26 06:44:13 +03:00
handler = i18nLoad(handler)
2014-09-24 10:36:34 +04:00
return handler
}
2014-10-26 06:44:13 +03:00
// 国际化处理包装.
func i18nLoad(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
i18n.Load()
// Handler 处理
handler(w, r)
}
}
2014-09-24 10:36:34 +04:00
// Handler 包装请求计时.
func stopwatch(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer func() {
glog.V(5).Infof("[%s] [%s]", r.RequestURI, time.Since(start))
}()
// Handler 处理
handler(w, r)
}
}
// Handler 包装 recover panic.
func panicRecover(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
2014-09-24 07:58:02 +04:00
return func(w http.ResponseWriter, r *http.Request) {
2014-09-26 13:36:12 +04:00
defer util.Recover()
2014-09-24 07:58:02 +04:00
// Handler 处理
2014-09-24 10:36:34 +04:00
handler(w, r)
2014-09-24 07:58:02 +04:00
}
}
// 初始化 mime.
//
// 有的操作系统(例如 Windows XP上运行时根据文件后缀获取不到对应的 mime 类型,会导致响应的 HTTP content-type 不正确。
func initMime() {
mime.AddExtensionType(".css", "text/css")
mime.AddExtensionType(".js", "application/x-javascript")
mime.AddExtensionType(".json", "application/json")
}