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.

109 lines
2.6 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. 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. goji.Get("/", indexHandler)
  61. goji.Post("/upload", uploadPostHandler)
  62. goji.Post("/upload/", uploadPostHandler)
  63. goji.Put("/upload", uploadPutHandler)
  64. goji.Put("/upload/:name", uploadPutHandler)
  65. staticBox := rice.MustFindBox("static")
  66. goji.Get("/static/*", http.StripPrefix("/static/",
  67. http.FileServer(staticBox.HTTPBox())))
  68. goji.Get(nameRe, fileDisplayHandler)
  69. goji.Get(selifRe, fileServeHandler)
  70. goji.NotFound(notFoundHandler)
  71. }
  72. func main() {
  73. flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
  74. "host to bind to (default: 127.0.0.1:8080)")
  75. flag.StringVar(&Config.filesDir, "filespath", "files/",
  76. "path to files directory")
  77. flag.StringVar(&Config.metaDir, "metapath", "meta/",
  78. "path to metadata directory")
  79. flag.BoolVar(&Config.noLogs, "nologs", false,
  80. "remove stdout output for each request")
  81. flag.StringVar(&Config.siteName, "sitename", "linx",
  82. "name of the site")
  83. flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
  84. "site base url (including trailing slash)")
  85. flag.Parse()
  86. setup()
  87. listener, err := net.Listen("tcp", Config.bind)
  88. if err != nil {
  89. log.Fatal("Could not bind: ", err)
  90. }
  91. goji.ServeListener(listener)
  92. }