forgejo/main.go

73 lines
1.7 KiB
Go
Raw Normal View History

2014-02-19 18:04:31 +00:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2016 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2014-02-19 09:50:53 +00:00
package main
2014-02-12 17:49:46 +00:00
import (
2014-02-19 09:50:53 +00:00
"os"
"runtime"
"strings"
"time"
2016-11-30 23:56:15 +00:00
"code.gitea.io/gitea/cmd"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
2018-11-01 13:41:07 +00:00
// register supported doc types
Support asciicast files as new markup (#22448) Support [asciicast files](https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v2.md) as a new markup via [asciinema-player](https://github.com/asciinema/asciinema-player). For more on asciinema, see the [introduction](https://asciinema.org/). So users can use asciinema recorder to generate an asciicast file (or you can download a sample file from https://asciinema.org/a/335480.cast?dl=1), then upload it to Gitea and play it on Gitea. Snapshots: <details> ## Upload asciicast files <img width="1134" alt="image" src="https://user-images.githubusercontent.com/9418365/212461061-cc2c7181-0e14-4534-af55-1ec60a639fd1.png"> ## Open an asciicast file <img width="1137" alt="image" src="https://user-images.githubusercontent.com/9418365/212461090-a3b5141f-4894-430d-a2b4-ea257801a0ed.png"> ## Play it <img width="1144" alt="image" src="https://user-images.githubusercontent.com/9418365/212461157-4e82db69-0e41-471d-928f-ac1fe0737105.png"> ## Copy contents from the "video" <img width="1145" alt="image" src="https://user-images.githubusercontent.com/9418365/212461286-211612bc-15d6-427a-89a9-6abff5c6a0a5.png"> ## View the source <img width="1140" alt="image" src="https://user-images.githubusercontent.com/9418365/212461187-05473b2d-ba3d-4072-84a6-4aa1e7d82182.png"> </details> Known issue: Don't support the [v1 version asciicast files](https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v1.md), it's a poorly designed version, it does not specify the file extension and uses `*.json` usually, so it's impossible to recognize the files. Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-01-18 00:46:58 +00:00
_ "code.gitea.io/gitea/modules/markup/asciicast"
_ "code.gitea.io/gitea/modules/markup/console"
_ "code.gitea.io/gitea/modules/markup/csv"
_ "code.gitea.io/gitea/modules/markup/markdown"
_ "code.gitea.io/gitea/modules/markup/orgmode"
"github.com/urfave/cli/v2"
2014-02-12 17:49:46 +00:00
)
// these flags will be set by the build flags
var (
Version = "development" // program version for this build
Tags = "" // the Golang build tags
MakeVersion = "" // "make" program version if built with make
)
[SEMVER] store SemVer in ForgejoSemVer after a database upgrade (cherry picked from commit b7fe7cf401f4bddd6455efc651f7ac054f3fe1cf) (cherry picked from commit cf339eed4f4851b18448dbdd83df32d00bc6f45b) (cherry picked from commit 4f3a16168bbeced519a60c32e10e2895d9367238) (cherry picked from commit 6f5bbc53fcebd614f5ee8627c7d6e3c637ffa694) (cherry picked from commit aca42b422e76668387769c15868ef77b073bb7db) (cherry picked from commit 5a7f7580e525694d1f27e12329c8532bd29273f8) (cherry picked from commit 06c383c807ad49e1e35b429a10c6a11c65aeebe5) (cherry picked from commit fe831dcb53b81b6cc632be751bfcbfb9bc00efd3) (cherry picked from commit cd12cd0dbce47c6117ea579ae5019c182155b3a9) (cherry picked from commit cc79163703ce31706c86b88c38fb8a20ed745e20) (cherry picked from commit 0102a5715ea6a03b560cc2f0b6cbe2b2576c255e) (cherry picked from commit 403f7520b3056eace36eae505afbab6a05f597b7) (cherry picked from commit a3b61510a246f61c174ddc3c288e556522a6aab9) (cherry picked from commit f83f0f9feb76a8b62ca4d74dc2785c713fbec282) (cherry picked from commit fd1c3a6d09a057070844cd955d0920518e60f408) (cherry picked from commit f7cdc3d6f1ad1ff01bb1814207fcb41210db80e8) (cherry picked from commit 060121b644e0515a6b673cdd514d52e6fa1e6ec3) (cherry picked from commit 62c847ff0235196e73e860d0d1658f0734985270) (cherry picked from commit 4d051b51c2813828b978ef84b6b72c0ba051741b) (cherry picked from commit 86e6981a936b1937065605892083b2488424bf5e) (cherry picked from commit c1fc9e441b1467033911f3848f82a89aaacf98a9)
2023-08-08 21:55:25 +00:00
var ForgejoVersion = "1.0.0"
2014-02-12 19:54:09 +00:00
func init() {
setting.AppVer = Version
[SEMVER] store SemVer in ForgejoSemVer after a database upgrade (cherry picked from commit b7fe7cf401f4bddd6455efc651f7ac054f3fe1cf) (cherry picked from commit cf339eed4f4851b18448dbdd83df32d00bc6f45b) (cherry picked from commit 4f3a16168bbeced519a60c32e10e2895d9367238) (cherry picked from commit 6f5bbc53fcebd614f5ee8627c7d6e3c637ffa694) (cherry picked from commit aca42b422e76668387769c15868ef77b073bb7db) (cherry picked from commit 5a7f7580e525694d1f27e12329c8532bd29273f8) (cherry picked from commit 06c383c807ad49e1e35b429a10c6a11c65aeebe5) (cherry picked from commit fe831dcb53b81b6cc632be751bfcbfb9bc00efd3) (cherry picked from commit cd12cd0dbce47c6117ea579ae5019c182155b3a9) (cherry picked from commit cc79163703ce31706c86b88c38fb8a20ed745e20) (cherry picked from commit 0102a5715ea6a03b560cc2f0b6cbe2b2576c255e) (cherry picked from commit 403f7520b3056eace36eae505afbab6a05f597b7) (cherry picked from commit a3b61510a246f61c174ddc3c288e556522a6aab9) (cherry picked from commit f83f0f9feb76a8b62ca4d74dc2785c713fbec282) (cherry picked from commit fd1c3a6d09a057070844cd955d0920518e60f408) (cherry picked from commit f7cdc3d6f1ad1ff01bb1814207fcb41210db80e8) (cherry picked from commit 060121b644e0515a6b673cdd514d52e6fa1e6ec3) (cherry picked from commit 62c847ff0235196e73e860d0d1658f0734985270) (cherry picked from commit 4d051b51c2813828b978ef84b6b72c0ba051741b) (cherry picked from commit 86e6981a936b1937065605892083b2488424bf5e) (cherry picked from commit c1fc9e441b1467033911f3848f82a89aaacf98a9)
2023-08-08 21:55:25 +00:00
setting.ForgejoVersion = ForgejoVersion
2019-06-12 19:41:28 +00:00
setting.AppBuiltWith = formatBuiltWith()
setting.AppStartTime = time.Now().UTC()
Refactor path & config system (#25330) # The problem There were many "path tricks": * By default, Gitea uses its program directory as its work path * Gitea tries to use the "work path" to guess its "custom path" and "custom conf (app.ini)" * Users might want to use other directories as work path * The non-default work path should be passed to Gitea by GITEA_WORK_DIR or "--work-path" * But some Gitea processes are started without these values * The "serv" process started by OpenSSH server * The CLI sub-commands started by site admin * The paths are guessed by SetCustomPathAndConf again and again * The default values of "work path / custom path / custom conf" can be changed when compiling # The solution * Use `InitWorkPathAndCommonConfig` to handle these path tricks, and use test code to cover its behaviors. * When Gitea's web server runs, write the WORK_PATH to "app.ini", this value must be the most correct one, because if this value is not right, users would find that the web UI doesn't work and then they should be able to fix it. * Then all other sub-commands can use the WORK_PATH in app.ini to initialize their paths. * By the way, when Gitea starts for git protocol, it shouldn't output any log, otherwise the git protocol gets broken and client blocks forever. The "work path" priority is: WORK_PATH in app.ini > cmd arg --work-path > env var GITEA_WORK_DIR > builtin default The "app.ini" searching order is: cmd arg --config > cmd arg "work path / custom path" > env var "work path / custom path" > builtin default ## ⚠️ BREAKING If your instance's "work path / custom path / custom conf" doesn't meet the requirements (eg: work path must be absolute), Gitea will report a fatal error and exit. You need to set these values according to the error log. ---- Close #24818 Close #24222 Close #21606 Close #21498 Close #25107 Close #24981 Maybe close #24503 Replace #23301 Replace #22754 And maybe more
2023-06-21 05:50:26 +00:00
}
[BRANDING] alias {FORGEJO,GITEA}_{CUSTOM,WORK_DIR} FORGEJO_* environment variables are set to the corresponding GITEA_* variable when the cli starts. This approach is intended to minimize the conflicts on rebase. All occurences of GITEA_* are left untouched in the codebase and they are only changed to FORGEJO_* if exposed to the user. (cherry picked from commit e466f9d10e145a3ed750f0c86cfcafad5f141ea8) (cherry picked from commit e33e95931b0d91c41433db68ddb19a8724b8ed80) (cherry picked from commit 2cfc6519b7cbc758f8322fd7f7b84dfc6d14ae73) (cherry picked from commit af8864373a65441c817dad1c3b696157c4024a7e) (cherry picked from commit a0550ff3391f0575b72c3d0d1fdf1fcd9b51e4d6) (cherry picked from commit 24dc0a5191a26fac0aef6b3a53fd58b9185edc88) (cherry picked from commit e255eea2b48ad39eeb3eae227ca0d4fd4b667d99) (cherry picked from commit 0c4f5afa7a37e6b805efb452b9d4166f7e2cec61) (cherry picked from commit 42fce708d05d616ebbdfb2eecf384ad2fdaa1d7a) (cherry picked from commit e7278c3c22b656e5730ec2ba0c1b4ee2ef6627ce) (cherry picked from commit 0fb9ed7e0eb5db59ab5b574d3d69b4b161f50b0b) (cherry picked from commit a98308aa4d6659f46f7f69996167f9c2f3c4e859) (cherry picked from commit b8695fcbe04792cb75acb62fdf9c6dee237e8365) (cherry picked from commit 4aee8719f57b40f8645905c10ba8966b29052295) (cherry picked from commit 1c503c1ba7414fe0b960187bcc370b44c536ec7f) (cherry picked from commit cf1ed8551e6a8abc56d3ed45b65b0e6ca4e1e891) (cherry picked from commit c52459b088cd52bf3b11b68cadf95d738fd09269) (cherry picked from commit 92cac277b28f9b68d3346f5dd463bc41c1952af6) (cherry picked from commit 2c744f11189ca76de62be908069500d7dbfc2a8f) (cherry picked from commit 989a98a8c52342fb146938c41c1db9a0818013f4) (cherry picked from commit 9cf7052bd4c126618833487134cb39dbe6d58297) (cherry picked from commit a13c9667e04555d38106bf7e63d57b05002b20ee) Conflicts: main.go https://codeberg.org/forgejo/forgejo/pulls/1216 (cherry picked from commit a9c3cf060d76ece0664b05f03690bd3a7d8f7089) (cherry picked from commit eb6d904b23bce2bc02ea96db0459b7b7a8e55cae) (cherry picked from commit fd4878e4877ced8a65cb348c69220b688556a214) (cherry picked from commit cc2cab92410e13abdd88c6ae8acfd17ee6e7fb0a) (cherry picked from commit be523c63dbc7d3dc1f852bb7aee045cf2d9f74fa) (cherry picked from commit 254d1a2f9a9751cb2e0d9475f2f6c7579120cd32) (cherry picked from commit 1eecf4200e2c073c13675f769bd1254fdcc4079f) (cherry picked from commit a01f5c3475991ae2dfceaa17ac3dd7adccce3d5f) (cherry picked from commit b1ba7ce32131a4868a734fec1aef64bd96150fbd) (cherry picked from commit eddfd96beb24604179ac80175e717c334cdb6d75) (cherry picked from commit c9c9b7e49bd298b4e1d1c374a8829e434f140d6e) (cherry picked from commit ad7e871779b18b8265ddf4c06566cff90f494c89) (cherry picked from commit 9073ed215bf33535a3764b834e2e16690af72679) (cherry picked from commit a827fdadd391159cd928e767b08e9d04473ed145) (cherry picked from commit 39e66246cd75800f26bdde94b421893ad2701183) (cherry picked from commit 46d1c778a2fd5030d7ad48b27ffcf18f04236e54) (cherry picked from commit a65bc0b75dfb03fe474ad47003aceb7c751d890c) (cherry picked from commit 5f901e967161a1768b8d1da1edee23e1eb2f8d79) Conflicts: cmd/main.go https://codeberg.org/forgejo/forgejo/pulls/1976 (cherry picked from commit a0e017d206ade5de9dacb7794052dde38aab3616) (cherry picked from commit e6e24b96312ee9090865f685e299b2073bb918f8) (cherry picked from commit 00667d869b593fe50c1236f902c5656b5f4265c7)
2023-02-24 12:38:29 +00:00
func forgejoEnv() {
for _, k := range []string{"CUSTOM", "WORK_DIR"} {
if v, ok := os.LookupEnv("FORGEJO_" + k); ok {
os.Setenv("GITEA_"+k, v)
}
}
}
2014-02-12 17:49:46 +00:00
func main() {
[BRANDING] alias {FORGEJO,GITEA}_{CUSTOM,WORK_DIR} FORGEJO_* environment variables are set to the corresponding GITEA_* variable when the cli starts. This approach is intended to minimize the conflicts on rebase. All occurences of GITEA_* are left untouched in the codebase and they are only changed to FORGEJO_* if exposed to the user. (cherry picked from commit e466f9d10e145a3ed750f0c86cfcafad5f141ea8) (cherry picked from commit e33e95931b0d91c41433db68ddb19a8724b8ed80) (cherry picked from commit 2cfc6519b7cbc758f8322fd7f7b84dfc6d14ae73) (cherry picked from commit af8864373a65441c817dad1c3b696157c4024a7e) (cherry picked from commit a0550ff3391f0575b72c3d0d1fdf1fcd9b51e4d6) (cherry picked from commit 24dc0a5191a26fac0aef6b3a53fd58b9185edc88) (cherry picked from commit e255eea2b48ad39eeb3eae227ca0d4fd4b667d99) (cherry picked from commit 0c4f5afa7a37e6b805efb452b9d4166f7e2cec61) (cherry picked from commit 42fce708d05d616ebbdfb2eecf384ad2fdaa1d7a) (cherry picked from commit e7278c3c22b656e5730ec2ba0c1b4ee2ef6627ce) (cherry picked from commit 0fb9ed7e0eb5db59ab5b574d3d69b4b161f50b0b) (cherry picked from commit a98308aa4d6659f46f7f69996167f9c2f3c4e859) (cherry picked from commit b8695fcbe04792cb75acb62fdf9c6dee237e8365) (cherry picked from commit 4aee8719f57b40f8645905c10ba8966b29052295) (cherry picked from commit 1c503c1ba7414fe0b960187bcc370b44c536ec7f) (cherry picked from commit cf1ed8551e6a8abc56d3ed45b65b0e6ca4e1e891) (cherry picked from commit c52459b088cd52bf3b11b68cadf95d738fd09269) (cherry picked from commit 92cac277b28f9b68d3346f5dd463bc41c1952af6) (cherry picked from commit 2c744f11189ca76de62be908069500d7dbfc2a8f) (cherry picked from commit 989a98a8c52342fb146938c41c1db9a0818013f4) (cherry picked from commit 9cf7052bd4c126618833487134cb39dbe6d58297) (cherry picked from commit a13c9667e04555d38106bf7e63d57b05002b20ee) Conflicts: main.go https://codeberg.org/forgejo/forgejo/pulls/1216 (cherry picked from commit a9c3cf060d76ece0664b05f03690bd3a7d8f7089) (cherry picked from commit eb6d904b23bce2bc02ea96db0459b7b7a8e55cae) (cherry picked from commit fd4878e4877ced8a65cb348c69220b688556a214) (cherry picked from commit cc2cab92410e13abdd88c6ae8acfd17ee6e7fb0a) (cherry picked from commit be523c63dbc7d3dc1f852bb7aee045cf2d9f74fa) (cherry picked from commit 254d1a2f9a9751cb2e0d9475f2f6c7579120cd32) (cherry picked from commit 1eecf4200e2c073c13675f769bd1254fdcc4079f) (cherry picked from commit a01f5c3475991ae2dfceaa17ac3dd7adccce3d5f) (cherry picked from commit b1ba7ce32131a4868a734fec1aef64bd96150fbd) (cherry picked from commit eddfd96beb24604179ac80175e717c334cdb6d75) (cherry picked from commit c9c9b7e49bd298b4e1d1c374a8829e434f140d6e) (cherry picked from commit ad7e871779b18b8265ddf4c06566cff90f494c89) (cherry picked from commit 9073ed215bf33535a3764b834e2e16690af72679) (cherry picked from commit a827fdadd391159cd928e767b08e9d04473ed145) (cherry picked from commit 39e66246cd75800f26bdde94b421893ad2701183) (cherry picked from commit 46d1c778a2fd5030d7ad48b27ffcf18f04236e54) (cherry picked from commit a65bc0b75dfb03fe474ad47003aceb7c751d890c) (cherry picked from commit 5f901e967161a1768b8d1da1edee23e1eb2f8d79) Conflicts: cmd/main.go https://codeberg.org/forgejo/forgejo/pulls/1976 (cherry picked from commit a0e017d206ade5de9dacb7794052dde38aab3616) (cherry picked from commit e6e24b96312ee9090865f685e299b2073bb918f8) (cherry picked from commit 00667d869b593fe50c1236f902c5656b5f4265c7)
2023-02-24 12:38:29 +00:00
forgejoEnv()
cli.OsExiter = func(code int) {
log.GetManager().Close()
os.Exit(code)
2016-11-30 23:56:15 +00:00
}
app := cmd.NewMainApp(Version, formatBuiltWith())
_ = cmd.RunMainApp(app, os.Args...) // all errors should have been handled by the RunMainApp
Rewrite logger system (#24726) ## ⚠️ Breaking The `log.<mode>.<logger>` style config has been dropped. If you used it, please check the new config manual & app.example.ini to make your instance output logs as expected. Although many legacy options still work, it's encouraged to upgrade to the new options. The SMTP logger is deleted because SMTP is not suitable to collect logs. If you have manually configured Gitea log options, please confirm the logger system works as expected after upgrading. ## Description Close #12082 and maybe more log-related issues, resolve some related FIXMEs in old code (which seems unfixable before) Just like rewriting queue #24505 : make code maintainable, clear legacy bugs, and add the ability to support more writers (eg: JSON, structured log) There is a new document (with examples): `logging-config.en-us.md` This PR is safer than the queue rewriting, because it's just for logging, it won't break other logic. ## The old problems The logging system is quite old and difficult to maintain: * Unclear concepts: Logger, NamedLogger, MultiChannelledLogger, SubLogger, EventLogger, WriterLogger etc * Some code is diffuclt to konw whether it is right: `log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs `log.DelLogger("console")` * The old system heavily depends on ini config system, it's difficult to create new logger for different purpose, and it's very fragile. * The "color" trick is difficult to use and read, many colors are unnecessary, and in the future structured log could help * It's difficult to add other log formats, eg: JSON format * The log outputer doesn't have full control of its goroutine, it's difficult to make outputer have advanced behaviors * The logs could be lost in some cases: eg: no Fatal error when using CLI. * Config options are passed by JSON, which is quite fragile. * INI package makes the KEY in `[log]` section visible in `[log.sub1]` and `[log.sub1.subA]`, this behavior is quite fragile and would cause more unclear problems, and there is no strong requirement to support `log.<mode>.<logger>` syntax. ## The new design See `logger.go` for documents. ## Screenshot <details> ![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff) ![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9) ![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee) </details> ## TODO * [x] add some new tests * [x] fix some tests * [x] test some sub-commands (manually ....) --------- Co-authored-by: Jason Song <i@wolfogre.com> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-21 22:35:11 +00:00
log.GetManager().Close()
2014-02-12 17:49:46 +00:00
}
2019-06-12 19:41:28 +00:00
func formatBuiltWith() string {
version := runtime.Version()
if len(MakeVersion) > 0 {
version = MakeVersion + ", " + runtime.Version()
}
if len(Tags) == 0 {
return " built with " + version
}
return " built with " + version + " : " + strings.ReplaceAll(Tags, " ", ", ")
}