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.

web.go 17KB

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