blog/core/core.go

139 lines
3.6 KiB
Go
Raw Normal View History

// Package core implements the core source code of kirsle/blog.
2017-10-08 04:48:58 +00:00
package core
import (
2017-12-23 21:22:51 +00:00
"fmt"
2017-10-08 04:48:58 +00:00
"net/http"
"path/filepath"
2017-10-08 04:48:58 +00:00
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/kirsle/blog/core/internal/log"
"github.com/kirsle/blog/core/internal/markdown"
2018-02-10 02:46:58 +00:00
"github.com/kirsle/blog/core/internal/models/comments"
"github.com/kirsle/blog/core/internal/models/posts"
"github.com/kirsle/blog/core/internal/models/settings"
"github.com/kirsle/blog/core/internal/models/users"
"github.com/kirsle/blog/core/internal/render"
"github.com/kirsle/blog/jsondb"
"github.com/kirsle/blog/jsondb/caches"
"github.com/kirsle/blog/jsondb/caches/null"
"github.com/kirsle/blog/jsondb/caches/redis"
"github.com/shurcooL/github_flavored_markdown/gfmstyle"
2017-10-08 04:48:58 +00:00
"github.com/urfave/negroni"
)
// Blog is the root application object that maintains the app configuration
// and helper objects.
type Blog struct {
Debug bool
2017-10-08 04:48:58 +00:00
// DocumentRoot is the core static files root; UserRoot masks over it.
DocumentRoot string
UserRoot string
2017-12-23 21:22:51 +00:00
DB *jsondb.DB
Cache caches.Cacher
2017-10-08 04:48:58 +00:00
// Web app objects.
n *negroni.Negroni // Negroni middleware manager
r *mux.Router // Router
store sessions.Store
2017-10-08 04:48:58 +00:00
}
// New initializes the Blog application.
func New(documentRoot, userRoot string) *Blog {
return &Blog{
2017-10-08 04:48:58 +00:00
DocumentRoot: documentRoot,
UserRoot: userRoot,
DB: jsondb.New(filepath.Join(userRoot, ".private")),
2017-12-23 21:22:51 +00:00
Cache: null.New(),
}
}
// Run quickly configures and starts the HTTP server.
func (b *Blog) Run(address string) {
b.Configure()
b.SetupHTTP()
b.ListenAndServe(address)
}
// Configure initializes (or reloads) the blog's configuration, and binds the
// settings in sub-packages.
func (b *Blog) Configure() {
// Load the site config, or start with defaults if not found.
settings.DB = b.DB
config, err := settings.Load()
if err != nil {
config = settings.Defaults()
2017-10-08 04:48:58 +00:00
}
// Bind configs in sub-packages.
render.UserRoot = &b.UserRoot
render.DocumentRoot = &b.DocumentRoot
// Initialize the session cookie store.
b.store = sessions.NewCookieStore([]byte(config.Security.SecretKey))
users.HashCost = config.Security.HashCost
// Initialize the rest of the models.
posts.DB = b.DB
users.DB = b.DB
comments.DB = b.DB
2017-12-23 21:22:51 +00:00
// Redis cache?
if config.Redis.Enabled {
addr := fmt.Sprintf("%s:%d", config.Redis.Host, config.Redis.Port)
log.Info("Connecting to Redis at %s/%d", addr, config.Redis.DB)
cache, err := redis.New(
addr,
config.Redis.DB,
config.Redis.Prefix,
)
if err != nil {
log.Error("Redis init error: %s", err.Error())
} else {
b.Cache = cache
b.DB.Cache = cache
markdown.Cache = cache
2017-12-23 21:22:51 +00:00
}
}
}
2017-12-23 21:22:51 +00:00
// SetupHTTP initializes the Negroni middleware engine and registers routes.
func (b *Blog) SetupHTTP() {
// Initialize the router.
2017-10-08 04:48:58 +00:00
r := mux.NewRouter()
r.HandleFunc("/initial-setup", b.SetupHandler)
b.AuthRoutes(r)
b.AdminRoutes(r)
b.ContactRoutes(r)
b.BlogRoutes(r)
b.CommentRoutes(r)
2017-11-15 14:55:15 +00:00
// GitHub Flavored Markdown CSS.
r.Handle("/css/gfm.css", http.StripPrefix("/css", http.FileServer(gfmstyle.Assets)))
r.PathPrefix("/").HandlerFunc(b.PageHandler)
r.NotFoundHandler = http.HandlerFunc(b.PageHandler)
2017-10-08 04:48:58 +00:00
n := negroni.New(
negroni.NewRecovery(),
negroni.NewLogger(),
negroni.HandlerFunc(b.SessionLoader),
negroni.HandlerFunc(b.CSRFMiddleware),
negroni.HandlerFunc(b.AuthMiddleware),
2017-10-08 04:48:58 +00:00
)
n.UseHandler(r)
2017-11-15 14:55:15 +00:00
// Keep references handy elsewhere in the app.
b.n = n
b.r = r
2017-10-08 04:48:58 +00:00
}
// ListenAndServe begins listening on the given bind address.
func (b *Blog) ListenAndServe(address string) {
log.Info("Listening on %s", address)
http.ListenAndServe(address, b.n)
}