wide/main.go

441 lines
14 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-12-09 06:51:14 +03:00
"compress/gzip"
2014-09-02 18:57:30 +04:00
"flag"
2014-09-07 13:07:25 +04:00
"html/template"
2014-12-09 06:51:14 +03:00
"io"
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-12-13 13:47:41 +03:00
"github.com/b3log/wide/log"
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
)
2014-12-13 13:47:41 +03:00
// Logger
var logger *log.Logger
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")
2014-12-14 18:16:11 +03:00
confLogLevel := flag.String("log_level", "info", "logging level: trace/debug/info/warn/error")
2014-12-11 10:59:58 +03:00
confStaticServer := flag.String("static_server", "", "this will overwrite Wide.StaticServer if specified")
2014-12-11 10:32:24 +03:00
confContext := flag.String("context", "", "this will overwrite Wide.Context if specified")
2014-11-18 09:04:24 +03:00
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-21 06:19:57 +03:00
2014-09-15 10:24:40 +04:00
flag.Parse()
2014-09-13 12:50:18 +04:00
2014-12-13 13:47:41 +03:00
log.SetLevel("warn")
logger = log.NewLogger(os.Stdout)
wd := util.OS.Pwd()
if strings.HasPrefix(wd, os.TempDir()) {
2014-12-13 13:47:41 +03:00
logger.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-12-13 13:53:48 +03:00
conf.Load(*confPath, *confIP, *confPort, *confServer, *confLogLevel, *confStaticServer, *confContext, *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-12-14 19:24:36 +03:00
// Main.
func main() {
runtime.GOMAXPROCS(conf.Wide.MaxProcs)
initMime()
// IDE
http.HandleFunc(conf.Wide.Context+"/", handlerGzWrapper(indexHandler))
http.HandleFunc(conf.Wide.Context+"/start", handlerWrapper(startHandler))
http.HandleFunc(conf.Wide.Context+"/about", handlerWrapper(aboutHandler))
http.HandleFunc(conf.Wide.Context+"/keyboard_shortcuts", handlerWrapper(keyboardShortcutsHandler))
// static resources
http.Handle(conf.Wide.Context+"/static/", http.StripPrefix(conf.Wide.Context+"/static/", http.FileServer(http.Dir("static"))))
serveSingle("/favicon.ico", "./static/favicon.ico")
// workspaces
for _, user := range conf.Wide.Users {
http.Handle(conf.Wide.Context+"/workspace/"+user.Name+"/",
http.StripPrefix(conf.Wide.Context+"/workspace/"+user.Name+"/", http.FileServer(http.Dir(user.GetWorkspace()))))
}
// session
http.HandleFunc(conf.Wide.Context+"/session/ws", handlerWrapper(session.WSHandler))
http.HandleFunc(conf.Wide.Context+"/session/save", handlerWrapper(session.SaveContent))
// run
http.HandleFunc(conf.Wide.Context+"/build", handlerWrapper(output.BuildHandler))
http.HandleFunc(conf.Wide.Context+"/run", handlerWrapper(output.RunHandler))
http.HandleFunc(conf.Wide.Context+"/stop", handlerWrapper(output.StopHandler))
http.HandleFunc(conf.Wide.Context+"/go/test", handlerWrapper(output.GoTestHandler))
http.HandleFunc(conf.Wide.Context+"/go/get", handlerWrapper(output.GoGetHandler))
http.HandleFunc(conf.Wide.Context+"/go/install", handlerWrapper(output.GoInstallHandler))
http.HandleFunc(conf.Wide.Context+"/output/ws", handlerWrapper(output.WSHandler))
// file tree
http.HandleFunc(conf.Wide.Context+"/files", handlerWrapper(file.GetFiles))
http.HandleFunc(conf.Wide.Context+"/file/refresh", handlerWrapper(file.RefreshDirectory))
http.HandleFunc(conf.Wide.Context+"/file", handlerWrapper(file.GetFile))
http.HandleFunc(conf.Wide.Context+"/file/save", handlerWrapper(file.SaveFile))
http.HandleFunc(conf.Wide.Context+"/file/new", handlerWrapper(file.NewFile))
http.HandleFunc(conf.Wide.Context+"/file/remove", handlerWrapper(file.RemoveFile))
http.HandleFunc(conf.Wide.Context+"/file/rename", handlerWrapper(file.RenameFile))
http.HandleFunc(conf.Wide.Context+"/file/search/text", handlerWrapper(file.SearchText))
http.HandleFunc(conf.Wide.Context+"/file/find/name", handlerWrapper(file.Find))
// file export/import
http.HandleFunc(conf.Wide.Context+"/file/zip/new", handlerWrapper(file.CreateZip))
http.HandleFunc(conf.Wide.Context+"/file/zip", handlerWrapper(file.GetZip))
http.HandleFunc(conf.Wide.Context+"/file/upload", handlerWrapper(file.Upload))
// editor
http.HandleFunc(conf.Wide.Context+"/editor/ws", handlerWrapper(editor.WSHandler))
http.HandleFunc(conf.Wide.Context+"/go/fmt", handlerWrapper(editor.GoFmtHandler))
http.HandleFunc(conf.Wide.Context+"/autocomplete", handlerWrapper(editor.AutocompleteHandler))
http.HandleFunc(conf.Wide.Context+"/exprinfo", handlerWrapper(editor.GetExprInfoHandler))
http.HandleFunc(conf.Wide.Context+"/find/decl", handlerWrapper(editor.FindDeclarationHandler))
http.HandleFunc(conf.Wide.Context+"/find/usages", handlerWrapper(editor.FindUsagesHandler))
// shell
http.HandleFunc(conf.Wide.Context+"/shell/ws", handlerWrapper(shell.WSHandler))
http.HandleFunc(conf.Wide.Context+"/shell", handlerWrapper(shell.IndexHandler))
// notification
http.HandleFunc(conf.Wide.Context+"/notification/ws", handlerWrapper(notification.WSHandler))
// user
http.HandleFunc(conf.Wide.Context+"/login", handlerWrapper(session.LoginHandler))
http.HandleFunc(conf.Wide.Context+"/logout", handlerWrapper(session.LogoutHandler))
http.HandleFunc(conf.Wide.Context+"/signup", handlerWrapper(session.SignUpUser))
http.HandleFunc(conf.Wide.Context+"/preference", handlerWrapper(session.PreferenceHandler))
logger.Infof("Wide is running [%s]", conf.Wide.Server+conf.Wide.Context)
err := http.ListenAndServe(conf.Wide.Server, nil)
if err != nil {
logger.Error(err)
}
}
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-12-14 19:24:36 +03:00
if "/" != r.RequestURI {
http.NotFound(w, r)
return
}
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-12-11 10:32:24 +03:00
http.Redirect(w, r, conf.Wide.Context+"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-12-11 10:59:58 +03:00
if "" != conf.Wide.Context {
httpSession.Options.Path = conf.Wide.Context
}
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 {
2014-12-13 13:47:41 +03:00
logger.Warnf("Not found user [%s]", username)
2014-10-30 13:21:44 +03:00
2014-12-11 10:32:24 +03:00
http.Redirect(w, r, conf.Wide.Context+"login", http.StatusFound)
2014-10-30 13:21:44 +03:00
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-12-13 13:47:41 +03:00
logger.Debugf("User [%s] has [%d] sessions", username, len(wideSessions))
2014-09-17 07:17:25 +04:00
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 {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-08-18 17:45:43 +04:00
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-12-11 10:32:24 +03:00
http.Redirect(w, r, conf.Wide.Context+"login", http.StatusFound)
2014-10-19 14:41:01 +04:00
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
2014-12-11 10:59:58 +03:00
if "" != conf.Wide.Context {
httpSession.Options.Path = conf.Wide.Context
}
2014-10-19 14:41:01 +04:00
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 {
2014-12-13 13:47:41 +03:00
logger.Errorf("Session [%s] not found", sid)
2014-11-02 10:44:24 +03:00
}
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 {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-19 14:41:01 +04:00
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 {
2014-12-11 10:32:24 +03:00
http.Redirect(w, r, conf.Wide.Context+"login", http.StatusFound)
2014-10-23 17:43:35 +04:00
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
2014-12-11 10:59:58 +03:00
if "" != conf.Wide.Context {
httpSession.Options.Path = conf.Wide.Context
}
2014-10-23 17:43:35 +04:00
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 {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-21 18:48:33 +04:00
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 {
2014-12-11 10:32:24 +03:00
http.Redirect(w, r, conf.Wide.Context+"login", http.StatusFound)
2014-10-23 17:43:35 +04:00
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
2014-12-11 10:59:58 +03:00
if "" != conf.Wide.Context {
httpSession.Options.Path = conf.Wide.Context
}
2014-10-23 17:43:35 +04:00
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 {
2014-12-13 13:47:41 +03:00
logger.Error(err)
2014-10-20 18:27:19 +04:00
http.Error(w, err.Error(), 500)
return
}
t.Execute(w, model)
}
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-12-09 06:51:14 +03:00
// handlerGzWrapper wraps the HTTP Handler for some common processes.
//
// 1. panic recover
// 2. gzip response
// 3. request stopwatch
// 4. i18n
func handlerGzWrapper(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
handler := panicRecover(f)
handler = gzipWrapper(handler)
handler = stopwatch(handler)
handler = i18nLoad(handler)
return handler
}
// gzipWrapper wraps the process with response gzip.
func gzipWrapper(f func(http.ResponseWriter, *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
f(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
f(gzr, r)
}
}
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() {
2014-12-15 10:50:52 +03:00
logger.Tracef("[%s, %s, %s]", r.RequestURI, time.Since(start), r.Method)
2014-09-24 10:36:34 +04:00
}()
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")
}
2014-12-09 06:51:14 +03:00
// gzipResponseWriter represents a gzip response writer.
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
// Write writes response with appropriate 'Content-Type'.
func (w gzipResponseWriter) Write(b []byte) (int, error) {
if "" == w.Header().Get("Content-Type") {
// If no content type, apply sniffing algorithm to un-gzipped body.
w.Header().Set("Content-Type", http.DetectContentType(b))
}
return w.Writer.Write(b)
}