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.

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