選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

web.go 8.3KB

9年前
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
7年前
10年前
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5年前
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
3年前
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
7年前
10年前
10年前
10年前
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5年前
10年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "context"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "os"
  10. "strings"
  11. _ "net/http/pprof" // Used for debugging if enabled and a web server is running
  12. "code.gitea.io/gitea/modules/graceful"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/process"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/routers"
  17. "code.gitea.io/gitea/routers/install"
  18. "github.com/felixge/fgprof"
  19. "github.com/urfave/cli"
  20. ini "gopkg.in/ini.v1"
  21. )
  22. // CmdWeb represents the available web sub-command.
  23. var CmdWeb = cli.Command{
  24. Name: "web",
  25. Usage: "Start Gitea web server",
  26. Description: `Gitea web server is the only thing you need to run,
  27. and it takes care of all the other things for you`,
  28. Action: runWeb,
  29. Flags: []cli.Flag{
  30. cli.StringFlag{
  31. Name: "port, p",
  32. Value: "3000",
  33. Usage: "Temporary port number to prevent conflict",
  34. },
  35. cli.StringFlag{
  36. Name: "install-port",
  37. Value: "3000",
  38. Usage: "Temporary port number to run the install page on to prevent conflict",
  39. },
  40. cli.StringFlag{
  41. Name: "pid, P",
  42. Value: setting.PIDFile,
  43. Usage: "Custom pid file path",
  44. },
  45. cli.BoolFlag{
  46. Name: "quiet, q",
  47. Usage: "Only display Fatal logging errors until logging is set-up",
  48. },
  49. cli.BoolFlag{
  50. Name: "verbose",
  51. Usage: "Set initial logging to TRACE level until logging is properly set-up",
  52. },
  53. },
  54. }
  55. func runHTTPRedirector() {
  56. _, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().HammerContext(), "Web: HTTP Redirector", process.SystemProcessType, true)
  57. defer finished()
  58. source := fmt.Sprintf("%s:%s", setting.HTTPAddr, setting.PortToRedirect)
  59. dest := strings.TrimSuffix(setting.AppURL, "/")
  60. log.Info("Redirecting: %s to %s", source, dest)
  61. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  62. target := dest + r.URL.Path
  63. if len(r.URL.RawQuery) > 0 {
  64. target += "?" + r.URL.RawQuery
  65. }
  66. http.Redirect(w, r, target, http.StatusTemporaryRedirect)
  67. })
  68. err := runHTTP("tcp", source, "HTTP Redirector", handler, setting.RedirectorUseProxyProtocol)
  69. if err != nil {
  70. log.Fatal("Failed to start port redirection: %v", err)
  71. }
  72. }
  73. func runWeb(ctx *cli.Context) error {
  74. if ctx.Bool("verbose") {
  75. _ = log.DelLogger("console")
  76. log.NewLogger(0, "console", "console", fmt.Sprintf(`{"level": "trace", "colorize": %t, "stacktraceLevel": "none"}`, log.CanColorStdout))
  77. } else if ctx.Bool("quiet") {
  78. _ = log.DelLogger("console")
  79. log.NewLogger(0, "console", "console", fmt.Sprintf(`{"level": "fatal", "colorize": %t, "stacktraceLevel": "none"}`, log.CanColorStdout))
  80. }
  81. defer func() {
  82. if panicked := recover(); panicked != nil {
  83. log.Fatal("PANIC: %v\n%s", panicked, log.Stack(2))
  84. }
  85. }()
  86. managerCtx, cancel := context.WithCancel(context.Background())
  87. graceful.InitManager(managerCtx)
  88. defer cancel()
  89. if os.Getppid() > 1 && len(os.Getenv("LISTEN_FDS")) > 0 {
  90. log.Info("Restarting Gitea on PID: %d from parent PID: %d", os.Getpid(), os.Getppid())
  91. } else {
  92. log.Info("Starting Gitea on PID: %d", os.Getpid())
  93. }
  94. // Set pid file setting
  95. if ctx.IsSet("pid") {
  96. setting.PIDFile = ctx.String("pid")
  97. setting.WritePIDFile = true
  98. }
  99. // Perform pre-initialization
  100. needsInstall := install.PreloadSettings(graceful.GetManager().HammerContext())
  101. if needsInstall {
  102. // Flag for port number in case first time run conflict
  103. if ctx.IsSet("port") {
  104. if err := setPort(ctx.String("port")); err != nil {
  105. return err
  106. }
  107. }
  108. if ctx.IsSet("install-port") {
  109. if err := setPort(ctx.String("install-port")); err != nil {
  110. return err
  111. }
  112. }
  113. installCtx, cancel := context.WithCancel(graceful.GetManager().HammerContext())
  114. c := install.Routes(installCtx)
  115. err := listen(c, false)
  116. cancel()
  117. if err != nil {
  118. log.Critical("Unable to open listener for installer. Is Gitea already running?")
  119. graceful.GetManager().DoGracefulShutdown()
  120. }
  121. select {
  122. case <-graceful.GetManager().IsShutdown():
  123. <-graceful.GetManager().Done()
  124. log.Info("PID: %d Gitea Web Finished", os.Getpid())
  125. log.Close()
  126. return err
  127. default:
  128. }
  129. } else {
  130. NoInstallListener()
  131. }
  132. if setting.EnablePprof {
  133. go func() {
  134. http.DefaultServeMux.Handle("/debug/fgprof", fgprof.Handler())
  135. _, _, finished := process.GetManager().AddTypedContext(context.Background(), "Web: PProf Server", process.SystemProcessType, true)
  136. // The pprof server is for debug purpose only, it shouldn't be exposed on public network. At the moment it's not worth to introduce a configurable option for it.
  137. log.Info("Starting pprof server on localhost:6060")
  138. log.Info("Stopped pprof server: %v", http.ListenAndServe("localhost:6060", nil))
  139. finished()
  140. }()
  141. }
  142. log.Info("Global init")
  143. // Perform global initialization
  144. setting.LoadFromExisting()
  145. routers.GlobalInitInstalled(graceful.GetManager().HammerContext())
  146. // We check that AppDataPath exists here (it should have been created during installation)
  147. // We can't check it in `GlobalInitInstalled`, because some integration tests
  148. // use cmd -> GlobalInitInstalled, but the AppDataPath doesn't exist during those tests.
  149. if _, err := os.Stat(setting.AppDataPath); err != nil {
  150. log.Fatal("Can not find APP_DATA_PATH '%s'", setting.AppDataPath)
  151. }
  152. // Override the provided port number within the configuration
  153. if ctx.IsSet("port") {
  154. if err := setPort(ctx.String("port")); err != nil {
  155. return err
  156. }
  157. }
  158. // Set up Chi routes
  159. c := routers.NormalRoutes(graceful.GetManager().HammerContext())
  160. err := listen(c, true)
  161. <-graceful.GetManager().Done()
  162. log.Info("PID: %d Gitea Web Finished", os.Getpid())
  163. log.Close()
  164. return err
  165. }
  166. func setPort(port string) error {
  167. setting.AppURL = strings.Replace(setting.AppURL, setting.HTTPPort, port, 1)
  168. setting.HTTPPort = port
  169. switch setting.Protocol {
  170. case setting.HTTPUnix:
  171. case setting.FCGI:
  172. case setting.FCGIUnix:
  173. default:
  174. defaultLocalURL := string(setting.Protocol) + "://"
  175. if setting.HTTPAddr == "0.0.0.0" {
  176. defaultLocalURL += "localhost"
  177. } else {
  178. defaultLocalURL += setting.HTTPAddr
  179. }
  180. defaultLocalURL += ":" + setting.HTTPPort + "/"
  181. // Save LOCAL_ROOT_URL if port changed
  182. setting.CreateOrAppendToCustomConf("server.LOCAL_ROOT_URL", func(cfg *ini.File) {
  183. cfg.Section("server").Key("LOCAL_ROOT_URL").SetValue(defaultLocalURL)
  184. })
  185. }
  186. return nil
  187. }
  188. func listen(m http.Handler, handleRedirector bool) error {
  189. listenAddr := setting.HTTPAddr
  190. if setting.Protocol != setting.HTTPUnix && setting.Protocol != setting.FCGIUnix {
  191. listenAddr = net.JoinHostPort(listenAddr, setting.HTTPPort)
  192. }
  193. _, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().HammerContext(), "Web: Gitea Server", process.SystemProcessType, true)
  194. defer finished()
  195. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL)
  196. // This can be useful for users, many users do wrong to their config and get strange behaviors behind a reverse-proxy.
  197. // A user may fix the configuration mistake when he sees this log.
  198. // And this is also very helpful to maintainers to provide help to users to resolve their configuration problems.
  199. log.Info("AppURL(ROOT_URL): %s", setting.AppURL)
  200. if setting.LFS.StartServer {
  201. log.Info("LFS server enabled")
  202. }
  203. var err error
  204. switch setting.Protocol {
  205. case setting.HTTP:
  206. if handleRedirector {
  207. NoHTTPRedirector()
  208. }
  209. err = runHTTP("tcp", listenAddr, "Web", m, setting.UseProxyProtocol)
  210. case setting.HTTPS:
  211. if setting.EnableAcme {
  212. err = runACME(listenAddr, m)
  213. break
  214. }
  215. if handleRedirector {
  216. if setting.RedirectOtherPort {
  217. go runHTTPRedirector()
  218. } else {
  219. NoHTTPRedirector()
  220. }
  221. }
  222. err = runHTTPS("tcp", listenAddr, "Web", setting.CertFile, setting.KeyFile, m, setting.UseProxyProtocol, setting.ProxyProtocolTLSBridging)
  223. case setting.FCGI:
  224. if handleRedirector {
  225. NoHTTPRedirector()
  226. }
  227. err = runFCGI("tcp", listenAddr, "FCGI Web", m, setting.UseProxyProtocol)
  228. case setting.HTTPUnix:
  229. if handleRedirector {
  230. NoHTTPRedirector()
  231. }
  232. err = runHTTP("unix", listenAddr, "Web", m, setting.UseProxyProtocol)
  233. case setting.FCGIUnix:
  234. if handleRedirector {
  235. NoHTTPRedirector()
  236. }
  237. err = runFCGI("unix", listenAddr, "Web", m, setting.UseProxyProtocol)
  238. default:
  239. log.Fatal("Invalid protocol: %s", setting.Protocol)
  240. }
  241. if err != nil {
  242. log.Critical("Failed to start server: %v", err)
  243. }
  244. log.Info("HTTP Listener: %s Closed", listenAddr)
  245. return err
  246. }