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.

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