summaryrefslogtreecommitdiffstats
path: root/web.go
diff options
context:
space:
mode:
authorMeaglith Ma <genedna@gmail.com>2014-04-23 12:29:53 +0800
committerMeaglith Ma <genedna@gmail.com>2014-04-23 12:29:53 +0800
commitee7bfe2ebe3a453beff5e8d4c1436061d321acfe (patch)
tree02575fe703fdcfaa2bd6450b852734d9305c9ce6 /web.go
parentb270b34c98b10b0e4807048890e883b6b06a6461 (diff)
parentf0cdf30134e62be6bf5924735a6145769e495cfc (diff)
downloadgitea-ee7bfe2ebe3a453beff5e8d4c1436061d321acfe.tar.gz
gitea-ee7bfe2ebe3a453beff5e8d4c1436061d321acfe.zip
Add memcached and redis Docker supported
Diffstat (limited to 'web.go')
-rw-r--r--web.go115
1 files changed, 70 insertions, 45 deletions
diff --git a/web.go b/web.go
index 5fc3350f1f..d8d5aba10d 100644
--- a/web.go
+++ b/web.go
@@ -8,13 +8,11 @@ import (
"fmt"
"html/template"
"net/http"
+ "os"
"github.com/codegangsta/cli"
"github.com/go-martini/martini"
- // "github.com/martini-contrib/oauth2"
- // "github.com/martini-contrib/sessions"
-
- "github.com/gogits/binding"
+ qlog "github.com/qiniu/log"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/avatar"
@@ -51,34 +49,26 @@ func newMartini() *martini.ClassicMartini {
}
func runWeb(*cli.Context) {
- fmt.Println("Server is running...")
routers.GlobalInit()
- log.Info("%s %s", base.AppName, base.AppVer)
m := newMartini()
// Middlewares.
m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
-
- // scope := "https://api.github.com/user"
- // oauth2.PathCallback = "/oauth2callback"
- // m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
- // m.Use(oauth2.Github(&oauth2.Options{
- // ClientId: "09383403ff2dc16daaa1",
- // ClientSecret: "5f6e7101d30b77952aab22b75eadae17551ea6b5",
- // RedirectURL: base.AppUrl + oauth2.PathCallback,
- // Scopes: []string{scope},
- // }))
-
m.Use(middleware.InitContext())
reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
+ ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{DisableCsrf: true})
+
reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
+ bindIgnErr := middleware.BindIgnErr
+
// Routers.
m.Get("/", ignSignIn, routers.Home)
- m.Any("/install", binding.BindIgnErr(auth.InstallForm{}), routers.Install)
+ m.Get("/install", bindIgnErr(auth.InstallForm{}), routers.Install)
+ m.Post("/install", bindIgnErr(auth.InstallForm{}), routers.InstallPost)
m.Get("/issues", reqSignIn, user.Issues)
m.Get("/pulls", reqSignIn, user.Pulls)
m.Get("/stars", reqSignIn, user.Stars)
@@ -89,33 +79,49 @@ func runWeb(*cli.Context) {
})
avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
+ os.MkdirAll("public/img/avatar/", os.ModePerm)
m.Get("/avatar/:hash", avt.ServeHTTP)
m.Group("/user", func(r martini.Router) {
- // r.Any("/login/github", user.SocialSignIn)
- r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
- r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
+ r.Get("/login", user.SignIn)
+ r.Post("/login", bindIgnErr(auth.LogInForm{}), user.SignInPost)
+ r.Get("/login/:name", user.SocialSignIn)
+ r.Get("/sign_up", user.SignUp)
+ r.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
+ r.Get("/reset_password", user.ResetPasswd)
+ r.Post("/reset_password", user.ResetPasswdPost)
}, reqSignOut)
m.Group("/user", func(r martini.Router) {
- r.Any("/logout", user.SignOut)
- r.Any("/delete", user.Delete)
- r.Any("/setting", binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
+ r.Get("/delete", user.Delete)
+ r.Post("/delete", user.DeletePost)
+ r.Get("/setting", user.Setting)
+ r.Post("/setting", bindIgnErr(auth.UpdateProfileForm{}), user.SettingPost)
}, reqSignIn)
m.Group("/user", func(r martini.Router) {
- r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
- r.Get("/activate", user.Activate)
+ r.Get("/feeds", middleware.Bind(auth.FeedsForm{}), user.Feeds)
+ r.Any("/activate", user.Activate)
+ r.Get("/email2user", user.Email2User)
+ r.Get("/forget_password", user.ForgotPasswd)
+ r.Post("/forget_password", user.ForgotPasswdPost)
+ r.Get("/logout", user.SignOut)
})
-
m.Group("/user/setting", func(r martini.Router) {
- r.Any("/password", binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
- r.Any("/ssh", binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
- r.Any("/notification", user.SettingNotification)
- r.Any("/security", user.SettingSecurity)
+ r.Get("/social", user.SettingSocial)
+ r.Get("/password", user.SettingPassword)
+ r.Post("/password", bindIgnErr(auth.UpdatePasswdForm{}), user.SettingPasswordPost)
+ r.Any("/ssh", bindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
+ r.Get("/notification", user.SettingNotification)
+ r.Get("/security", user.SettingSecurity)
}, reqSignIn)
m.Get("/user/:username", ignSignIn, user.Profile)
- m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
+ m.Group("/repo", func(r martini.Router) {
+ m.Get("/create", repo.Create)
+ m.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
+ m.Get("/migrate", repo.Migrate)
+ m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
+ }, reqSignIn)
adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
@@ -126,9 +132,11 @@ func runWeb(*cli.Context) {
r.Get("/config", admin.Config)
}, adminReq)
m.Group("/admin/users", func(r martini.Router) {
- r.Any("/new", binding.BindIgnErr(auth.RegisterForm{}), admin.NewUser)
- r.Any("/:userid", binding.BindIgnErr(auth.AdminEditUserForm{}), admin.EditUser)
- r.Any("/:userid/delete", admin.DeleteUser)
+ r.Get("/new", admin.NewUser)
+ r.Post("/new", bindIgnErr(auth.RegisterForm{}), admin.NewUserPost)
+ r.Get("/:userid", admin.EditUser)
+ r.Post("/:userid", bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
+ r.Get("/:userid/delete", admin.DeleteUser)
}, adminReq)
if martini.Env == martini.Dev {
@@ -136,18 +144,23 @@ func runWeb(*cli.Context) {
}
m.Group("/:username/:reponame", func(r martini.Router) {
- r.Post("/settings", repo.SettingPost)
r.Get("/settings", repo.Setting)
+ r.Post("/settings", repo.SettingPost)
r.Get("/action/:action", repo.Action)
- r.Any("/issues/new", binding.BindIgnErr(auth.CreateIssueForm{}), repo.CreateIssue)
- r.Post("/issues/:index", binding.BindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
+ r.Get("/issues/new", repo.CreateIssue)
+ r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
+ r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
r.Post("/comment/:action", repo.Comment)
+ r.Get("/releases/new", repo.ReleasesNew)
}, reqSignIn, middleware.RepoAssignment(true))
m.Group("/:username/:reponame", func(r martini.Router) {
+ r.Post("/releases/new", bindIgnErr(auth.NewReleaseForm{}), repo.ReleasesNewPost)
+ }, reqSignIn, middleware.RepoAssignment(true, true))
+
+ m.Group("/:username/:reponame", func(r martini.Router) {
r.Get("/issues", repo.Issues)
r.Get("/issues/:index", repo.ViewIssue)
- r.Get("/releases", repo.Releases)
r.Get("/pulls", repo.Pulls)
r.Get("/branches", repo.Branches)
}, ignSignIn, middleware.RepoAssignment(true))
@@ -157,24 +170,36 @@ func runWeb(*cli.Context) {
r.Get("/src/:branchname/**", repo.Single)
r.Get("/raw/:branchname/**", repo.SingleDownload)
r.Get("/commits/:branchname", repo.Commits)
+ r.Get("/commits/:branchname/search", repo.SearchCommits)
r.Get("/commit/:branchname", repo.Diff)
r.Get("/commit/:branchname/**", repo.Diff)
+ r.Get("/releases", repo.Releases)
+ r.Get("/archive/:branchname/:reponame.zip", repo.ZipDownload)
}, ignSignIn, middleware.RepoAssignment(true, true))
m.Group("/:username", func(r martini.Router) {
- r.Any("/:reponame/**", repo.Http)
r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
- }, ignSignIn)
+ r.Any("/:reponame/**", repo.Http)
+ }, ignSignInAndCsrf)
// Not found handler.
m.NotFound(routers.NotFound)
+ protocol := base.Cfg.MustValue("server", "PROTOCOL", "http")
listenAddr := fmt.Sprintf("%s:%s",
base.Cfg.MustValue("server", "HTTP_ADDR"),
base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
- log.Info("Listen: %s", listenAddr)
- if err := http.ListenAndServe(listenAddr, m); err != nil {
- fmt.Println(err.Error())
- //log.Critical(err.Error()) // not working now
+
+ if protocol == "http" {
+ log.Info("Listen: http://%s", listenAddr)
+ if err := http.ListenAndServe(listenAddr, m); err != nil {
+ qlog.Error(err.Error())
+ }
+ } else if protocol == "https" {
+ log.Info("Listen: https://%s", listenAddr)
+ if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
+ base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
+ qlog.Error(err.Error())
+ }
}
}