47 lines
861 B
Go
47 lines
861 B
Go
package gophertype
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"git.kirsle.net/apps/gophertype/pkg/models"
|
|
"github.com/gorilla/mux"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/urfave/negroni"
|
|
)
|
|
|
|
// Site is the master struct for the Gophertype server.
|
|
type Site struct {
|
|
n *negroni.Negroni
|
|
mux *mux.Router
|
|
}
|
|
|
|
// NewSite initializes the Site.
|
|
func NewSite() *Site {
|
|
site := &Site{}
|
|
|
|
n := negroni.New()
|
|
n.Use(negroni.NewRecovery())
|
|
n.Use(negroni.NewLogger())
|
|
site.n = n
|
|
|
|
return site
|
|
}
|
|
|
|
// UseDB specifies the database to use.
|
|
func (s *Site) UseDB(driver string, path string) error {
|
|
db, err := gorm.Open(driver, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Printf("Using database driver '%s'", driver)
|
|
models.UseDB(db)
|
|
return nil
|
|
}
|
|
|
|
// ListenAndServe starts the HTTP service.
|
|
func (s *Site) ListenAndServe(addr string) error {
|
|
return http.ListenAndServe(addr, s.n)
|
|
}
|