wide/main.go

368 lines
11 KiB
Go
Raw Normal View History

2014-11-12 18:13:14 +03:00
// Copyright (c) 2014, B3log
2014-11-13 06:54:52 +03:00
//
2014-11-12 18:13:14 +03:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-11-13 06:54:52 +03:00
//
2014-11-12 18:13:14 +03:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-11-13 06:54:52 +03:00
//
2014-11-12 18:13:14 +03:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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"
2014-11-02 10:44:24 +03:00
"math/rand"
"mime"
2014-09-07 13:07:25 +04:00
"net/http"
"os"
2014-09-07 13:07:25 +04:00
"runtime"
2014-11-02 10:44:24 +03:00
"strconv"
"strings"
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-29 09:05:10 +03:00
// The only one init function in Wide.
2014-09-02 18:57:30 +04:00
func init() {
2014-11-16 17:25:27 +03:00
confPath := flag.String("conf", "conf/wide.json", "path of wide.json")
2014-11-18 08:34:13 +03:00
confIP := flag.String("ip", "", "ip to visit")
confPort := flag.String("port", "", "port to visit")
2014-11-18 09:04:24 +03:00
confServer := flag.String("server", "", "this will overwrite Wide.Server if specified")
confChannel := flag.String("channel", "", "this will overwrite Wide.XXXChannel if specified")
2014-11-21 06:19:57 +03:00
confStat := flag.Bool("stat", false, "whether report statistics periodically")
2014-11-18 10:32:55 +03:00
confDocker := flag.Bool("docker", false, "whether run in a docker container")
2014-11-16 17:25:27 +03:00
2014-11-20 05:05:55 +03:00
flag.Set("alsologtostderr", "true")
flag.Set("stderrthreshold", "INFO")
2014-09-16 13:32:55 +04:00
flag.Set("v", "3")
2014-11-21 06:19:57 +03:00
2014-09-15 10:24:40 +04:00
flag.Parse()
2014-09-13 12:50:18 +04:00
wd := util.OS.Pwd()
if strings.HasPrefix(wd, os.TempDir()) {
2014-11-23 09:50:42 +03:00
glog.Error("Don't run wide in OS' temp directory or with `go run`")
os.Exit(-1)
}
2014-11-02 05:39:33 +03:00
i18n.Load()
2014-09-15 10:24:40 +04:00
event.Load()
2014-09-02 18:57:30 +04:00
2014-11-18 10:32:55 +03:00
conf.Load(*confPath, *confIP, *confPort, *confServer, *confChannel, *confDocker)
2014-11-16 17:25:27 +03:00
2014-09-22 19:13:07 +04:00
conf.FixedTimeCheckEnv()
conf.FixedTimeSave()
2014-09-23 17:03:44 +04:00
session.FixedTimeRelease()
2014-11-21 06:19:57 +03:00
if *confStat {
session.FixedTimeReport()
}
2014-09-02 18:57:30 +04:00
}
2014-10-29 09:05:10 +03:00
// indexHandler handles request of Wide index.
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-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-10-29 09:05:10 +03:00
// create a Wide session
2014-11-02 10:44:24 +03:00
rand.Seed(time.Now().UnixNano())
sid := strconv.Itoa(rand.Int())
wideSession := session.WideSessions.New(httpSession, sid)
2014-09-17 10:35:48 +04:00
2014-09-21 16:31:36 +04:00
username := httpSession.Values["username"].(string)
2014-10-30 13:21:44 +03:00
user := conf.Wide.GetUser(username)
if nil == user {
glog.Warningf("Not found user [%s]", username)
http.Redirect(w, r, "/login", http.StatusFound)
return
}
locale := user.Locale
2014-09-24 07:00:33 +04:00
wideSessions := session.WideSessions.GetByUsername(username)
2014-09-23 18:29:53 +04:00
2014-10-23 17:43:35 +04:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
2014-11-16 15:45:12 +03:00
"session": wideSession, "latestSessionContent": user.LatestSessionContent,
2014-11-30 05:18:17 +03:00
"pathSeparator": conf.PathSeparator, "codeMirrorVer": conf.CodeMirrorVer,
"user": user, "editorThemes": conf.GetEditorThemes()}
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-29 09:05:10 +03:00
// serveSingle registers the handler function for the given pattern and filename.
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-29 09:05:10 +03:00
// startHandler handles request of start page.
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-11-02 10:44:24 +03:00
sid := r.URL.Query()["sid"][0]
wSession := session.WideSessions.Get(sid)
if nil == wSession {
glog.Errorf("Session [%s] not found", sid)
}
2014-10-23 17:43:35 +04:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
2014-11-02 10:44:24 +03:00
"username": username, "workspace": userWorkspace, "ver": conf.WideVersion, "session": wSession}
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-29 09:05:10 +03:00
// keyboardShortcutsHandler handles request of keyboard shortcuts page.
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-29 09:05:10 +03:00
// aboutHandle handles request of about page.
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
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
"ver": conf.WideVersion, "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-10-29 09:05:10 +03:00
// Main.
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("/", 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-10-29 09:05:10 +03:00
// static resources
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-10-29 09:05:10 +03:00
// workspaces
for _, user := range conf.Wide.Users {
http.Handle("/workspace/"+user.Name+"/",
http.StripPrefix("/workspace/"+user.Name+"/", http.FileServer(http.Dir(user.GetWorkspace()))))
}
2014-09-01 17:40:10 +04:00
2014-10-29 09:05:10 +03:00
// session
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-10-29 09:05:10 +03:00
// run
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-10-29 09:05:10 +03:00
// file tree
2014-09-24 10:36:34 +04:00
http.HandleFunc("/files", handlerWrapper(file.GetFiles))
2014-11-25 09:20:45 +03:00
http.HandleFunc("/file/refresh", handlerWrapper(file.RefreshDirectory))
2014-09-24 10:36:34 +04:00
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-11-07 12:22:20 +03:00
http.HandleFunc("/file/rename", handlerWrapper(file.RenameFile))
2014-10-11 10:59:38 +04:00
http.HandleFunc("/file/search/text", handlerWrapper(file.SearchText))
2014-11-13 06:54:52 +03:00
http.HandleFunc("/file/find/name", handlerWrapper(file.Find))
2014-08-18 17:45:43 +04:00
2014-11-24 05:39:33 +03:00
// file export/import
2014-11-24 06:17:42 +03:00
http.HandleFunc("/file/zip/new", handlerWrapper(file.CreateZip))
2014-11-28 18:12:38 +03:00
http.HandleFunc("/file/zip", handlerWrapper(file.GetZip))
http.HandleFunc("/file/upload", handlerWrapper(file.Upload))
2014-11-24 05:39:33 +03:00
2014-10-29 09:05:10 +03:00
// editor
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))
2014-08-18 17:45:43 +04:00
2014-10-29 09:05:10 +03: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-10-29 09:05:10 +03:00
// notification
2014-09-24 10:36:34 +04:00
http.HandleFunc("/notification/ws", handlerWrapper(notification.WSHandler))
2014-09-15 14:03:52 +04:00
2014-10-29 09:05:10 +03:00
// user
http.HandleFunc("/login", handlerWrapper(session.LoginHandler))
http.HandleFunc("/logout", handlerWrapper(session.LogoutHandler))
http.HandleFunc("/signup", handlerWrapper(session.SignUpUser))
2014-11-17 05:49:25 +03:00
http.HandleFunc("/preference", handlerWrapper(session.PreferenceHandler))
2014-08-31 14:50:38 +04:00
2014-11-21 06:19:57 +03:00
glog.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-10-29 09:05:10 +03:00
// handlerWrapper wraps the HTTP Handler for some common processes.
2014-09-25 09:29:04 +04:00
//
2014-10-08 09:54:16 +04:00
// 1. panic recover
2014-10-29 09:05:10 +03:00
// 2. request stopwatch
// 3. i18n
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-29 09:05:10 +03:00
// i18nLoad wraps the i18n process.
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(w, r)
}
}
2014-10-29 09:05:10 +03:00
// stopwatch wraps the request stopwatch process.
2014-09-24 10:36:34 +04:00
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(w, r)
}
}
2014-10-29 09:05:10 +03:00
// panicRecover wraps the panic recover process.
2014-09-24 10:36:34 +04:00
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
2014-09-24 10:36:34 +04:00
handler(w, r)
2014-09-24 07:58:02 +04:00
}
}
2014-10-29 09:05:10 +03:00
// initMime initializes mime types.
//
2014-10-29 09:05:10 +03:00
// We can't get the mime types on some OS (such as Windows XP) by default, so initializes them here.
func initMime() {
mime.AddExtensionType(".css", "text/css")
mime.AddExtensionType(".js", "application/x-javascript")
mime.AddExtensionType(".json", "application/json")
}