add download log api

This commit is contained in:
qyzh 2020-11-14 21:53:37 +08:00
parent 028aae2806
commit 9ee9bc6945
5 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,13 @@
// package userlog
// import (
// "fmt"
// "path"
// "code.gitea.io/gitea/modules/log"
// "code.gitea.io/gitea/modules/setting"
// )
// func init(){
// filename := path.Join(setting.LogRootPath, "userlog.log")
// log.NewLogger(1000, "userlog", "file", fmt.Sprintf(`{"filename":"%s"}`, filename))
// }

View File

@ -82,6 +82,7 @@ import (
"code.gitea.io/gitea/routers/api/v1/settings"
_ "code.gitea.io/gitea/routers/api/v1/swagger" // for swagger generation
"code.gitea.io/gitea/routers/api/v1/user"
"code.gitea.io/gitea/routers/api/v1/trace"
"gitea.com/macaron/binding"
"gitea.com/macaron/macaron"
@ -970,6 +971,12 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/topics", func() {
m.Get("/search", repo.TopicSearch)
})
m.Group("/log", func() {
m.Get("/list", trace.GetLogList)
m.Get("/download/:filename", trace.DownloadLog)
// m.Combo("/download").Post(bind(trace.LogDownloadOptions{}), trace.DownloadLog)
})
}, securityHeaders(), context.APIContexter(), sudo())
}

View File

@ -0,0 +1,62 @@
package trace
import (
// "fmt"
"os"
// "path/filepath"
"io/ioutil"
// "net/http"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/context"
)
type LogDownloadOptions struct {
FileName string `json:"filename" binding:"Required"`
}
func GetLogList(ctx *context.APIContext) {
var logList []string
dir_list, err := ioutil.ReadDir(setting.LogRootPath)
if err != nil {
ctx.ServerError("logList", err)
return
}
for i:=0; i<len(dir_list); i++ {
// fmt.Println(i, "=", v.Name())
logList = append(logList, dir_list[i].Name())
}
ctx.JSON(200, &logList)
}
type NotFoundError struct {
Message string `json:"message"`
}
func DownloadLog(ctx *context.APIContext) {
file_path := ctx.Params(":filename")
// file_path := data.FileName
path := setting.LogRootPath+"/"+file_path
b, err := PathExists(path)
if !b || err != nil {
// ctx.Error(http.StatusNotFound, "NoSuchFile", fmt.Sprintf("no such file: %s", file_path))
ctx.JSON(200, &NotFoundError{Message: "no such file!"})
} else {
ctx.ServeFile(path, file_path)
}
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

View File

@ -11,6 +11,7 @@ import (
gocontext "context"
"fmt"
"io/ioutil"
// "io"
"net/http"
"os"
"os/exec"
@ -570,6 +571,11 @@ func serviceRPC(h serviceHandler, service string) {
cmd.Stdin = reqBody
cmd.Stderr = &stderr
// var gitcontent bytes.Buffer
// _ = io.MultiWriter(h.w, &gitcontent)
// accessLogger := log.GetLogger("access")
// accessLogger.SendLog(log.INFO, "", "", 0, gitcontent.String(), "")
pid := process.GetManager().Add(fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir), cancel)
defer process.GetManager().Remove(pid)