logs.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package routes
  14. import (
  15. "net/http"
  16. "path"
  17. "github.com/emicklei/go-restful"
  18. )
  19. // Logs adds handlers for the /logs path serving log files from /var/log.
  20. type Logs struct{}
  21. // Install func registers the logs handler.
  22. func (l Logs) Install(c *restful.Container) {
  23. // use restful: ws.Route(ws.GET("/logs/{logpath:*}").To(fileHandler))
  24. // See github.com/emicklei/go-restful/blob/master/examples/restful-serve-static.go
  25. ws := new(restful.WebService)
  26. ws.Path("/logs")
  27. ws.Doc("get log files")
  28. ws.Route(ws.GET("/{logpath:*}").To(logFileHandler).Param(ws.PathParameter("logpath", "path to the log").DataType("string")))
  29. ws.Route(ws.GET("/").To(logFileListHandler))
  30. c.Add(ws)
  31. }
  32. func logFileHandler(req *restful.Request, resp *restful.Response) {
  33. logdir := "/var/log"
  34. actual := path.Join(logdir, req.PathParameter("logpath"))
  35. http.ServeFile(resp.ResponseWriter, req.Request, actual)
  36. }
  37. func logFileListHandler(req *restful.Request, resp *restful.Response) {
  38. logdir := "/var/log"
  39. http.ServeFile(resp.ResponseWriter, req.Request, logdir)
  40. }