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.

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