2019-11-15 03:03:56 +00:00
|
|
|
package gophertype
|
|
|
|
|
|
|
|
import (
|
2019-11-27 00:54:02 +00:00
|
|
|
"html/template"
|
2019-11-15 03:03:56 +00:00
|
|
|
"net/http"
|
|
|
|
|
2019-11-26 19:17:01 +00:00
|
|
|
"git.kirsle.net/apps/gophertype/pkg/console"
|
2019-11-27 00:54:02 +00:00
|
|
|
"git.kirsle.net/apps/gophertype/pkg/controllers"
|
2019-11-15 03:03:56 +00:00
|
|
|
"git.kirsle.net/apps/gophertype/pkg/models"
|
2019-11-27 00:54:02 +00:00
|
|
|
"git.kirsle.net/apps/gophertype/pkg/responses"
|
2019-11-26 19:17:01 +00:00
|
|
|
"git.kirsle.net/apps/gophertype/pkg/settings"
|
2019-11-15 03:03:56 +00:00
|
|
|
"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.
|
2019-11-26 19:17:01 +00:00
|
|
|
func NewSite(pubroot string) *Site {
|
|
|
|
// Initialize the settings.json inside the user root.
|
|
|
|
if err := settings.SetFilename(pubroot); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:03:56 +00:00
|
|
|
site := &Site{}
|
|
|
|
|
|
|
|
n := negroni.New()
|
|
|
|
n.Use(negroni.NewRecovery())
|
|
|
|
n.Use(negroni.NewLogger())
|
|
|
|
site.n = n
|
|
|
|
|
2019-11-27 00:54:02 +00:00
|
|
|
// Register blog global template functions.
|
|
|
|
responses.ExtraFuncs = template.FuncMap{
|
2020-02-14 06:03:01 +00:00
|
|
|
"BlogIndex": controllers.PartialBlogIndex,
|
|
|
|
"RenderComments": controllers.RenderComments,
|
|
|
|
"RenderCommentsRO": controllers.RenderCommentsRO,
|
|
|
|
"RenderComment": controllers.RenderComment,
|
|
|
|
"RenderCommentForm": controllers.RenderCommentForm,
|
2019-11-27 00:54:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:03:56 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-26 19:17:01 +00:00
|
|
|
console.Info("Using database driver '%s'", driver)
|
2019-11-15 03:03:56 +00:00
|
|
|
models.UseDB(db)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenAndServe starts the HTTP service.
|
|
|
|
func (s *Site) ListenAndServe(addr string) error {
|
2019-11-26 19:17:01 +00:00
|
|
|
console.Info("Listening on %s", addr)
|
2019-11-15 03:03:56 +00:00
|
|
|
return http.ListenAndServe(addr, s.n)
|
|
|
|
}
|