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"
|
2017-11-03 16:52:40 +00:00
|
|
|
"path/filepath"
|
2017-10-08 04:48:58 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2017-11-07 17:53:02 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2017-12-23 21:22:51 +00:00
|
|
|
"github.com/kirsle/blog/core/caches"
|
|
|
|
"github.com/kirsle/blog/core/caches/null"
|
|
|
|
"github.com/kirsle/blog/core/caches/redis"
|
2017-11-03 16:52:40 +00:00
|
|
|
"github.com/kirsle/blog/core/jsondb"
|
2017-11-26 23:53:10 +00:00
|
|
|
"github.com/kirsle/blog/core/models/comments"
|
2017-11-24 19:56:32 +00:00
|
|
|
"github.com/kirsle/blog/core/models/posts"
|
2017-11-08 03:48:22 +00:00
|
|
|
"github.com/kirsle/blog/core/models/settings"
|
2017-11-07 17:01:02 +00:00
|
|
|
"github.com/kirsle/blog/core/models/users"
|
2017-11-24 19:56:32 +00:00
|
|
|
"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 {
|
2017-10-31 16:42:15 +00:00
|
|
|
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-11-03 16:52:40 +00:00
|
|
|
|
2017-10-08 04:48:58 +00:00
|
|
|
// Web app objects.
|
2017-11-07 17:53:02 +00:00
|
|
|
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 {
|
|
|
|
blog := &Blog{
|
|
|
|
DocumentRoot: documentRoot,
|
|
|
|
UserRoot: userRoot,
|
2017-11-03 16:52:40 +00:00
|
|
|
DB: jsondb.New(filepath.Join(userRoot, ".private")),
|
2017-12-23 21:22:51 +00:00
|
|
|
Cache: null.New(),
|
2017-11-08 03:48:22 +00:00
|
|
|
}
|
2017-11-07 17:53:02 +00:00
|
|
|
|
2017-11-08 03:48:22 +00:00
|
|
|
// Load the site config, or start with defaults if not found.
|
|
|
|
settings.DB = blog.DB
|
|
|
|
config, err := settings.Load()
|
|
|
|
if err != nil {
|
|
|
|
config = settings.Defaults()
|
2017-10-08 04:48:58 +00:00
|
|
|
}
|
2017-11-03 16:52:40 +00:00
|
|
|
|
2017-11-08 03:48:22 +00:00
|
|
|
// Initialize the session cookie store.
|
|
|
|
blog.store = sessions.NewCookieStore([]byte(config.Security.SecretKey))
|
|
|
|
users.HashCost = config.Security.HashCost
|
|
|
|
|
|
|
|
// Initialize the rest of the models.
|
2017-11-24 19:56:32 +00:00
|
|
|
posts.DB = blog.DB
|
2017-11-07 17:01:02 +00:00
|
|
|
users.DB = blog.DB
|
2017-11-26 23:53:10 +00:00
|
|
|
comments.DB = blog.DB
|
2017-11-07 17:01:02 +00:00
|
|
|
|
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 {
|
|
|
|
blog.Cache = cache
|
|
|
|
blog.DB.Cache = cache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 03:48:22 +00:00
|
|
|
// Initialize the router.
|
2017-10-08 04:48:58 +00:00
|
|
|
r := mux.NewRouter()
|
2017-11-15 14:55:15 +00:00
|
|
|
r.HandleFunc("/initial-setup", blog.SetupHandler)
|
2017-12-24 03:15:50 +00:00
|
|
|
blog.AuthRoutes(r)
|
2017-11-15 14:55:15 +00:00
|
|
|
blog.AdminRoutes(r)
|
2017-12-23 02:34:58 +00:00
|
|
|
blog.ContactRoutes(r)
|
2017-11-20 05:49:19 +00:00
|
|
|
blog.BlogRoutes(r)
|
2017-11-26 23:53:10 +00:00
|
|
|
blog.CommentRoutes(r)
|
2017-11-15 14:55:15 +00:00
|
|
|
|
2017-11-24 19:56:32 +00:00
|
|
|
// GitHub Flavored Markdown CSS.
|
|
|
|
r.Handle("/css/gfm.css", http.StripPrefix("/css", http.FileServer(gfmstyle.Assets)))
|
|
|
|
|
2017-11-15 14:55:15 +00:00
|
|
|
r.PathPrefix("/").HandlerFunc(blog.PageHandler)
|
2017-10-08 04:48:58 +00:00
|
|
|
r.NotFoundHandler = http.HandlerFunc(blog.PageHandler)
|
|
|
|
|
|
|
|
n := negroni.New(
|
|
|
|
negroni.NewRecovery(),
|
|
|
|
negroni.NewLogger(),
|
2017-11-15 14:55:15 +00:00
|
|
|
negroni.HandlerFunc(blog.SessionLoader),
|
2017-11-24 19:56:32 +00:00
|
|
|
negroni.HandlerFunc(blog.CSRFMiddleware),
|
2017-11-15 14:55:15 +00:00
|
|
|
negroni.HandlerFunc(blog.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.
|
|
|
|
blog.n = n
|
|
|
|
blog.r = r
|
|
|
|
|
2017-10-08 04:48:58 +00:00
|
|
|
return blog
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|