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.

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