forgejo/main.go

73 lines
1.7 KiB
Go
Raw Normal View History

2014-02-19 19:04:31 +01:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2016 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2014-02-19 10:50:53 +01:00
package main
2014-02-12 18:49:46 +01:00
import (
2014-02-19 10:50:53 +01:00
"os"
"runtime"
"strings"
"time"
2016-12-01 00:56:15 +01:00
"code.gitea.io/gitea/cmd"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
2018-11-01 14:41:07 +01: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 01:46:58 +01: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 18:49:46 +01: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
)
var ForgejoVersion = "1.0.0"
2014-02-12 20:54:09 +01:00
func init() {
setting.AppVer = Version
setting.ForgejoVersion = ForgejoVersion
2019-06-12 21:41:28 +02: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 07:50:26 +02: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)
2023-02-24 13:38:29 +01: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 18:49:46 +01: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)
2023-02-24 13:38:29 +01:00
forgejoEnv()
cli.OsExiter = func(code int) {
log.GetManager().Close()
os.Exit(code)
2016-12-01 00:56:15 +01: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-22 00:35:11 +02:00
log.GetManager().Close()
2014-02-12 18:49:46 +01:00
}
2019-06-12 21:41:28 +02: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, " ", ", ")
}