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.

142 lines
3.6 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. }
  29. var Templates = make(map[string]*pongo2.Template)
  30. var TemplateSet *pongo2.TemplateSet
  31. var staticBox *rice.Box
  32. var timeStarted time.Time
  33. var timeStartedStr string
  34. func setup() {
  35. if Config.noLogs {
  36. goji.Abandon(middleware.Logger)
  37. }
  38. // make directories if needed
  39. err := os.MkdirAll(Config.filesDir, 0755)
  40. if err != nil {
  41. fmt.Println("Error: could not create files directory")
  42. os.Exit(1)
  43. }
  44. err = os.MkdirAll(Config.metaDir, 0700)
  45. if err != nil {
  46. fmt.Println("Error: could not create metadata directory")
  47. os.Exit(1)
  48. }
  49. // ensure siteURL ends wth '/'
  50. if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
  51. Config.siteURL = Config.siteURL + "/"
  52. }
  53. // Template setup
  54. p2l, err := NewPongo2TemplatesLoader()
  55. if err != nil {
  56. fmt.Println("Error: could not load templates")
  57. os.Exit(1)
  58. }
  59. TemplateSet := pongo2.NewSet("templates", p2l)
  60. TemplateSet.Globals["sitename"] = Config.siteName
  61. err = populateTemplatesMap(TemplateSet, Templates)
  62. if err != nil {
  63. fmt.Println("Error: could not load templates")
  64. os.Exit(1)
  65. }
  66. staticBox = rice.MustFindBox("static")
  67. timeStarted = time.Now()
  68. timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)
  69. // Routing setup
  70. nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
  71. selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
  72. selifIndexRe := regexp.MustCompile(`^/selif/$`)
  73. torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`)
  74. goji.Get("/", indexHandler)
  75. goji.Get("/paste/", pasteHandler)
  76. goji.Get("/paste", http.RedirectHandler("/paste/", 301))
  77. if Config.remoteUploads {
  78. goji.Get("/upload", uploadRemote)
  79. goji.Get("/upload/", uploadRemote)
  80. }
  81. goji.Post("/upload", uploadPostHandler)
  82. goji.Post("/upload/", uploadPostHandler)
  83. goji.Put("/upload", uploadPutHandler)
  84. goji.Put("/upload/:name", uploadPutHandler)
  85. goji.Delete("/:name", deleteHandler)
  86. goji.Get("/static/*", staticHandler)
  87. goji.Get(nameRe, fileDisplayHandler)
  88. goji.Get(selifRe, fileServeHandler)
  89. goji.Get(selifIndexRe, unauthorizedHandler)
  90. goji.Get(torrentRe, fileTorrentHandler)
  91. goji.NotFound(notFoundHandler)
  92. }
  93. func main() {
  94. flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
  95. "host to bind to (default: 127.0.0.1:8080)")
  96. flag.StringVar(&Config.filesDir, "filespath", "files/",
  97. "path to files directory")
  98. flag.StringVar(&Config.metaDir, "metapath", "meta/",
  99. "path to metadata directory")
  100. flag.BoolVar(&Config.noLogs, "nologs", false,
  101. "remove stdout output for each request")
  102. flag.BoolVar(&Config.allowHotlink, "allowhotlink", false,
  103. "Allow hotlinking of files")
  104. flag.StringVar(&Config.siteName, "sitename", "linx",
  105. "name of the site")
  106. flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
  107. "site base url (including trailing slash)")
  108. flag.BoolVar(&Config.fastcgi, "fastcgi", false,
  109. "serve through fastcgi")
  110. flag.BoolVar(&Config.remoteUploads, "remoteuploads", false,
  111. "enable remote uploads")
  112. flag.Parse()
  113. setup()
  114. listener, err := net.Listen("tcp", Config.bind)
  115. if err != nil {
  116. log.Fatal("Could not bind: ", err)
  117. }
  118. if Config.fastcgi {
  119. fcgi.Serve(listener, goji.DefaultMux)
  120. } else {
  121. goji.ServeListener(listener)
  122. }
  123. }