2020-01-29 09:01:06 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-29 09:01:06 +08:00
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
2020-07-06 08:07:07 +08:00
|
|
|
"fmt"
|
2020-01-29 09:01:06 +08:00
|
|
|
"net/http"
|
|
|
|
|
2022-06-24 18:49:47 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-01-29 09:01:06 +08:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2021-07-25 00:03:58 +08:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2020-01-29 09:01:06 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/private"
|
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2020-07-06 08:07:07 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-01-29 09:01:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// FlushQueues flushes all the Queues
|
2021-01-26 23:36:53 +08:00
|
|
|
func FlushQueues(ctx *context.PrivateContext) {
|
|
|
|
opts := web.GetForm(ctx).(*private.FlushOptions)
|
2020-01-29 09:01:06 +08:00
|
|
|
if opts.NonBlocking {
|
|
|
|
// Save the hammer ctx here - as a new one is created each time you call this.
|
|
|
|
baseCtx := graceful.GetManager().HammerContext()
|
|
|
|
go func() {
|
|
|
|
err := queue.GetManager().FlushAll(baseCtx, opts.Timeout)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Flushing request timed-out with error: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-06-24 03:38:19 +08:00
|
|
|
ctx.JSON(http.StatusAccepted, private.Response{
|
Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687)
# Why this PR comes
At first, I'd like to help users like #23636 (there are a lot)
The unclear "Internal Server Error" is quite anonying, scare users,
frustrate contributors, nobody knows what happens.
So, it's always good to provide meaningful messages to end users (of
course, do not leak sensitive information).
When I started working on the "response message to end users", I found
that the related code has a lot of technical debt. A lot of copy&paste
code, unclear fields and usages.
So I think it's good to make everything clear.
# Tech Backgrounds
Gitea has many sub-commands, some are used by admins, some are used by
SSH servers or Git Hooks. Many sub-commands use "internal API" to
communicate with Gitea web server.
Before, Gitea server always use `StatusCode + Json "err" field` to
return messages.
* The CLI sub-commands: they expect to show all error related messages
to site admin
* The Serv/Hook sub-commands (for git clients): they could only show
safe messages to end users, the error log could only be recorded by
"SSHLog" to Gitea web server.
In the old design, it assumes that:
* If the StatusCode is 500 (in some functions), then the "err" field is
error log, shouldn't be exposed to git client.
* If the StatusCode is 40x, then the "err" field could be exposed. And
some functions always read the "err" no matter what the StatusCode is.
The old code is not strict, and it's difficult to distinguish the
messages clearly and then output them correctly.
# This PR
To help to remove duplicate code and make everything clear, this PR
introduces `ResponseExtra` and `requestJSONResp`.
* `ResponseExtra` is a struct which contains "extra" information of a
internal API response, including StatusCode, UserMsg, Error
* `requestJSONResp` is a generic function which can be used for all
cases to help to simplify the calls.
* Remove all `map["err"]`, always use `private.Response{Err}` to
construct error messages.
* User messages and error messages are separated clearly, the `fail` and
`handleCliResponseExtra` will output correct messages.
* Replace all `Internal Server Error` messages with meaningful (still
safe) messages.
This PR saves more than 300 lines, while makes the git client messages
more clear.
Many gitea-serv/git-hook related essential functions are covered by
tests.
---------
Co-authored-by: delvh <dev.lh@web.de>
2023-03-29 14:32:26 +08:00
|
|
|
UserMsg: "Flushing",
|
2020-01-29 09:01:06 +08:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-05-31 14:18:11 +08:00
|
|
|
err := queue.GetManager().FlushAll(ctx, opts.Timeout)
|
2020-01-29 09:01:06 +08:00
|
|
|
if err != nil {
|
2021-06-24 03:38:19 +08:00
|
|
|
ctx.JSON(http.StatusRequestTimeout, private.Response{
|
Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687)
# Why this PR comes
At first, I'd like to help users like #23636 (there are a lot)
The unclear "Internal Server Error" is quite anonying, scare users,
frustrate contributors, nobody knows what happens.
So, it's always good to provide meaningful messages to end users (of
course, do not leak sensitive information).
When I started working on the "response message to end users", I found
that the related code has a lot of technical debt. A lot of copy&paste
code, unclear fields and usages.
So I think it's good to make everything clear.
# Tech Backgrounds
Gitea has many sub-commands, some are used by admins, some are used by
SSH servers or Git Hooks. Many sub-commands use "internal API" to
communicate with Gitea web server.
Before, Gitea server always use `StatusCode + Json "err" field` to
return messages.
* The CLI sub-commands: they expect to show all error related messages
to site admin
* The Serv/Hook sub-commands (for git clients): they could only show
safe messages to end users, the error log could only be recorded by
"SSHLog" to Gitea web server.
In the old design, it assumes that:
* If the StatusCode is 500 (in some functions), then the "err" field is
error log, shouldn't be exposed to git client.
* If the StatusCode is 40x, then the "err" field could be exposed. And
some functions always read the "err" no matter what the StatusCode is.
The old code is not strict, and it's difficult to distinguish the
messages clearly and then output them correctly.
# This PR
To help to remove duplicate code and make everything clear, this PR
introduces `ResponseExtra` and `requestJSONResp`.
* `ResponseExtra` is a struct which contains "extra" information of a
internal API response, including StatusCode, UserMsg, Error
* `requestJSONResp` is a generic function which can be used for all
cases to help to simplify the calls.
* Remove all `map["err"]`, always use `private.Response{Err}` to
construct error messages.
* User messages and error messages are separated clearly, the `fail` and
`handleCliResponseExtra` will output correct messages.
* Replace all `Internal Server Error` messages with meaningful (still
safe) messages.
This PR saves more than 300 lines, while makes the git client messages
more clear.
Many gitea-serv/git-hook related essential functions are covered by
tests.
---------
Co-authored-by: delvh <dev.lh@web.de>
2023-03-29 14:32:26 +08:00
|
|
|
UserMsg: fmt.Sprintf("%v", err),
|
2020-01-29 09:01:06 +08:00
|
|
|
})
|
|
|
|
}
|
2021-12-15 14:59:57 +08:00
|
|
|
ctx.PlainText(http.StatusOK, "success")
|
2020-01-29 09:01:06 +08:00
|
|
|
}
|
2020-07-06 08:07:07 +08:00
|
|
|
|
|
|
|
// PauseLogging pauses logging
|
2021-01-26 23:36:53 +08:00
|
|
|
func PauseLogging(ctx *context.PrivateContext) {
|
2020-07-06 08:07:07 +08:00
|
|
|
log.Pause()
|
2021-12-15 14:59:57 +08:00
|
|
|
ctx.PlainText(http.StatusOK, "success")
|
2020-07-06 08:07:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResumeLogging resumes logging
|
2021-01-26 23:36:53 +08:00
|
|
|
func ResumeLogging(ctx *context.PrivateContext) {
|
2020-07-06 08:07:07 +08:00
|
|
|
log.Resume()
|
2021-12-15 14:59:57 +08:00
|
|
|
ctx.PlainText(http.StatusOK, "success")
|
2020-07-06 08:07:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseReopenLogging releases and reopens logging files
|
2021-01-26 23:36:53 +08:00
|
|
|
func ReleaseReopenLogging(ctx *context.PrivateContext) {
|
2020-07-06 08:07:07 +08:00
|
|
|
if err := log.ReleaseReopen(); err != nil {
|
2021-06-24 03:38:19 +08:00
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: fmt.Sprintf("Error during release and reopen: %v", err),
|
2020-07-06 08:07:07 +08:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-12-15 14:59:57 +08:00
|
|
|
ctx.PlainText(http.StatusOK, "success")
|
2020-07-06 08:07:07 +08:00
|
|
|
}
|
|
|
|
|
2022-06-24 18:49:47 +08:00
|
|
|
// SetLogSQL re-sets database SQL logging
|
|
|
|
func SetLogSQL(ctx *context.PrivateContext) {
|
|
|
|
db.SetLogSQL(ctx, ctx.FormBool("on"))
|
|
|
|
ctx.PlainText(http.StatusOK, "success")
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:07:07 +08:00
|
|
|
// RemoveLogger removes a logger
|
2021-01-26 23:36:53 +08:00
|
|
|
func RemoveLogger(ctx *context.PrivateContext) {
|
2020-07-06 08:07:07 +08:00
|
|
|
group := ctx.Params("group")
|
|
|
|
name := ctx.Params("name")
|
|
|
|
ok, err := log.GetLogger(group).DelLogger(name)
|
|
|
|
if err != nil {
|
2021-06-24 03:38:19 +08:00
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: fmt.Sprintf("Failed to remove logger: %s %s %v", group, name, err),
|
2020-07-06 08:07:07 +08:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
setting.RemoveSubLogDescription(group, name)
|
|
|
|
}
|
2021-12-15 14:59:57 +08:00
|
|
|
ctx.PlainText(http.StatusOK, fmt.Sprintf("Removed %s %s", group, name))
|
2020-07-06 08:07:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddLogger adds a logger
|
2021-01-26 23:36:53 +08:00
|
|
|
func AddLogger(ctx *context.PrivateContext) {
|
|
|
|
opts := web.GetForm(ctx).(*private.LoggerOptions)
|
2020-07-06 08:07:07 +08:00
|
|
|
if len(opts.Group) == 0 {
|
|
|
|
opts.Group = log.DEFAULT
|
|
|
|
}
|
|
|
|
if _, ok := opts.Config["flags"]; !ok {
|
|
|
|
switch opts.Group {
|
|
|
|
case "access":
|
|
|
|
opts.Config["flags"] = log.FlagsFromString("")
|
|
|
|
case "router":
|
|
|
|
opts.Config["flags"] = log.FlagsFromString("date,time")
|
|
|
|
default:
|
|
|
|
opts.Config["flags"] = log.FlagsFromString("stdflags")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := opts.Config["colorize"]; !ok && opts.Mode == "console" {
|
|
|
|
if _, ok := opts.Config["stderr"]; ok {
|
|
|
|
opts.Config["colorize"] = log.CanColorStderr
|
|
|
|
} else {
|
|
|
|
opts.Config["colorize"] = log.CanColorStdout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := opts.Config["level"]; !ok {
|
2023-02-20 00:12:01 +08:00
|
|
|
opts.Config["level"] = setting.Log.Level
|
2020-07-06 08:07:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := opts.Config["stacktraceLevel"]; !ok {
|
2023-02-20 00:12:01 +08:00
|
|
|
opts.Config["stacktraceLevel"] = setting.Log.StacktraceLogLevel
|
2020-07-06 08:07:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Mode == "file" {
|
|
|
|
if _, ok := opts.Config["maxsize"]; !ok {
|
|
|
|
opts.Config["maxsize"] = 1 << 28
|
|
|
|
}
|
|
|
|
if _, ok := opts.Config["maxdays"]; !ok {
|
|
|
|
opts.Config["maxdays"] = 7
|
|
|
|
}
|
|
|
|
if _, ok := opts.Config["compressionLevel"]; !ok {
|
|
|
|
opts.Config["compressionLevel"] = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 00:12:01 +08:00
|
|
|
bufferLen := setting.Log.BufferLength
|
2020-07-06 08:07:07 +08:00
|
|
|
byteConfig, err := json.Marshal(opts.Config)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to marshal log configuration: %v %v", opts.Config, err)
|
2021-06-24 03:38:19 +08:00
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: fmt.Sprintf("Failed to marshal log configuration: %v %v", opts.Config, err),
|
2020-07-06 08:07:07 +08:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config := string(byteConfig)
|
|
|
|
|
|
|
|
if err := log.NewNamedLogger(opts.Group, bufferLen, opts.Name, opts.Mode, config); err != nil {
|
|
|
|
log.Error("Failed to create new named logger: %s %v", config, err)
|
2021-06-24 03:38:19 +08:00
|
|
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
|
|
|
Err: fmt.Sprintf("Failed to create new named logger: %s %v", config, err),
|
2020-07-06 08:07:07 +08:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setting.AddSubLogDescription(opts.Group, setting.SubLogDescription{
|
|
|
|
Name: opts.Name,
|
|
|
|
Provider: opts.Mode,
|
|
|
|
Config: config,
|
|
|
|
})
|
|
|
|
|
2021-12-15 14:59:57 +08:00
|
|
|
ctx.PlainText(http.StatusOK, "success")
|
2020-07-06 08:07:07 +08:00
|
|
|
}
|