wide/main.go

442 lines
13 KiB
Go
Raw Normal View History

2019-05-17 06:28:50 +03:00
// Copyright (c) 2014-present, 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
//
2018-03-12 07:28:33 +03:00
// https://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"
"mime"
2014-09-07 13:07:25 +04:00
"net/http"
2014-12-24 05:18:32 +03:00
_ "net/http/pprof"
"os"
2017-05-04 13:05:01 +03:00
"os/signal"
2014-09-07 13:07:25 +04:00
"runtime"
"strings"
2017-05-04 13:05:01 +03:00
"syscall"
2014-09-24 10:36:34 +04:00
"time"
2014-09-07 13:07:25 +04:00
2019-12-01 15:21:00 +03:00
"github.com/88250/gulu"
"github.com/88250/wide/conf"
"github.com/88250/wide/editor"
"github.com/88250/wide/event"
"github.com/88250/wide/file"
"github.com/88250/wide/i18n"
"github.com/88250/wide/notification"
"github.com/88250/wide/output"
"github.com/88250/wide/playground"
"github.com/88250/wide/session"
2014-08-18 17:45:43 +04:00
)
2014-12-13 13:47:41 +03:00
// Logger
2019-05-24 16:04:25 +03:00
var logger *gulu.Logger
2014-12-13 13:47:41 +03:00
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")
2019-05-16 20:41:04 +03:00
confData := flag.String("data", "", "path of data dir")
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")
2022-08-09 04:56:45 +03:00
confReadOnly := flag.String("readonly", "", "this will overrite Wide.ReadOnly if specified")
2019-05-23 13:28:11 +03:00
confSiteStatCode := flag.String("site_stat_code", "", "this will overrite Wide.SiteStatCode 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
2019-05-24 16:04:25 +03:00
gulu.Log.SetLevel("warn")
logger = gulu.Log.NewLogger(os.Stdout)
2014-12-13 13:47:41 +03:00
2019-05-24 16:04:25 +03:00
//wd := gulu.OS.Pwd()
//if strings.HasPrefix(wd, os.TempDir()) {
// 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()
2022-08-09 04:56:45 +03:00
conf.Load(*confPath, *confData, *confServer, *confLogLevel, *confReadOnly, template.HTML(*confSiteStatCode))
2014-11-16 17:25:27 +03:00
2014-09-22 19:13:07 +04:00
conf.FixedTimeCheckEnv()
session.FixedTimeSave()
2014-09-23 17:03:44 +04:00
session.FixedTimeRelease()
session.FixedTimeReport()
2015-08-04 16:37:14 +03:00
2019-05-29 13:48:08 +03:00
logger.Debug("host [" + runtime.Version() + ", " + runtime.GOOS + "_" + runtime.GOARCH + "]")
2014-09-02 18:57:30 +04:00
}
2014-12-14 19:24:36 +03:00
// Main.
func main() {
initMime()
2017-05-04 13:05:01 +03:00
handleSignal()
2014-12-14 19:24:36 +03:00
// IDE
2019-05-16 19:41:52 +03:00
http.HandleFunc("/", handlerGzWrapper(indexHandler))
http.HandleFunc("/start", handlerWrapper(startHandler))
http.HandleFunc("/about", handlerWrapper(aboutHandler))
http.HandleFunc("/keyboard_shortcuts", handlerWrapper(keyboardShortcutsHandler))
2014-12-14 19:24:36 +03:00
// static resources
2019-05-16 19:41:52 +03:00
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
2019-05-17 08:00:59 +03:00
http.Handle("/static/users/", http.StripPrefix("/static/", http.FileServer(http.Dir(conf.Wide.Data+"/static"))))
2019-05-17 05:25:11 +03:00
serveSingle("/favicon.ico", "./static/images/favicon.png")
2014-12-14 19:24:36 +03:00
2019-05-16 19:41:52 +03:00
// oauth
2019-12-13 15:50:35 +03:00
http.HandleFunc("/login/redirect", session.LoginRedirectHandler)
http.HandleFunc("/login/callback", session.LoginCallbackHandler)
2019-05-16 19:41:52 +03:00
2014-12-14 19:24:36 +03:00
// session
2019-05-16 19:41:52 +03:00
http.HandleFunc("/session/ws", handlerWrapper(session.WSHandler))
http.HandleFunc("/session/save", handlerWrapper(session.SaveContentHandler))
2014-12-14 19:24:36 +03:00
// run
2019-05-16 19:41:52 +03:00
http.HandleFunc("/build", handlerWrapper(output.BuildHandler))
http.HandleFunc("/run", handlerWrapper(output.RunHandler))
http.HandleFunc("/stop", handlerWrapper(output.StopHandler))
http.HandleFunc("/go/test", handlerWrapper(output.GoTestHandler))
http.HandleFunc("/go/vet", handlerWrapper(output.GoVetHandler))
http.HandleFunc("/go/install", handlerWrapper(output.GoInstallHandler))
http.HandleFunc("/output/ws", handlerWrapper(output.WSHandler))
2014-12-14 19:24:36 +03:00
2015-08-04 16:55:00 +03:00
// cross-compilation
2019-05-16 19:41:52 +03:00
http.HandleFunc("/cross", handlerWrapper(output.CrossCompilationHandler))
2015-08-04 16:37:14 +03:00
2014-12-14 19:24:36 +03:00
// file tree
2019-05-16 19:41:52 +03:00
http.HandleFunc("/files", handlerWrapper(file.GetFilesHandler))
http.HandleFunc("/file/refresh", handlerWrapper(file.RefreshDirectoryHandler))
http.HandleFunc("/file", handlerWrapper(file.GetFileHandler))
http.HandleFunc("/file/save", handlerWrapper(file.SaveFileHandler))
http.HandleFunc("/file/new", handlerWrapper(file.NewFileHandler))
http.HandleFunc("/file/remove", handlerWrapper(file.RemoveFileHandler))
http.HandleFunc("/file/rename", handlerWrapper(file.RenameFileHandler))
http.HandleFunc("/file/search/text", handlerWrapper(file.SearchTextHandler))
http.HandleFunc("/file/find/name", handlerWrapper(file.FindHandler))
2014-12-14 19:24:36 +03:00
2014-12-29 09:16:14 +03:00
// outline
2019-05-16 19:41:52 +03:00
http.HandleFunc("/outline", handlerWrapper(file.GetOutlineHandler))
2014-12-29 09:16:14 +03:00
2019-05-17 07:17:58 +03:00
// file export
2019-05-16 19:41:52 +03:00
http.HandleFunc("/file/zip/new", handlerWrapper(file.CreateZipHandler))
http.HandleFunc("/file/zip", handlerWrapper(file.GetZipHandler))
2014-12-14 19:24:36 +03:00
// editor
2019-05-16 19:41:52 +03:00
http.HandleFunc("/go/fmt", handlerWrapper(editor.GoFmtHandler))
http.HandleFunc("/autocomplete", handlerWrapper(editor.AutocompleteHandler))
http.HandleFunc("/exprinfo", handlerWrapper(editor.GetExprInfoHandler))
http.HandleFunc("/find/decl", handlerWrapper(editor.FindDeclarationHandler))
http.HandleFunc("/find/usages", handlerWrapper(editor.FindUsagesHandler))
2014-12-14 19:24:36 +03:00
// notification
2019-05-16 19:41:52 +03:00
http.HandleFunc("/notification/ws", handlerWrapper(notification.WSHandler))
2014-12-14 19:24:36 +03:00
// user
2019-05-17 05:34:09 +03:00
http.HandleFunc("/login", handlerWrapper(session.LoginHandler))
2019-05-16 19:41:52 +03:00
http.HandleFunc("/logout", handlerWrapper(session.LogoutHandler))
http.HandleFunc("/preference", handlerWrapper(session.PreferenceHandler))
2014-12-14 19:24:36 +03:00
2015-02-13 04:59:51 +03:00
// playground
2019-05-16 19:41:52 +03:00
http.HandleFunc("/playground", handlerWrapper(playground.IndexHandler))
http.HandleFunc("/playground/", handlerWrapper(playground.IndexHandler))
http.HandleFunc("/playground/ws", handlerWrapper(playground.WSHandler))
http.HandleFunc("/playground/save", handlerWrapper(playground.SaveHandler))
http.HandleFunc("/playground/build", handlerWrapper(playground.BuildHandler))
http.HandleFunc("/playground/run", handlerWrapper(playground.RunHandler))
http.HandleFunc("/playground/stop", handlerWrapper(playground.StopHandler))
http.HandleFunc("/playground/autocomplete", handlerWrapper(playground.AutocompleteHandler))
logger.Infof("Wide is running [%s]", conf.Wide.Server)
2019-05-17 07:42:16 +03:00
err := http.ListenAndServe("127.0.0.1:7070", nil)
2014-12-14 19:24:36 +03:00
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) {
2019-05-16 19:41:52 +03:00
if "/" != r.RequestURI {
http.Redirect(w, r, "/", http.StatusFound)
2014-12-14 19:24:36 +03:00
return
}
2019-05-16 19:41:52 +03:00
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
2014-09-16 19:58:52 +04:00
if httpSession.IsNew {
2019-05-16 20:41:04 +03: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
2019-05-16 18:17:25 +03:00
uid := httpSession.Values["uid"].(string)
if "playground" == uid { // reserved user for Playground
2019-05-16 20:41:04 +03:00
http.Redirect(w, r, "/login", http.StatusFound)
2015-02-13 04:59:51 +03:00
return
}
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
2019-05-16 18:17:25 +03:00
user := conf.GetUser(uid)
2014-10-30 13:21:44 +03:00
if nil == user {
2019-05-16 20:41:04 +03:00
http.Redirect(w, r, "/login", http.StatusFound)
2014-10-30 13:21:44 +03:00
return
}
locale := user.Locale
2014-09-24 07:00:33 +04:00
2019-05-16 18:17:25 +03:00
wideSessions := session.WideSessions.GetByUserId(uid)
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,
2019-05-16 18:17:25 +03:00
"uid": uid, "sid": session.WideSessions.GenId(), "latestSessionContent": user.LatestSessionContent,
2014-11-30 05:18:17 +03:00
"pathSeparator": conf.PathSeparator, "codeMirrorVer": conf.CodeMirrorVer,
2019-05-29 13:48:08 +03:00
"user": user, "editorThemes": conf.GetEditorThemes(), "crossPlatforms": []string{"darwin_amd64", "linux_amd64", "windows_amd64"}}
2014-09-23 18:29:53 +04:00
2019-05-16 18:17:25 +03:00
logger.Debugf("User [%s] has [%d] sessions", uid, 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)
2019-05-17 05:34:09 +03:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2014-08-18 17:45:43 +04:00
return
}
2014-08-25 18:17:02 +04:00
t.Execute(w, model)
2014-08-18 17:45:43 +04:00
}
2017-05-04 13:05:01 +03:00
// handleSignal handles system signal for graceful shutdown.
func handleSignal() {
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
s := <-c
logger.Tracef("Got signal [%s]", s)
session.SaveOnlineUsers()
logger.Tracef("Saved all online user, exit")
os.Exit(0)
}()
}
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) {
2019-05-16 19:41:52 +03:00
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
2014-10-19 14:41:01 +04:00
if httpSession.IsNew {
2019-05-16 20:41:04 +03:00
http.Redirect(w, r, "/s", http.StatusFound)
2014-10-19 14:41:01 +04:00
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
httpSession.Save(r, w)
2019-05-16 18:17:25 +03:00
uid := httpSession.Values["uid"].(string)
2019-12-13 15:54:55 +03:00
user := conf.GetUser(uid)
locale := user.Locale
2019-05-16 18:17:25 +03:00
userWorkspace := conf.GetUserWorkspace(uid)
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,
2019-12-13 15:54:55 +03:00
"uid": uid, "workspace": userWorkspace, "ver": conf.WideVersion, "sid": sid, "username": user.Name}
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)
2019-05-17 05:34:09 +03:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2014-10-19 14:41:01 +04:00
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) {
2019-05-16 19:41:52 +03:00
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
2014-10-23 17:43:35 +04:00
if httpSession.IsNew {
2019-05-16 20:41:04 +03:00
http.Redirect(w, r, "/login", http.StatusFound)
2014-10-23 17:43:35 +04:00
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
httpSession.Save(r, w)
2019-05-16 18:17:25 +03:00
uid := httpSession.Values["uid"].(string)
locale := conf.GetUser(uid).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)
2019-05-17 05:34:09 +03:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2014-10-21 18:48:33 +04:00
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) {
2019-05-16 19:41:52 +03:00
httpSession, _ := session.HTTPSession.Get(r, session.CookieName)
2014-10-23 17:43:35 +04:00
if httpSession.IsNew {
2019-05-16 19:41:52 +03:00
http.Redirect(w, r, "/login", http.StatusFound)
2014-10-23 17:43:35 +04:00
return
}
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
httpSession.Save(r, w)
2019-05-16 18:17:25 +03:00
uid := httpSession.Values["uid"].(string)
locale := conf.GetUser(uid).Locale
2014-10-23 17:43:35 +04: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)
2019-05-17 05:34:09 +03:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2014-10-20 18:27:19 +04:00
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) {
2019-08-06 07:04:27 +03:00
defer gulu.Panic.Recover(nil)
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)
}