You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "html/template"
  8. "net/http"
  9. "github.com/codegangsta/cli"
  10. "github.com/go-martini/martini"
  11. // "github.com/martini-contrib/oauth2"
  12. // "github.com/martini-contrib/sessions"
  13. "github.com/gogits/binding"
  14. "github.com/gogits/gogs/modules/auth"
  15. "github.com/gogits/gogs/modules/avatar"
  16. "github.com/gogits/gogs/modules/base"
  17. "github.com/gogits/gogs/modules/log"
  18. "github.com/gogits/gogs/modules/middleware"
  19. "github.com/gogits/gogs/routers"
  20. "github.com/gogits/gogs/routers/admin"
  21. "github.com/gogits/gogs/routers/api/v1"
  22. "github.com/gogits/gogs/routers/dev"
  23. "github.com/gogits/gogs/routers/repo"
  24. "github.com/gogits/gogs/routers/user"
  25. )
  26. var CmdWeb = cli.Command{
  27. Name: "web",
  28. Usage: "Gogs web server",
  29. Description: `
  30. gogs web server is the only thing you need to run,
  31. and it takes care of all the other things for you`,
  32. Action: runWeb,
  33. Flags: []cli.Flag{},
  34. }
  35. func newMartini() *martini.ClassicMartini {
  36. r := martini.NewRouter()
  37. m := martini.New()
  38. m.Use(middleware.Logger())
  39. m.Use(martini.Recovery())
  40. m.Use(martini.Static("public"))
  41. m.MapTo(r, (*martini.Routes)(nil))
  42. m.Action(r.Handle)
  43. return &martini.ClassicMartini{m, r}
  44. }
  45. func runWeb(*cli.Context) {
  46. fmt.Println("Server is running...")
  47. routers.GlobalInit()
  48. log.Info("%s %s", base.AppName, base.AppVer)
  49. m := newMartini()
  50. // Middlewares.
  51. m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
  52. // scope := "https://api.github.com/user"
  53. // oauth2.PathCallback = "/oauth2callback"
  54. // m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
  55. // m.Use(oauth2.Github(&oauth2.Options{
  56. // ClientId: "09383403ff2dc16daaa1",
  57. // ClientSecret: "5f6e7101d30b77952aab22b75eadae17551ea6b5",
  58. // RedirectURL: base.AppUrl + oauth2.PathCallback,
  59. // Scopes: []string{scope},
  60. // }))
  61. m.Use(middleware.InitContext())
  62. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  63. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
  64. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  65. // Routers.
  66. m.Get("/", ignSignIn, routers.Home)
  67. m.Any("/install", binding.BindIgnErr(auth.InstallForm{}), routers.Install)
  68. m.Get("/issues", reqSignIn, user.Issues)
  69. m.Get("/pulls", reqSignIn, user.Pulls)
  70. m.Get("/stars", reqSignIn, user.Stars)
  71. m.Get("/help", routers.Help)
  72. m.Group("/api/v1", func(r martini.Router) {
  73. r.Post("/markdown", v1.Markdown)
  74. })
  75. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  76. m.Get("/avatar/:hash", avt.ServeHTTP)
  77. m.Group("/user", func(r martini.Router) {
  78. // r.Any("/login/github", user.SocialSignIn)
  79. r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
  80. r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
  81. }, reqSignOut)
  82. m.Group("/user", func(r martini.Router) {
  83. r.Any("/logout", user.SignOut)
  84. r.Any("/delete", user.Delete)
  85. r.Any("/setting", binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
  86. }, reqSignIn)
  87. m.Group("/user", func(r martini.Router) {
  88. r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  89. r.Get("/activate", user.Activate)
  90. })
  91. m.Group("/user/setting", func(r martini.Router) {
  92. r.Any("/password", binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
  93. r.Any("/ssh", binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
  94. r.Any("/notification", user.SettingNotification)
  95. r.Any("/security", user.SettingSecurity)
  96. }, reqSignIn)
  97. m.Get("/user/:username", ignSignIn, user.Profile)
  98. m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
  99. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  100. m.Get("/admin", adminReq, admin.Dashboard)
  101. m.Group("/admin", func(r martini.Router) {
  102. r.Get("/users", admin.Users)
  103. r.Get("/repos", admin.Repositories)
  104. r.Get("/config", admin.Config)
  105. }, adminReq)
  106. m.Group("/admin/users", func(r martini.Router) {
  107. r.Any("/new", binding.BindIgnErr(auth.RegisterForm{}), admin.NewUser)
  108. r.Any("/:userid", binding.BindIgnErr(auth.AdminEditUserForm{}), admin.EditUser)
  109. r.Any("/:userid/delete", admin.DeleteUser)
  110. }, adminReq)
  111. if martini.Env == martini.Dev {
  112. m.Get("/template/**", dev.TemplatePreview)
  113. }
  114. m.Group("/:username/:reponame", func(r martini.Router) {
  115. r.Post("/settings", repo.SettingPost)
  116. r.Get("/settings", repo.Setting)
  117. r.Get("/action/:action", repo.Action)
  118. r.Any("/issues/new", binding.BindIgnErr(auth.CreateIssueForm{}), repo.CreateIssue)
  119. r.Post("/issues/:index", binding.BindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  120. r.Post("/comment/:action", repo.Comment)
  121. }, reqSignIn, middleware.RepoAssignment(true))
  122. m.Group("/:username/:reponame", func(r martini.Router) {
  123. r.Get("/issues", repo.Issues)
  124. r.Get("/issues/:index", repo.ViewIssue)
  125. r.Get("/releases", repo.Releases)
  126. r.Get("/pulls", repo.Pulls)
  127. r.Get("/branches", repo.Branches)
  128. }, ignSignIn, middleware.RepoAssignment(true))
  129. m.Group("/:username/:reponame", func(r martini.Router) {
  130. r.Get("/src/:branchname", repo.Single)
  131. r.Get("/src/:branchname/**", repo.Single)
  132. r.Get("/raw/:branchname/**", repo.SingleDownload)
  133. r.Get("/commits/:branchname", repo.Commits)
  134. r.Get("/commit/:branchname", repo.Diff)
  135. r.Get("/commit/:branchname/**", repo.Diff)
  136. }, ignSignIn, middleware.RepoAssignment(true, true))
  137. m.Group("/:username", func(r martini.Router) {
  138. r.Any("/:reponame/**", repo.Http)
  139. r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
  140. }, ignSignIn)
  141. // Not found handler.
  142. m.NotFound(routers.NotFound)
  143. protocol := base.Cfg.MustValue("server", "PROTOCOL", "http")
  144. listenAddr := fmt.Sprintf("%s:%s",
  145. base.Cfg.MustValue("server", "HTTP_ADDR"),
  146. base.Cfg.MustValue("server", "HTTP_PORT", "3000"))
  147. if protocol == "http" {
  148. log.Info("Listen: http://%s", listenAddr)
  149. if err := http.ListenAndServe(listenAddr, m); err != nil {
  150. fmt.Println(err.Error())
  151. //log.Critical(err.Error()) // not working now
  152. }
  153. } else if protocol == "https" {
  154. log.Info("Listen: https://%s", listenAddr)
  155. if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"),
  156. base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil {
  157. fmt.Println(err.Error())
  158. }
  159. }
  160. }