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.

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