mirror of https://github.com/jlelse/GoBlog
308 lines
9.2 KiB
Go
308 lines
9.2 KiB
Go
package main
|
|
|
|
import (
|
|
"compress/flate"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"sync/atomic"
|
|
|
|
"github.com/caddyserver/certmagic"
|
|
"github.com/go-chi/chi"
|
|
"github.com/go-chi/chi/middleware"
|
|
"github.com/gorilla/handlers"
|
|
)
|
|
|
|
const (
|
|
contentType = "Content-Type"
|
|
|
|
charsetUtf8Suffix = "; charset=utf-8"
|
|
|
|
contentTypeHTML = "text/html"
|
|
contentTypeJSON = "application/json"
|
|
contentTypeWWWForm = "application/x-www-form-urlencoded"
|
|
contentTypeMultipartForm = "multipart/form-data"
|
|
contentTypeAS = "application/activity+json"
|
|
|
|
contentTypeHTMLUTF8 = contentTypeHTML + charsetUtf8Suffix
|
|
contentTypeJSONUTF8 = contentTypeJSON + charsetUtf8Suffix
|
|
contentTypeASUTF8 = contentTypeAS + charsetUtf8Suffix
|
|
|
|
userAgent = "User-Agent"
|
|
appUserAgent = "GoBlog"
|
|
)
|
|
|
|
var (
|
|
d *dynamicHandler
|
|
logMiddleware func(next http.Handler) http.Handler
|
|
authMiddleware func(next http.Handler) http.Handler
|
|
)
|
|
|
|
func startServer() (err error) {
|
|
// Init
|
|
if appConfig.Server.Logging {
|
|
f, err := os.OpenFile(appConfig.Server.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
logMiddleware = func(next http.Handler) http.Handler {
|
|
lh := handlers.CombinedLoggingHandler(f, next)
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
// Remove remote address for privacy
|
|
r.RemoteAddr = "127.0.0.1"
|
|
lh.ServeHTTP(rw, r)
|
|
})
|
|
}
|
|
}
|
|
authMiddleware = middleware.BasicAuth("", map[string]string{
|
|
appConfig.User.Nick: appConfig.User.Password,
|
|
})
|
|
// Start
|
|
d = &dynamicHandler{}
|
|
err = reloadRouter()
|
|
if err != nil {
|
|
return
|
|
}
|
|
localAddress := ":" + strconv.Itoa(appConfig.Server.Port)
|
|
if appConfig.Server.PublicHTTPS {
|
|
certmagic.Default.Storage = &certmagic.FileStorage{Path: "data/https"}
|
|
certmagic.DefaultACME.Agreed = true
|
|
certmagic.DefaultACME.Email = appConfig.Server.LetsEncryptMail
|
|
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA
|
|
err = certmagic.HTTPS([]string{appConfig.Server.Domain}, securityHeaders(d))
|
|
} else {
|
|
err = http.ListenAndServe(localAddress, d)
|
|
}
|
|
return
|
|
}
|
|
|
|
func reloadRouter() error {
|
|
h, err := buildHandler()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
purgeCache()
|
|
d.swapHandler(h)
|
|
return nil
|
|
}
|
|
|
|
func buildHandler() (http.Handler, error) {
|
|
r := chi.NewRouter()
|
|
|
|
if appConfig.Server.Logging {
|
|
r.Use(logMiddleware)
|
|
}
|
|
// r.Use(middleware.Logger)
|
|
r.Use(middleware.Recoverer)
|
|
r.Use(middleware.Compress(flate.DefaultCompression))
|
|
r.Use(middleware.RedirectSlashes)
|
|
r.Use(middleware.GetHead)
|
|
if !appConfig.Cache.Enable {
|
|
r.Use(middleware.NoCache)
|
|
}
|
|
|
|
// Profiler
|
|
if appConfig.Server.Debug {
|
|
r.Mount("/debug", middleware.Profiler())
|
|
}
|
|
|
|
// API
|
|
r.Route("/api", func(apiRouter chi.Router) {
|
|
apiRouter.Use(middleware.NoCache, authMiddleware)
|
|
apiRouter.Post("/hugo", apiPostCreateHugo)
|
|
})
|
|
|
|
// Micropub
|
|
r.Route(micropubPath, func(mpRouter chi.Router) {
|
|
mpRouter.Use(checkIndieAuth)
|
|
mpRouter.With(middleware.NoCache).Get("/", serveMicropubQuery)
|
|
mpRouter.Post("/", serveMicropubPost)
|
|
if appConfig.Micropub.MediaStorage != nil {
|
|
mpRouter.Post(micropubMediaSubPath, serveMicropubMedia)
|
|
}
|
|
})
|
|
|
|
// IndieAuth
|
|
r.Route("/indieauth", func(indieauthRouter chi.Router) {
|
|
indieauthRouter.Use(middleware.NoCache)
|
|
indieauthRouter.With(authMiddleware, minifier.Middleware).Get("/", indieAuthAuthGet)
|
|
indieauthRouter.With(authMiddleware).Post("/accept", indieAuthAccept)
|
|
indieauthRouter.Post("/", indieAuthAuthPost)
|
|
indieauthRouter.Get("/token", indieAuthToken)
|
|
indieauthRouter.Post("/token", indieAuthToken)
|
|
})
|
|
|
|
// ActivityPub and stuff
|
|
if appConfig.ActivityPub.Enabled {
|
|
r.Post("/activitypub/inbox/{blog}", apHandleInbox)
|
|
r.Post("/activitypub/{blog}/inbox", apHandleInbox)
|
|
r.Get("/.well-known/webfinger", apHandleWebfinger)
|
|
r.Get("/.well-known/host-meta", handleWellKnownHostMeta)
|
|
}
|
|
|
|
// Webmentions
|
|
r.Route("/webmention", func(webmentionRouter chi.Router) {
|
|
webmentionRouter.Use(middleware.NoCache)
|
|
webmentionRouter.Post("/", handleWebmention)
|
|
webmentionRouter.With(authMiddleware, minifier.Middleware).Get("/admin", webmentionAdmin)
|
|
webmentionRouter.With(authMiddleware).Post("/admin/delete/{id:\\d+}", webmentionAdminDelete)
|
|
webmentionRouter.With(authMiddleware).Post("/admin/approve/{id:\\d+}", webmentionAdminApprove)
|
|
})
|
|
|
|
// Posts
|
|
allPostPaths, err := allPostPaths()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var postMW []func(http.Handler) http.Handler
|
|
if appConfig.ActivityPub.Enabled {
|
|
postMW = []func(http.Handler) http.Handler{manipulateAsPath, cacheMiddleware, minifier.Middleware}
|
|
} else {
|
|
postMW = []func(http.Handler) http.Handler{cacheMiddleware, minifier.Middleware}
|
|
}
|
|
for _, path := range allPostPaths {
|
|
if path != "" {
|
|
r.With(postMW...).Get(path, servePost)
|
|
}
|
|
}
|
|
|
|
// Post aliases
|
|
allPostAliases, err := allPostAliases()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, path := range allPostAliases {
|
|
if path != "" {
|
|
r.With(cacheMiddleware).Get(path, servePostAlias)
|
|
}
|
|
}
|
|
|
|
// Assets
|
|
for _, path := range allAssetPaths() {
|
|
r.Get(path, serveAsset)
|
|
}
|
|
|
|
paginationPath := "/page/{page:[0-9-]+}"
|
|
feedPath := ".{feed:rss|json|atom}"
|
|
|
|
for blog, blogConfig := range appConfig.Blogs {
|
|
|
|
fullBlogPath := blogConfig.Path
|
|
blogPath := fullBlogPath
|
|
if blogPath == "/" {
|
|
blogPath = ""
|
|
}
|
|
|
|
// Indexes, Feeds
|
|
for _, section := range blogConfig.Sections {
|
|
if section.Name != "" {
|
|
path := blogPath + "/" + section.Name
|
|
handler := serveSection(blog, path, section)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path, handler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+feedPath, handler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, handler)
|
|
}
|
|
}
|
|
|
|
for _, taxonomy := range blogConfig.Taxonomies {
|
|
if taxonomy.Name != "" {
|
|
path := blogPath + "/" + taxonomy.Name
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path, serveTaxonomy(blog, taxonomy))
|
|
values, err := allTaxonomyValues(blog, taxonomy.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, tv := range values {
|
|
vPath := path + "/" + urlize(tv)
|
|
handler := serveTaxonomyValue(blog, vPath, taxonomy, tv)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath, handler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+feedPath, handler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+paginationPath, handler)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Photos
|
|
if blogConfig.Photos.Enabled {
|
|
photoPath := blogPath + blogConfig.Photos.Path
|
|
handler := servePhotos(blog, photoPath)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(photoPath, handler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(photoPath+paginationPath, handler)
|
|
}
|
|
|
|
// Search
|
|
if blogConfig.Search.Enabled {
|
|
searchPath := blogPath + blogConfig.Search.Path
|
|
handler := serveSearch(blog, searchPath)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(searchPath, handler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Post(searchPath, handler)
|
|
searchResultPath := searchPath + "/" + searchPlaceholder
|
|
resultHandler := serveSearchResults(blog, searchResultPath)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(searchResultPath, resultHandler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(searchResultPath+feedPath, resultHandler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(searchResultPath+paginationPath, resultHandler)
|
|
}
|
|
|
|
// Blog
|
|
var mw []func(http.Handler) http.Handler
|
|
if appConfig.ActivityPub.Enabled {
|
|
mw = []func(http.Handler) http.Handler{manipulateAsPath, cacheMiddleware, minifier.Middleware}
|
|
} else {
|
|
mw = []func(http.Handler) http.Handler{cacheMiddleware, minifier.Middleware}
|
|
}
|
|
handler := serveHome(blog, blogPath)
|
|
r.With(mw...).Get(fullBlogPath, handler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+feedPath, handler)
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+paginationPath, handler)
|
|
|
|
// Custom pages
|
|
for _, cp := range blogConfig.CustomPages {
|
|
handler := serveCustomPage(blogConfig, cp)
|
|
if cp.Cache {
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(cp.Path, handler)
|
|
} else {
|
|
r.With(minifier.Middleware).Get(cp.Path, handler)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sitemap
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(sitemapPath, serveSitemap)
|
|
|
|
// Robots.txt - doesn't need cache, because it's too simple
|
|
r.Get("/robots.txt", serveRobotsTXT)
|
|
|
|
// Check redirects, then serve 404
|
|
r.With(checkRegexRedirects, cacheMiddleware, minifier.Middleware).NotFound(serve404)
|
|
|
|
return r, nil
|
|
}
|
|
|
|
func securityHeaders(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Add("Strict-Transport-Security", "max-age=31536000;")
|
|
w.Header().Add("Referrer-Policy", "no-referrer")
|
|
w.Header().Add("X-Content-Type-Options", "nosniff")
|
|
w.Header().Add("X-Frame-Options", "SAMEORIGIN")
|
|
w.Header().Add("X-Xss-Protection", "1; mode=block")
|
|
// TODO: Add CSP
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
type dynamicHandler struct {
|
|
realHandler atomic.Value
|
|
}
|
|
|
|
func (d *dynamicHandler) swapHandler(h http.Handler) {
|
|
d.realHandler.Store(h)
|
|
}
|
|
|
|
func (d *dynamicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
// Fix to use Path routing instead of RawPath routing in Chi
|
|
r.URL.RawPath = ""
|
|
// Serve request
|
|
d.realHandler.Load().(http.Handler).ServeHTTP(w, r)
|
|
}
|