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.

96 lines
2.2 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. "os"
  9. "regexp"
  10. "github.com/flosch/pongo2"
  11. "github.com/zenazn/goji"
  12. "github.com/zenazn/goji/web/middleware"
  13. )
  14. var Config struct {
  15. bind string
  16. filesDir string
  17. metaDir string
  18. noLogs bool
  19. siteName string
  20. siteURL string
  21. }
  22. func setup() {
  23. if Config.noLogs {
  24. goji.Abandon(middleware.Logger)
  25. }
  26. // make directories if needed
  27. var err error
  28. err = os.MkdirAll(Config.filesDir, 0755)
  29. if err != nil {
  30. fmt.Println("Error: could not create files directory")
  31. os.Exit(1)
  32. }
  33. err = os.MkdirAll(Config.metaDir, 0700)
  34. if err != nil {
  35. fmt.Println("Error: could not create metadata directory")
  36. os.Exit(1)
  37. }
  38. // ensure siteURL ends wth '/'
  39. if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
  40. Config.siteURL = Config.siteURL + "/"
  41. }
  42. // Template Globals
  43. pongo2.DefaultSet.Globals["sitename"] = Config.siteName
  44. // Routing setup
  45. nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
  46. selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
  47. goji.Get("/", indexHandler)
  48. goji.Post("/upload", uploadPostHandler)
  49. goji.Post("/upload/", http.RedirectHandler("/upload", 301))
  50. goji.Put("/upload", uploadPutHandler)
  51. goji.Put("/upload/:name", uploadPutHandler)
  52. goji.Get("/static/*", http.StripPrefix("/static/",
  53. http.FileServer(http.Dir("static/"))))
  54. goji.Get(nameRe, fileDisplayHandler)
  55. goji.Get(selifRe, fileServeHandler)
  56. goji.NotFound(notFoundHandler)
  57. }
  58. func main() {
  59. flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
  60. "host to bind to (default: 127.0.0.1:8080)")
  61. flag.StringVar(&Config.filesDir, "filespath", "files/",
  62. "path to files directory")
  63. flag.StringVar(&Config.metaDir, "metapath", "meta/",
  64. "path to metadata directory")
  65. flag.BoolVar(&Config.noLogs, "nologs", false,
  66. "remove stdout output for each request")
  67. flag.StringVar(&Config.siteName, "sitename", "linx",
  68. "name of the site")
  69. flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
  70. "site base url (including trailing slash)")
  71. flag.Parse()
  72. setup()
  73. listener, err := net.Listen("tcp", Config.bind)
  74. if err != nil {
  75. log.Fatal("Could not bind: ", err)
  76. }
  77. goji.ServeListener(listener)
  78. }