Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0,4,5 alpha3 #2011

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions api/casaos/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ paths:
"500":
$ref: "#/components/responses/ResponseInternalServerError"

/health/logs:
get:
tags:
- Health methods
summary: Get log
operationId: getHealthlogs
responses:
"200":
description: OK
content:
application/octet-stream:
schema:
type: string
format: binary
"500":
$ref: "#/components/responses/ResponseInternalServerError"
/file/test:
get:
tags:
Expand Down
52 changes: 34 additions & 18 deletions codegen/casaos_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,9 @@ func AddFile(ar archiver.Writer, path, commonPath string) error {
defer file.Close()

if path != commonPath {
filename := info.Name()
//filename := info.Name()
filename := strings.TrimPrefix(path, commonPath)
filename = strings.TrimPrefix(filename, string(filepath.Separator))
err = ar.Write(archiver.File{
FileInfo: archiver.FileInfo{
FileInfo: info,
Expand Down
5 changes: 4 additions & 1 deletion route/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func InitV2Router() http.Handler {
},
TokenLookupFuncs: []echo_middleware.ValuesExtractor{
func(c echo.Context) ([]string, error) {
return []string{c.Request().Header.Get(echo.HeaderAuthorization)}, nil
if len(c.Request().Header.Get(echo.HeaderAuthorization)) > 0 {
return []string{c.Request().Header.Get(echo.HeaderAuthorization)}, nil
}
return []string{c.QueryParam("token")}, nil
},
},
}))
Expand Down
57 changes: 57 additions & 0 deletions route/v2/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package v2

import (
"net/http"
"net/url"
"os"
"path/filepath"

"github.com/IceWhaleTech/CasaOS/codegen"
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
"github.com/IceWhaleTech/CasaOS/service"
"github.com/labstack/echo/v4"
"github.com/mholt/archiver/v3"
)

func (s *CasaOS) GetHealthServices(ctx echo.Context) error {
Expand Down Expand Up @@ -41,3 +46,55 @@ func (s *CasaOS) GetHealthPorts(ctx echo.Context) error {
},
})
}
func (c *CasaOS) GetHealthlogs(ctx echo.Context) error {
var name, currentPath, commonDir, extension string
var err error
var ar archiver.Writer
fileList, err := os.ReadDir("/var/log/casaos")
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{
Message: &message,
})
}
extension, ar, err = file.GetCompressionAlgorithm("zip")
if err != nil {
ctx.Response().Header().Set("Content-Type", "application/json")
message := err.Error()
return ctx.JSON(http.StatusNotFound, codegen.ResponseInternalServerError{
Message: &message,
})
}
err = ar.Create(ctx.Response().Writer)
if err != nil {
ctx.Response().Header().Set("Content-Type", "application/json")
message := err.Error()
return ctx.JSON(http.StatusNotFound, codegen.ResponseInternalServerError{
Message: &message,
})
}
defer ar.Close()

commonDir = "/var/log/casaos"

currentPath = filepath.Base(commonDir)

name = currentPath
name += extension
ctx.Response().Header().Add("Content-Type", "application/octet-stream")
ctx.Response().Header().Add("Content-Transfer-Encoding", "binary")
ctx.Response().Header().Add("Cache-Control", "no-cache")
ctx.Response().Header().Add("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))

for _, fname := range fileList {
err := file.AddFile(ar, filepath.Join("/var/log/casaos", fname.Name()), commonDir)
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{
Message: &message,
})
}

}
return nil
}