A web blog and personal homepage engine written in Go.
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.

66 lines
1.6 KiB

  1. package gophertype
  2. import (
  3. "html/template"
  4. "net/http"
  5. "git.kirsle.net/apps/gophertype/pkg/console"
  6. "git.kirsle.net/apps/gophertype/pkg/controllers"
  7. "git.kirsle.net/apps/gophertype/pkg/models"
  8. "git.kirsle.net/apps/gophertype/pkg/responses"
  9. "git.kirsle.net/apps/gophertype/pkg/settings"
  10. "github.com/gorilla/mux"
  11. "github.com/jinzhu/gorm"
  12. "github.com/urfave/negroni"
  13. )
  14. // Site is the master struct for the Gophertype server.
  15. type Site struct {
  16. n *negroni.Negroni
  17. mux *mux.Router
  18. }
  19. // NewSite initializes the Site.
  20. func NewSite(pubroot string) *Site {
  21. // Initialize the settings.json inside the user root.
  22. if err := settings.SetFilename(pubroot); err != nil {
  23. panic(err)
  24. }
  25. site := &Site{}
  26. n := negroni.New()
  27. n.Use(negroni.NewRecovery())
  28. n.Use(negroni.NewLogger())
  29. site.n = n
  30. // Register blog global template functions.
  31. responses.ExtraFuncs = template.FuncMap{
  32. "BlogIndex": controllers.PartialBlogIndex,
  33. "RenderComments": controllers.RenderComments,
  34. "RenderCommentsRO": controllers.RenderCommentsRO,
  35. "RenderComment": controllers.RenderComment,
  36. "RenderCommentForm": controllers.RenderCommentForm,
  37. }
  38. return site
  39. }
  40. // UseDB specifies the database to use.
  41. func (s *Site) UseDB(driver string, path string) error {
  42. db, err := gorm.Open(driver, path)
  43. if err != nil {
  44. return err
  45. }
  46. console.Info("Using database driver '%s'", driver)
  47. models.UseDB(db)
  48. return nil
  49. }
  50. // ListenAndServe starts the HTTP service.
  51. func (s *Site) ListenAndServe(addr string) error {
  52. console.Info("Listening on %s", addr)
  53. return http.ListenAndServe(addr, s.n)
  54. }