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.

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