2015-01-18 08:59:10 +03:00
|
|
|
// Copyright (c) 2014-2015, b3log.org
|
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-10-24 19:51:24 +04:00
|
|
|
"mime"
|
2014-09-07 13:07:25 +04:00
|
|
|
"net/http"
|
2014-12-24 05:18:32 +03:00
|
|
|
_ "net/http/pprof"
|
2014-11-23 09:45:55 +03:00
|
|
|
"os"
|
2014-09-07 13:07:25 +04:00
|
|
|
"runtime"
|
2014-11-23 09:45:55 +03:00
|
|
|
"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"
|
2015-02-13 04:59:51 +03:00
|
|
|
"github.com/b3log/wide/playground"
|
2015-03-07 19:01:06 +03:00
|
|
|
"github.com/b3log/wide/scm/git"
|
2014-09-17 10:35:48 +04:00
|
|
|
"github.com/b3log/wide/session"
|
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-12-19 10:43:59 +03:00
|
|
|
confIP := flag.String("ip", "", "this will overwrite Wide.IP if specified")
|
|
|
|
confPort := flag.String("port", "", "this will overwrite Wide.Port if specified")
|
2014-11-18 09:04:24 +03:00
|
|
|
confServer := flag.String("server", "", "this will overwrite Wide.Server if specified")
|
2015-09-26 13:07:55 +03:00
|
|
|
confLogLevel := flag.String("log_level", "", "this will overwrite Wide.LogLevel if specified")
|
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-12-19 10:43:59 +03:00
|
|
|
confChannel := flag.String("channel", "", "this will overwrite Wide.Channel 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")
|
2015-02-13 04:59:51 +03:00
|
|
|
confPlayground := flag.String("playground", "", "this will overwrite Wide.Playground if specified")
|
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)
|
|
|
|
|
2014-11-23 09:45:55 +03:00
|
|
|
wd := util.OS.Pwd()
|
|
|
|
if strings.HasPrefix(wd, os.TempDir()) {
|
2015-10-12 08:43:59 +03:00
|
|
|
logger.Error("Don't run Wide in OS' temp directory or with `go run`")
|
2014-11-23 09:45:55 +03:00
|
|
|
|
|
|
|
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,
|
2015-02-13 04:59:51 +03:00
|
|
|
*confPlayground, *confDocker)
|
2014-11-16 17:25:27 +03:00
|
|
|
|
2014-09-22 19:13:07 +04:00
|
|
|
conf.FixedTimeCheckEnv()
|
2014-09-23 17:03:44 +04:00
|
|
|
|
2014-12-19 10:43:59 +03:00
|
|
|
session.FixedTimeSave()
|
2014-09-23 17:03:44 +04:00
|
|
|
session.FixedTimeRelease()
|
2014-11-21 06:19:57 +03:00
|
|
|
|
|
|
|
if *confStat {
|
|
|
|
session.FixedTimeReport()
|
|
|
|
}
|
2015-08-04 16:37:14 +03:00
|
|
|
|
2015-09-26 10:40:05 +03:00
|
|
|
logger.Debug("host ["+runtime.Version()+", "+runtime.GOOS+"_"+runtime.GOARCH+
|
|
|
|
"], cross-compilation ", util.Go.GetCrossPlatforms())
|
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
|
2014-12-19 10:43:59 +03:00
|
|
|
for _, user := range conf.Users {
|
2014-12-14 19:24:36 +03:00
|
|
|
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))
|
2015-03-09 09:16:46 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/session/save", handlerWrapper(session.SaveContentHandler))
|
2014-12-14 19:24:36 +03:00
|
|
|
|
|
|
|
// 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))
|
2014-12-31 13:02:04 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/go/vet", handlerWrapper(output.GoVetHandler))
|
2014-12-14 19:24:36 +03:00
|
|
|
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))
|
|
|
|
|
2015-08-04 16:55:00 +03:00
|
|
|
// cross-compilation
|
2015-08-04 16:37:14 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/cross", handlerWrapper(output.CrossCompilationHandler))
|
|
|
|
|
2014-12-14 19:24:36 +03:00
|
|
|
// file tree
|
2015-03-09 09:16:46 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/files", handlerWrapper(file.GetFilesHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/refresh", handlerWrapper(file.RefreshDirectoryHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file", handlerWrapper(file.GetFileHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/save", handlerWrapper(file.SaveFileHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/new", handlerWrapper(file.NewFileHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/remove", handlerWrapper(file.RemoveFileHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/rename", handlerWrapper(file.RenameFileHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/search/text", handlerWrapper(file.SearchTextHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/find/name", handlerWrapper(file.FindHandler))
|
2014-12-14 19:24:36 +03:00
|
|
|
|
2014-12-29 09:16:14 +03:00
|
|
|
// outline
|
2015-03-09 09:16:46 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/outline", handlerWrapper(file.GetOutlineHandler))
|
2014-12-29 09:16:14 +03:00
|
|
|
|
2014-12-14 19:24:36 +03:00
|
|
|
// file export/import
|
2015-03-09 09:16:46 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/zip/new", handlerWrapper(file.CreateZipHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/zip", handlerWrapper(file.GetZipHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/upload", handlerWrapper(file.UploadHandler))
|
2015-03-21 09:39:26 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/file/decompress", handlerWrapper(file.DecompressHandler))
|
2014-12-14 19:24:36 +03:00
|
|
|
|
|
|
|
// 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
|
2015-02-13 04:59:51 +03:00
|
|
|
// http.HandleFunc(conf.Wide.Context+"/shell/ws", handlerWrapper(shell.WSHandler))
|
|
|
|
// http.HandleFunc(conf.Wide.Context+"/shell", handlerWrapper(shell.IndexHandler))
|
2014-12-14 19:24:36 +03:00
|
|
|
|
|
|
|
// 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))
|
2015-03-09 09:16:46 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/signup", handlerWrapper(session.SignUpUserHandler))
|
2014-12-14 19:24:36 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/preference", handlerWrapper(session.PreferenceHandler))
|
|
|
|
|
2015-02-13 04:59:51 +03:00
|
|
|
// playground
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground", handlerWrapper(playground.IndexHandler))
|
2015-02-13 05:50:14 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground/", handlerWrapper(playground.IndexHandler))
|
2015-02-13 04:59:51 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground/ws", handlerWrapper(playground.WSHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground/save", handlerWrapper(playground.SaveHandler))
|
2015-02-16 12:10:21 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground/short-url", handlerWrapper(playground.ShortURLHandler))
|
2015-02-13 04:59:51 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground/build", handlerWrapper(playground.BuildHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground/run", handlerWrapper(playground.RunHandler))
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground/stop", handlerWrapper(playground.StopHandler))
|
2015-02-15 09:24:49 +03:00
|
|
|
http.HandleFunc(conf.Wide.Context+"/playground/autocomplete", handlerWrapper(playground.AutocompleteHandler))
|
2015-02-13 04:59:51 +03:00
|
|
|
|
2015-03-07 19:01:06 +03:00
|
|
|
// git
|
|
|
|
http.HandleFunc(conf.Wide.Context+"/git/clone", handlerWrapper(git.CloneHandler))
|
|
|
|
|
2014-12-14 19:24:36 +03:00
|
|
|
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 {
|
2015-06-30 04:50:35 +03:00
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2014-12-14 19:24:36 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-02-13 04:59:51 +03:00
|
|
|
username := httpSession.Values["username"].(string)
|
|
|
|
if "playground" == username { // reserved user for Playground
|
|
|
|
http.Redirect(w, r, conf.Wide.Context+"login", http.StatusFound)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-12-19 10:43:59 +03:00
|
|
|
user := conf.GetUser(username)
|
2014-10-30 13:21:44 +03:00
|
|
|
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,
|
2015-09-27 02:36:34 +03:00
|
|
|
"username": username, "sid": session.WideSessions.GenId(), "latestSessionContent": user.LatestSessionContent,
|
2014-11-30 05:18:17 +03:00
|
|
|
"pathSeparator": conf.PathSeparator, "codeMirrorVer": conf.CodeMirrorVer,
|
2015-08-04 16:37:14 +03:00
|
|
|
"user": user, "editorThemes": conf.GetEditorThemes(), "crossPlatforms": util.Go.GetCrossPlatforms()}
|
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-12-19 10:43:59 +03:00
|
|
|
locale := conf.GetUser(username).Locale
|
|
|
|
userWorkspace := conf.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,
|
2015-09-30 06:55:02 +03:00
|
|
|
"username": username, "workspace": userWorkspace, "ver": conf.WideVersion, "sid": sid}
|
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)
|
2014-12-19 10:43:59 +03:00
|
|
|
locale := conf.GetUser(username).Locale
|
2014-10-23 17:43:35 +04:00
|
|
|
|
|
|
|
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)
|
2014-12-19 10:43:59 +03:00
|
|
|
locale := conf.GetUser(username).Locale
|
2014-10-23 17:43:35 +04:00
|
|
|
|
2014-10-29 18:03:03 +03:00
|
|
|
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:51:51 +03:00
|
|
|
logger.Tracef("[%s, %s, %s]", r.Method, r.RequestURI, time.Since(start))
|
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-24 19:51:24 +04:00
|
|
|
|
2014-10-29 09:05:10 +03:00
|
|
|
// initMime initializes mime types.
|
2014-10-24 19:51:24 +04:00
|
|
|
//
|
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.
|
2014-10-24 19:51:24 +04:00
|
|
|
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)
|
|
|
|
}
|