wide/main.go

92 lines
2.2 KiB
Go
Raw Normal View History

2014-08-18 17:45:43 +04:00
package main
import (
2014-08-18 17:51:03 +04:00
"github.com/b3log/wide/conf"
"github.com/b3log/wide/editor"
"github.com/b3log/wide/file"
2014-08-25 18:17:02 +04:00
"github.com/b3log/wide/i18n"
2014-08-18 17:51:03 +04:00
"github.com/b3log/wide/output"
"github.com/b3log/wide/shell"
2014-08-31 13:39:39 +04:00
"github.com/b3log/wide/user"
2014-08-18 17:45:43 +04:00
"github.com/golang/glog"
"html/template"
"math/rand"
"net/http"
"strconv"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
2014-09-01 06:47:59 +04:00
model := map[string]interface{}{"Wide": conf.Wide, "i18n": i18n.GetLangs(r), "locale": i18n.GetLocale(r)}
2014-08-25 18:17:02 +04:00
2014-08-31 13:39:39 +04:00
session, _ := user.Session.Get(r, "wide-session")
2014-08-18 17:45:43 +04:00
2014-09-01 14:55:11 +04:00
if session.IsNew {
// TODO: 以 admin 作为用户登录
name := conf.Wide.Users[0].Name
glog.Infof("[%s] logged in", name)
session.Values["username"] = name
session.Values["id"] = strconv.Itoa(rand.Int())
}
2014-08-18 17:45:43 +04:00
session.Save(r, w)
2014-09-01 06:47:59 +04:00
t, err := template.ParseFiles("view/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
}
func main() {
2014-08-31 14:50:38 +04:00
conf.Load()
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-09-01 17:40:10 +04:00
// 库资源
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data"))))
2014-09-01 06:47:59 +04:00
// IDE 首页
2014-08-18 17:45:43 +04:00
http.HandleFunc("/", indexHandler)
2014-09-01 06:47:59 +04:00
// 运行相关
2014-08-22 07:57:05 +04:00
http.HandleFunc("/build", output.BuildHandler)
2014-08-18 17:45:43 +04:00
http.HandleFunc("/run", output.RunHandler)
http.HandleFunc("/output/ws", output.WSHandler)
2014-09-01 06:47:59 +04:00
// 文件树
2014-08-18 17:45:43 +04:00
http.HandleFunc("/files", file.GetFiles)
http.HandleFunc("/file", file.GetFile)
http.HandleFunc("/file/save", file.SaveFile)
http.HandleFunc("/file/new", file.NewFile)
http.HandleFunc("/file/remove", file.RemoveFile)
2014-09-01 06:47:59 +04:00
// 编辑器
2014-08-18 17:45:43 +04:00
http.HandleFunc("/editor/ws", editor.WSHandler)
http.HandleFunc("/fmt", editor.FmtHandler)
2014-09-01 06:47:59 +04:00
http.HandleFunc("/autocomplete", editor.AutocompleteHandler)
2014-08-18 17:45:43 +04:00
2014-09-01 06:47:59 +04:00
// Shell
2014-08-18 17:45:43 +04:00
http.HandleFunc("/shell/ws", shell.WSHandler)
2014-09-01 06:47:59 +04:00
// 用户
2014-08-31 14:50:38 +04:00
http.HandleFunc("/user/new", user.AddUser)
http.HandleFunc("/user/repos/init", user.InitGitRepos)
2014-09-01 06:47:59 +04:00
// 文档
http.Handle("/doc/", http.StripPrefix("/doc/", http.FileServer(http.Dir("doc"))))
2014-08-31 13:23:14 +04:00
glog.Infof("Wide is running [%s]", conf.Wide.Server)
2014-08-18 17:45:43 +04:00
err := http.ListenAndServe(conf.Wide.Server, nil)
if err != nil {
glog.Fatal(err)
}
}