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.

143 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("/favicon.ico", staticHandler)
  88. goji.Get(nameRe, fileDisplayHandler)
  89. goji.Get(selifRe, fileServeHandler)
  90. goji.Get(selifIndexRe, unauthorizedHandler)
  91. goji.Get(torrentRe, fileTorrentHandler)
  92. goji.NotFound(notFoundHandler)
  93. }
  94. func main() {
  95. flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
  96. "host to bind to (default: 127.0.0.1:8080)")
  97. flag.StringVar(&Config.filesDir, "filespath", "files/",
  98. "path to files directory")
  99. flag.StringVar(&Config.metaDir, "metapath", "meta/",
  100. "path to metadata directory")
  101. flag.BoolVar(&Config.noLogs, "nologs", false,
  102. "remove stdout output for each request")
  103. flag.BoolVar(&Config.allowHotlink, "allowhotlink", false,
  104. "Allow hotlinking of files")
  105. flag.StringVar(&Config.siteName, "sitename", "linx",
  106. "name of the site")
  107. flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
  108. "site base url (including trailing slash)")
  109. flag.BoolVar(&Config.fastcgi, "fastcgi", false,
  110. "serve through fastcgi")
  111. flag.BoolVar(&Config.remoteUploads, "remoteuploads", false,
  112. "enable remote uploads")
  113. flag.Parse()
  114. setup()
  115. listener, err := net.Listen("tcp", Config.bind)
  116. if err != nil {
  117. log.Fatal("Could not bind: ", err)
  118. }
  119. if Config.fastcgi {
  120. fcgi.Serve(listener, goji.DefaultMux)
  121. } else {
  122. goji.ServeListener(listener)
  123. }
  124. }