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.

155 lines
4.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "net"
  6. "net/http"
  7. "net/http/fcgi"
  8. "os"
  9. "regexp"
  10. "strconv"
  11. "time"
  12. "github.com/GeertJohan/go.rice"
  13. "github.com/flosch/pongo2"
  14. "github.com/zenazn/goji"
  15. "github.com/zenazn/goji/web/middleware"
  16. )
  17. var Config struct {
  18. bind string
  19. filesDir string
  20. metaDir string
  21. siteName string
  22. siteURL string
  23. contentSecurityPolicy string
  24. fileContentSecurityPolicy string
  25. xFrameOptions string
  26. noLogs bool
  27. allowHotlink bool
  28. fastcgi bool
  29. remoteUploads bool
  30. }
  31. var Templates = make(map[string]*pongo2.Template)
  32. var TemplateSet *pongo2.TemplateSet
  33. var staticBox *rice.Box
  34. var timeStarted time.Time
  35. var timeStartedStr string
  36. func setup() {
  37. goji.Use(ContentSecurityPolicy(CSPOptions{
  38. policy: Config.contentSecurityPolicy,
  39. frame: Config.xFrameOptions,
  40. }))
  41. if Config.noLogs {
  42. goji.Abandon(middleware.Logger)
  43. }
  44. // make directories if needed
  45. err := os.MkdirAll(Config.filesDir, 0755)
  46. if err != nil {
  47. log.Fatal("Could not create files directory:", err)
  48. }
  49. err = os.MkdirAll(Config.metaDir, 0700)
  50. if err != nil {
  51. log.Fatal("Could not create metadata directory:", err)
  52. }
  53. // ensure siteURL ends wth '/'
  54. if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
  55. Config.siteURL = Config.siteURL + "/"
  56. }
  57. // Template setup
  58. p2l, err := NewPongo2TemplatesLoader()
  59. if err != nil {
  60. log.Fatal("Error: could not load templates", err)
  61. }
  62. TemplateSet := pongo2.NewSet("templates", p2l)
  63. TemplateSet.Globals["sitename"] = Config.siteName
  64. err = populateTemplatesMap(TemplateSet, Templates)
  65. if err != nil {
  66. log.Fatal("Error: could not load templates", err)
  67. }
  68. staticBox = rice.MustFindBox("static")
  69. timeStarted = time.Now()
  70. timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)
  71. // Routing setup
  72. nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
  73. selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
  74. selifIndexRe := regexp.MustCompile(`^/selif/$`)
  75. torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`)
  76. goji.Get("/", indexHandler)
  77. goji.Get("/paste/", pasteHandler)
  78. goji.Get("/paste", http.RedirectHandler("/paste/", 301))
  79. if Config.remoteUploads {
  80. goji.Get("/upload", uploadRemote)
  81. goji.Get("/upload/", uploadRemote)
  82. }
  83. goji.Post("/upload", uploadPostHandler)
  84. goji.Post("/upload/", uploadPostHandler)
  85. goji.Put("/upload", uploadPutHandler)
  86. goji.Put("/upload/:name", uploadPutHandler)
  87. goji.Delete("/:name", deleteHandler)
  88. goji.Get("/static/*", staticHandler)
  89. goji.Get("/favicon.ico", staticHandler)
  90. goji.Get("/robots.txt", staticHandler)
  91. goji.Get(nameRe, fileDisplayHandler)
  92. goji.Get(selifRe, fileServeHandler)
  93. goji.Get(selifIndexRe, unauthorizedHandler)
  94. goji.Get(torrentRe, fileTorrentHandler)
  95. goji.NotFound(notFoundHandler)
  96. }
  97. func main() {
  98. flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
  99. "host to bind to (default: 127.0.0.1:8080)")
  100. flag.StringVar(&Config.filesDir, "filespath", "files/",
  101. "path to files directory")
  102. flag.StringVar(&Config.metaDir, "metapath", "meta/",
  103. "path to metadata directory")
  104. flag.BoolVar(&Config.noLogs, "nologs", false,
  105. "remove stdout output for each request")
  106. flag.BoolVar(&Config.allowHotlink, "allowhotlink", false,
  107. "Allow hotlinking of files")
  108. flag.StringVar(&Config.siteName, "sitename", "linx",
  109. "name of the site")
  110. flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
  111. "site base url (including trailing slash)")
  112. flag.BoolVar(&Config.fastcgi, "fastcgi", false,
  113. "serve through fastcgi")
  114. flag.BoolVar(&Config.remoteUploads, "remoteuploads", false,
  115. "enable remote uploads")
  116. flag.StringVar(&Config.contentSecurityPolicy, "contentsecuritypolicy",
  117. "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; referrer none;",
  118. "value of default Content-Security-Policy header")
  119. flag.StringVar(&Config.fileContentSecurityPolicy, "filecontentsecuritypolicy",
  120. "default-src 'none'; img-src 'self'; object-src 'self'; media-src 'self'; sandbox; referrer none;",
  121. "value of Content-Security-Policy header for file access")
  122. flag.StringVar(&Config.xFrameOptions, "xframeoptions", "SAMEORIGIN",
  123. "value of X-Frame-Options header")
  124. flag.Parse()
  125. setup()
  126. listener, err := net.Listen("tcp", Config.bind)
  127. if err != nil {
  128. log.Fatal("Could not bind: ", err)
  129. }
  130. if Config.fastcgi {
  131. fcgi.Serve(listener, goji.DefaultMux)
  132. } else {
  133. goji.ServeListener(listener)
  134. }
  135. }