Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.4 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "path"
  6. "strings"
  7. "github.com/zenazn/goji/web"
  8. )
  9. func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  10. fileName := c.URLParams["name"]
  11. filePath := path.Join(Config.filesDir, fileName)
  12. if !fileExistsAndNotExpired(fileName) {
  13. notFoundHandler(c, w, r)
  14. return
  15. }
  16. if !Config.allowHotlink {
  17. referer := r.Header.Get("Referer")
  18. if referer != "" && !strings.HasPrefix(referer, Config.siteURL) {
  19. w.WriteHeader(403)
  20. return
  21. }
  22. }
  23. http.ServeFile(w, r, filePath)
  24. }
  25. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  26. path := r.URL.Path
  27. if path[len(path)-1:] == "/" {
  28. notFoundHandler(c, w, r)
  29. return
  30. } else {
  31. if path == "/favicon.ico" {
  32. path = "/static/images/favicon.gif"
  33. }
  34. filePath := strings.TrimPrefix(path, "/static/")
  35. file, err := staticBox.Open(filePath)
  36. if err != nil {
  37. notFoundHandler(c, w, r)
  38. return
  39. }
  40. w.Header().Set("Etag", timeStartedStr)
  41. w.Header().Set("Cache-Control", "max-age=86400")
  42. http.ServeContent(w, r, filePath, timeStarted, file)
  43. return
  44. }
  45. }
  46. func fileExistsAndNotExpired(filename string) bool {
  47. filePath := path.Join(Config.filesDir, filename)
  48. _, err := os.Stat(filePath)
  49. if err != nil {
  50. return false
  51. }
  52. if isFileExpired(filename) {
  53. os.Remove(path.Join(Config.filesDir, filename))
  54. os.Remove(path.Join(Config.metaDir, filename))
  55. return false
  56. }
  57. return true
  58. }