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.

503 lines
17 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
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
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
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 cmd
  5. import (
  6. "crypto/tls"
  7. "fmt"
  8. "html/template"
  9. "io/ioutil"
  10. "net/http"
  11. "net/http/fcgi"
  12. "os"
  13. "path"
  14. "strings"
  15. "github.com/Unknwon/macaron"
  16. "github.com/codegangsta/cli"
  17. "github.com/macaron-contrib/binding"
  18. "github.com/macaron-contrib/cache"
  19. "github.com/macaron-contrib/captcha"
  20. "github.com/macaron-contrib/csrf"
  21. "github.com/macaron-contrib/i18n"
  22. "github.com/macaron-contrib/oauth2"
  23. "github.com/macaron-contrib/session"
  24. "github.com/macaron-contrib/toolbox"
  25. "gopkg.in/ini.v1"
  26. api "github.com/gogits/go-gogs-client"
  27. "github.com/gogits/gogs/models"
  28. "github.com/gogits/gogs/modules/auth"
  29. "github.com/gogits/gogs/modules/auth/apiv1"
  30. "github.com/gogits/gogs/modules/avatar"
  31. "github.com/gogits/gogs/modules/base"
  32. "github.com/gogits/gogs/modules/bindata"
  33. "github.com/gogits/gogs/modules/git"
  34. "github.com/gogits/gogs/modules/log"
  35. "github.com/gogits/gogs/modules/middleware"
  36. "github.com/gogits/gogs/modules/setting"
  37. "github.com/gogits/gogs/routers"
  38. "github.com/gogits/gogs/routers/admin"
  39. "github.com/gogits/gogs/routers/api/v1"
  40. "github.com/gogits/gogs/routers/dev"
  41. "github.com/gogits/gogs/routers/org"
  42. "github.com/gogits/gogs/routers/repo"
  43. "github.com/gogits/gogs/routers/user"
  44. )
  45. var CmdWeb = cli.Command{
  46. Name: "web",
  47. Usage: "Start Gogs web server",
  48. Description: `Gogs web server is the only thing you need to run,
  49. and it takes care of all the other things for you`,
  50. Action: runWeb,
  51. Flags: []cli.Flag{
  52. cli.StringFlag{"port, p", "3000", "Temporary port number to prevent conflict", ""},
  53. cli.StringFlag{"config, c", "custom/conf/app.ini", "Custom configuration file path", ""},
  54. },
  55. }
  56. type VerChecker struct {
  57. ImportPath string
  58. Version func() string
  59. Expected string
  60. }
  61. // checkVersion checks if binary matches the version of templates files.
  62. func checkVersion() {
  63. // Templates.
  64. data, err := ioutil.ReadFile(path.Join(setting.StaticRootPath, "templates/.VERSION"))
  65. if err != nil {
  66. log.Fatal(4, "Fail to read 'templates/.VERSION': %v", err)
  67. }
  68. if string(data) != setting.AppVer {
  69. log.Fatal(4, "Binary and template file version does not match, did you forget to recompile?")
  70. }
  71. // Check dependency version.
  72. checkers := []VerChecker{
  73. {"github.com/Unknwon/macaron", macaron.Version, "0.5.4"},
  74. {"github.com/macaron-contrib/binding", binding.Version, "0.0.6"},
  75. {"github.com/macaron-contrib/cache", cache.Version, "0.0.7"},
  76. {"github.com/macaron-contrib/csrf", csrf.Version, "0.0.3"},
  77. {"github.com/macaron-contrib/i18n", i18n.Version, "0.0.7"},
  78. {"github.com/macaron-contrib/session", session.Version, "0.1.6"},
  79. {"gopkg.in/ini.v1", ini.Version, "1.2.0"},
  80. }
  81. for _, c := range checkers {
  82. ver := strings.Join(strings.Split(c.Version(), ".")[:3], ".")
  83. if git.MustParseVersion(ver).LessThan(git.MustParseVersion(c.Expected)) {
  84. log.Fatal(4, "Package '%s' version is too old(%s -> %s), did you forget to update?", c.ImportPath, ver, c.Expected)
  85. }
  86. }
  87. }
  88. // newMacaron initializes Macaron instance.
  89. func newMacaron() *macaron.Macaron {
  90. m := macaron.New()
  91. m.Use(macaron.Logger())
  92. m.Use(macaron.Recovery())
  93. if setting.EnableGzip {
  94. m.Use(macaron.Gziper())
  95. }
  96. if setting.Protocol == setting.FCGI {
  97. m.SetURLPrefix(setting.AppSubUrl)
  98. }
  99. m.Use(macaron.Static(
  100. path.Join(setting.StaticRootPath, "public"),
  101. macaron.StaticOptions{
  102. SkipLogging: !setting.DisableRouterLog,
  103. },
  104. ))
  105. m.Use(macaron.Static(
  106. setting.AvatarUploadPath,
  107. macaron.StaticOptions{
  108. Prefix: "avatars",
  109. SkipLogging: !setting.DisableRouterLog,
  110. },
  111. ))
  112. m.Use(macaron.Renderer(macaron.RenderOptions{
  113. Directory: path.Join(setting.StaticRootPath, "templates"),
  114. Funcs: []template.FuncMap{base.TemplateFuncs},
  115. IndentJSON: macaron.Env != macaron.PROD,
  116. }))
  117. localeNames, err := bindata.AssetDir("conf/locale")
  118. if err != nil {
  119. log.Fatal(4, "Fail to list locale files: %v", err)
  120. }
  121. localFiles := make(map[string][]byte)
  122. for _, name := range localeNames {
  123. localFiles[name] = bindata.MustAsset("conf/locale/" + name)
  124. }
  125. m.Use(i18n.I18n(i18n.Options{
  126. SubURL: setting.AppSubUrl,
  127. Files: localFiles,
  128. CustomDirectory: path.Join(setting.CustomPath, "conf/locale"),
  129. Langs: setting.Langs,
  130. Names: setting.Names,
  131. Redirect: true,
  132. }))
  133. m.Use(cache.Cacher(cache.Options{
  134. Adapter: setting.CacheAdapter,
  135. AdapterConfig: setting.CacheConn,
  136. Interval: setting.CacheInternal,
  137. }))
  138. m.Use(captcha.Captchaer(captcha.Options{
  139. SubURL: setting.AppSubUrl,
  140. }))
  141. m.Use(session.Sessioner(setting.SessionConfig))
  142. m.Use(csrf.Csrfer(csrf.Options{
  143. Secret: setting.SecretKey,
  144. SetCookie: true,
  145. Header: "X-Csrf-Token",
  146. CookiePath: setting.AppSubUrl,
  147. }))
  148. m.Use(toolbox.Toolboxer(m, toolbox.Options{
  149. HealthCheckFuncs: []*toolbox.HealthCheckFuncDesc{
  150. &toolbox.HealthCheckFuncDesc{
  151. Desc: "Database connection",
  152. Func: models.Ping,
  153. },
  154. },
  155. }))
  156. // OAuth 2.
  157. if setting.OauthService != nil {
  158. for _, info := range setting.OauthService.OauthInfos {
  159. m.Use(oauth2.NewOAuth2Provider(info.Options, info.AuthUrl, info.TokenUrl))
  160. }
  161. }
  162. m.Use(middleware.Contexter())
  163. return m
  164. }
  165. func runWeb(ctx *cli.Context) {
  166. if ctx.IsSet("config") {
  167. setting.CustomConf = ctx.String("config")
  168. }
  169. routers.GlobalInit()
  170. checkVersion()
  171. m := newMacaron()
  172. reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
  173. ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: setting.Service.RequireSignInView})
  174. ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{DisableCsrf: true})
  175. reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})
  176. bind := binding.Bind
  177. bindIgnErr := binding.BindIgnErr
  178. // Routers.
  179. m.Get("/", ignSignIn, routers.Home)
  180. m.Get("/explore", ignSignIn, routers.Explore)
  181. m.Combo("/install", routers.InstallInit).
  182. Get(routers.Install).
  183. Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost)
  184. m.Group("", func() {
  185. m.Get("/pulls", user.Pulls)
  186. m.Get("/issues", user.Issues)
  187. }, reqSignIn)
  188. // API.
  189. // FIXME: custom form error response.
  190. m.Group("/api", func() {
  191. m.Group("/v1", func() {
  192. // Miscellaneous.
  193. m.Post("/markdown", bindIgnErr(apiv1.MarkdownForm{}), v1.Markdown)
  194. m.Post("/markdown/raw", v1.MarkdownRaw)
  195. // Users.
  196. m.Group("/users", func() {
  197. m.Get("/search", v1.SearchUsers)
  198. m.Group("/:username", func() {
  199. m.Get("", v1.GetUserInfo)
  200. m.Group("/tokens", func() {
  201. m.Combo("").Get(v1.ListAccessTokens).Post(bind(v1.CreateAccessTokenForm{}), v1.CreateAccessToken)
  202. }, middleware.ApiReqBasicAuth())
  203. })
  204. })
  205. // Repositories.
  206. m.Combo("/user/repos", middleware.ApiReqToken()).Get(v1.ListMyRepos).Post(bind(api.CreateRepoOption{}), v1.CreateRepo)
  207. m.Post("/org/:org/repos", middleware.ApiReqToken(), bind(api.CreateRepoOption{}), v1.CreateOrgRepo)
  208. m.Group("/repos", func() {
  209. m.Get("/search", v1.SearchRepos)
  210. m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), v1.MigrateRepo)
  211. m.Group("/:username/:reponame", func() {
  212. m.Combo("/hooks").Get(v1.ListRepoHooks).Post(bind(api.CreateHookOption{}), v1.CreateRepoHook)
  213. m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook)
  214. m.Get("/raw/*", middleware.RepoRef(), v1.GetRepoRawFile)
  215. }, middleware.ApiRepoAssignment(), middleware.ApiReqToken())
  216. })
  217. m.Any("/*", func(ctx *middleware.Context) {
  218. ctx.HandleAPI(404, "Page not found")
  219. })
  220. })
  221. })
  222. // User.
  223. m.Group("/user", func() {
  224. m.Get("/login", user.SignIn)
  225. m.Post("/login", bindIgnErr(auth.SignInForm{}), user.SignInPost)
  226. m.Get("/info/:name", user.SocialSignIn)
  227. m.Get("/sign_up", user.SignUp)
  228. m.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
  229. m.Get("/reset_password", user.ResetPasswd)
  230. m.Post("/reset_password", user.ResetPasswdPost)
  231. }, reqSignOut)
  232. m.Group("/user/settings", func() {
  233. m.Get("", user.Settings)
  234. m.Post("", bindIgnErr(auth.UpdateProfileForm{}), user.SettingsPost)
  235. m.Post("/avatar", binding.MultipartForm(auth.UploadAvatarForm{}), user.SettingsAvatar)
  236. m.Get("/email", user.SettingsEmails)
  237. m.Post("/email", bindIgnErr(auth.AddEmailForm{}), user.SettingsEmailPost)
  238. m.Get("/password", user.SettingsPassword)
  239. m.Post("/password", bindIgnErr(auth.ChangePasswordForm{}), user.SettingsPasswordPost)
  240. m.Get("/ssh", user.SettingsSSHKeys)
  241. m.Post("/ssh", bindIgnErr(auth.AddSSHKeyForm{}), user.SettingsSSHKeysPost)
  242. m.Get("/social", user.SettingsSocial)
  243. m.Combo("/applications").Get(user.SettingsApplications).Post(bindIgnErr(auth.NewAccessTokenForm{}), user.SettingsApplicationsPost)
  244. m.Route("/delete", "GET,POST", user.SettingsDelete)
  245. }, reqSignIn)
  246. m.Group("/user", func() {
  247. // r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
  248. m.Any("/activate", user.Activate)
  249. m.Any("/activate_email", user.ActivateEmail)
  250. m.Get("/email2user", user.Email2User)
  251. m.Get("/forget_password", user.ForgotPasswd)
  252. m.Post("/forget_password", user.ForgotPasswdPost)
  253. m.Get("/logout", user.SignOut)
  254. })
  255. // Gravatar service.
  256. avt := avatar.CacheServer("public/img/avatar/", "public/img/avatar_default.jpg")
  257. os.MkdirAll("public/img/avatar/", os.ModePerm)
  258. m.Get("/avatar/:hash", avt.ServeHTTP)
  259. adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true})
  260. m.Group("/admin", func() {
  261. m.Get("", adminReq, admin.Dashboard)
  262. m.Get("/config", admin.Config)
  263. m.Get("/monitor", admin.Monitor)
  264. m.Group("/users", func() {
  265. m.Get("", admin.Users)
  266. m.Get("/new", admin.NewUser)
  267. m.Post("/new", bindIgnErr(auth.RegisterForm{}), admin.NewUserPost)
  268. m.Get("/:userid", admin.EditUser)
  269. m.Post("/:userid", bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
  270. m.Post("/:userid/delete", admin.DeleteUser)
  271. })
  272. m.Group("/orgs", func() {
  273. m.Get("", admin.Organizations)
  274. })
  275. m.Group("/repos", func() {
  276. m.Get("", admin.Repositories)
  277. })
  278. m.Group("/auths", func() {
  279. m.Get("", admin.Authentications)
  280. m.Get("/new", admin.NewAuthSource)
  281. m.Post("/new", bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost)
  282. m.Get("/:authid", admin.EditAuthSource)
  283. m.Post("/:authid", bindIgnErr(auth.AuthenticationForm{}), admin.EditAuthSourcePost)
  284. m.Post("/:authid/delete", admin.DeleteAuthSource)
  285. })
  286. m.Group("/notices", func() {
  287. m.Get("", admin.Notices)
  288. m.Get("/:id:int/delete", admin.DeleteNotice)
  289. })
  290. }, adminReq)
  291. m.Get("/:username", ignSignIn, user.Profile)
  292. if macaron.Env == macaron.DEV {
  293. m.Get("/template/*", dev.TemplatePreview)
  294. }
  295. reqAdmin := middleware.RequireAdmin()
  296. // Organization.
  297. m.Group("/org", func() {
  298. m.Get("/create", org.Create)
  299. m.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost)
  300. m.Group("/:org", func() {
  301. m.Get("/dashboard", user.Dashboard)
  302. m.Get("/members", org.Members)
  303. m.Get("/members/action/:action", org.MembersAction)
  304. m.Get("/teams", org.Teams)
  305. m.Get("/teams/:team", org.TeamMembers)
  306. m.Get("/teams/:team/repositories", org.TeamRepositories)
  307. m.Get("/teams/:team/action/:action", org.TeamsAction)
  308. m.Get("/teams/:team/action/repo/:action", org.TeamsRepoAction)
  309. }, middleware.OrgAssignment(true, true))
  310. m.Group("/:org", func() {
  311. m.Get("/teams/new", org.NewTeam)
  312. m.Post("/teams/new", bindIgnErr(auth.CreateTeamForm{}), org.NewTeamPost)
  313. m.Get("/teams/:team/edit", org.EditTeam)
  314. m.Post("/teams/:team/edit", bindIgnErr(auth.CreateTeamForm{}), org.EditTeamPost)
  315. m.Post("/teams/:team/delete", org.DeleteTeam)
  316. m.Group("/settings", func() {
  317. m.Get("", org.Settings)
  318. m.Post("", bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost)
  319. m.Get("/hooks", org.SettingsHooks)
  320. m.Get("/hooks/new", repo.WebHooksNew)
  321. m.Post("/hooks/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
  322. m.Post("/hooks/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
  323. m.Get("/hooks/:id", repo.WebHooksEdit)
  324. m.Post("/hooks/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  325. m.Post("/hooks/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
  326. m.Route("/delete", "GET,POST", org.SettingsDelete)
  327. })
  328. m.Route("/invitations/new", "GET,POST", org.Invitation)
  329. }, middleware.OrgAssignment(true, true, true))
  330. }, reqSignIn)
  331. m.Group("/org", func() {
  332. m.Get("/:org", org.Home)
  333. }, ignSignIn, middleware.OrgAssignment(true))
  334. // Repository.
  335. m.Group("/repo", func() {
  336. m.Get("/create", repo.Create)
  337. m.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
  338. m.Get("/migrate", repo.Migrate)
  339. m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
  340. m.Get("/fork", repo.Fork)
  341. m.Post("/fork", bindIgnErr(auth.CreateRepoForm{}), repo.ForkPost)
  342. }, reqSignIn)
  343. m.Group("/:username/:reponame", func() {
  344. m.Get("/settings", repo.Settings)
  345. m.Post("/settings", bindIgnErr(auth.RepoSettingForm{}), repo.SettingsPost)
  346. m.Group("/settings", func() {
  347. m.Route("/collaboration", "GET,POST", repo.SettingsCollaboration)
  348. m.Get("/hooks", repo.Webhooks)
  349. m.Get("/hooks/new", repo.WebHooksNew)
  350. m.Post("/hooks/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
  351. m.Post("/hooks/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
  352. m.Get("/hooks/:id", repo.WebHooksEdit)
  353. m.Post("/hooks/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
  354. m.Post("/hooks/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
  355. m.Group("/hooks/git", func() {
  356. m.Get("", repo.GitHooks)
  357. m.Get("/:name", repo.GitHooksEdit)
  358. m.Post("/:name", repo.GitHooksEditPost)
  359. }, middleware.GitHookService())
  360. })
  361. }, reqSignIn, middleware.RepoAssignment(true), reqAdmin)
  362. m.Group("/:username/:reponame", func() {
  363. m.Get("/action/:action", repo.Action)
  364. m.Group("/issues", func() {
  365. m.Get("/new", repo.CreateIssue)
  366. m.Post("/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
  367. m.Post("/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
  368. m.Post("/:index/label", repo.UpdateIssueLabel)
  369. m.Post("/:index/milestone", repo.UpdateIssueMilestone)
  370. m.Post("/:index/assignee", repo.UpdateAssignee)
  371. m.Get("/:index/attachment/:id", repo.IssueGetAttachment)
  372. m.Post("/labels/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel)
  373. m.Post("/labels/edit", bindIgnErr(auth.CreateLabelForm{}), repo.UpdateLabel)
  374. m.Post("/labels/delete", repo.DeleteLabel)
  375. m.Get("/milestones/new", repo.NewMilestone)
  376. m.Post("/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost)
  377. m.Get("/milestones/:index/edit", repo.UpdateMilestone)
  378. m.Post("/milestones/:index/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.UpdateMilestonePost)
  379. m.Get("/milestones/:index/:action", repo.UpdateMilestone)
  380. })
  381. m.Post("/comment/:action", repo.Comment)
  382. m.Group("/releases", func() {
  383. m.Get("/new", repo.NewRelease)
  384. m.Post("/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost)
  385. m.Get("/edit/:tagname", repo.EditRelease)
  386. m.Post("/edit/:tagname", bindIgnErr(auth.EditReleaseForm{}), repo.EditReleasePost)
  387. }, middleware.RepoRef())
  388. }, reqSignIn, middleware.RepoAssignment(true))
  389. m.Group("/:username/:reponame", func() {
  390. m.Get("/releases", middleware.RepoRef(), repo.Releases)
  391. m.Get("/issues", repo.Issues)
  392. m.Get("/issues/:index", repo.ViewIssue)
  393. m.Get("/issues/milestones", repo.Milestones)
  394. m.Get("/pulls", repo.Pulls)
  395. m.Get("/branches", repo.Branches)
  396. m.Get("/archive/*", repo.Download)
  397. m.Get("/issues2/", repo.Issues2)
  398. m.Get("/pulls2/", repo.PullRequest2)
  399. m.Get("/labels2/", repo.Labels2)
  400. m.Get("/milestone2/", repo.Milestones2)
  401. m.Group("", func() {
  402. m.Get("/src/*", repo.Home)
  403. m.Get("/raw/*", repo.SingleDownload)
  404. m.Get("/commits/*", repo.RefCommits)
  405. m.Get("/commit/*", repo.Diff)
  406. }, middleware.RepoRef())
  407. m.Get("/compare/:before([a-z0-9]+)...:after([a-z0-9]+)", repo.CompareDiff)
  408. }, ignSignIn, middleware.RepoAssignment(true))
  409. m.Group("/:username", func() {
  410. m.Get("/:reponame", ignSignIn, middleware.RepoAssignment(true, true), middleware.RepoRef(), repo.Home)
  411. m.Any("/:reponame/*", ignSignInAndCsrf, repo.Http)
  412. })
  413. // robots.txt
  414. m.Get("/robots.txt", func(ctx *middleware.Context) {
  415. if setting.HasRobotsTxt {
  416. ctx.ServeFileContent(path.Join(setting.CustomPath, "robots.txt"))
  417. } else {
  418. ctx.Error(404)
  419. }
  420. })
  421. // Not found handler.
  422. m.NotFound(routers.NotFound)
  423. // Flag for port number in case first time run conflict.
  424. if ctx.IsSet("port") {
  425. setting.AppUrl = strings.Replace(setting.AppUrl, setting.HttpPort, ctx.String("port"), 1)
  426. setting.HttpPort = ctx.String("port")
  427. }
  428. var err error
  429. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  430. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl)
  431. switch setting.Protocol {
  432. case setting.HTTP:
  433. err = http.ListenAndServe(listenAddr, m)
  434. case setting.HTTPS:
  435. server := &http.Server{Addr: listenAddr, TLSConfig: &tls.Config{MinVersion: tls.VersionTLS10}, Handler: m}
  436. err = server.ListenAndServeTLS(setting.CertFile, setting.KeyFile)
  437. case setting.FCGI:
  438. err = fcgi.Serve(nil, m)
  439. default:
  440. log.Fatal(4, "Invalid protocol: %s", setting.Protocol)
  441. }
  442. if err != nil {
  443. log.Fatal(4, "Fail to start server: %v", err)
  444. }
  445. }