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.
 
 

47 lines
861 B

  1. package gophertype
  2. import (
  3. "log"
  4. "net/http"
  5. "git.kirsle.net/apps/gophertype/pkg/models"
  6. "github.com/gorilla/mux"
  7. "github.com/jinzhu/gorm"
  8. "github.com/urfave/negroni"
  9. )
  10. // Site is the master struct for the Gophertype server.
  11. type Site struct {
  12. n *negroni.Negroni
  13. mux *mux.Router
  14. }
  15. // NewSite initializes the Site.
  16. func NewSite() *Site {
  17. site := &Site{}
  18. n := negroni.New()
  19. n.Use(negroni.NewRecovery())
  20. n.Use(negroni.NewLogger())
  21. site.n = n
  22. return site
  23. }
  24. // UseDB specifies the database to use.
  25. func (s *Site) UseDB(driver string, path string) error {
  26. db, err := gorm.Open(driver, path)
  27. if err != nil {
  28. return err
  29. }
  30. log.Printf("Using database driver '%s'", driver)
  31. models.UseDB(db)
  32. return nil
  33. }
  34. // ListenAndServe starts the HTTP service.
  35. func (s *Site) ListenAndServe(addr string) error {
  36. return http.ListenAndServe(addr, s.n)
  37. }