...
1
16
17 package routes
18
19 import (
20 "net/http"
21 "os"
22 "path"
23
24 "github.com/emicklei/go-restful/v3"
25 )
26
27
28 type Logs struct{}
29
30
31 func (l Logs) Install(c *restful.Container) {
32
33
34 ws := new(restful.WebService)
35 ws.Path("/logs")
36 ws.Doc("get log files")
37 ws.Route(ws.GET("/{logpath:*}").To(logFileHandler).Param(ws.PathParameter("logpath", "path to the log").DataType("string")))
38 ws.Route(ws.GET("/").To(logFileListHandler))
39
40 c.Add(ws)
41 }
42
43 func logFileHandler(req *restful.Request, resp *restful.Response) {
44 logdir := "/var/log"
45 actual := path.Join(logdir, req.PathParameter("logpath"))
46
47
48 if logFileNameIsTooLong(actual) {
49 http.Error(resp, "file not found", http.StatusNotFound)
50 return
51 }
52 http.ServeFile(resp.ResponseWriter, req.Request, actual)
53 }
54
55 func logFileListHandler(req *restful.Request, resp *restful.Response) {
56 logdir := "/var/log"
57 http.ServeFile(resp.ResponseWriter, req.Request, logdir)
58 }
59
60
61
62 func logFileNameIsTooLong(filePath string) bool {
63 _, err := os.Stat(filePath)
64 if err != nil {
65 if e, ok := err.(*os.PathError); ok && e.Err == fileNameTooLong {
66 return true
67 }
68 }
69 return false
70 }
71
View as plain text