GoBlog/http.go

288 lines
8.0 KiB
Go
Raw Normal View History

2020-07-28 19:17:07 +00:00
package main
import (
2020-10-19 19:09:51 +00:00
"compress/flate"
2020-07-28 19:17:07 +00:00
"net/http"
"os"
2020-07-28 19:17:07 +00:00
"strconv"
"sync/atomic"
2020-09-25 17:23:01 +00:00
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
2020-11-03 17:58:32 +00:00
"github.com/gorilla/handlers"
"golang.org/x/crypto/acme/autocert"
2020-07-28 19:17:07 +00:00
)
const (
contentType = "Content-Type"
2020-07-31 14:23:29 +00:00
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
)
var (
2020-11-03 17:58:32 +00:00
d *dynamicHandler
logMiddleware func(next http.Handler) http.Handler
authMiddleware func(next http.Handler) http.Handler
)
2020-07-31 19:44:16 +00:00
2020-08-01 15:49:46 +00:00
func startServer() (err error) {
2020-11-03 17:58:32 +00:00
// 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 {
2020-08-01 15:49:46 +00:00
return
2020-07-28 19:17:07 +00:00
}
2020-08-04 17:42:09 +00:00
localAddress := ":" + strconv.Itoa(appConfig.Server.Port)
2020-10-06 17:07:48 +00:00
if appConfig.Server.PublicHTTPS {
cache, err := newAutocertCache()
if err != nil {
return err
}
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(appConfig.Server.Domain),
Cache: cache,
Email: appConfig.Server.LetsEncryptMail,
}
tlsConfig := certManager.TLSConfig()
server := http.Server{
Addr: ":https",
2020-10-16 13:35:38 +00:00
Handler: securityHeaders(d),
TLSConfig: tlsConfig,
}
go http.ListenAndServe(":http", certManager.HTTPHandler(nil))
err = server.ListenAndServeTLS("", "")
2020-10-06 17:07:48 +00:00
} else if appConfig.Server.LocalHTTPS {
2020-08-01 15:49:46 +00:00
err = http.ListenAndServeTLS(localAddress, "https/server.crt", "https/server.key", d)
} else {
err = http.ListenAndServe(localAddress, d)
2020-07-29 14:41:36 +00:00
}
2020-08-01 15:49:46 +00:00
return
}
2020-07-29 14:41:36 +00:00
2020-07-31 19:44:16 +00:00
func reloadRouter() error {
h, err := buildHandler()
if err != nil {
return err
}
d.swapHandler(h)
return nil
}
func buildHandler() (http.Handler, error) {
r := chi.NewRouter()
2020-08-04 17:42:09 +00:00
if appConfig.Server.Logging {
2020-11-03 17:58:32 +00:00
r.Use(logMiddleware)
2020-07-28 19:52:56 +00:00
}
2020-11-03 17:58:32 +00:00
r.Use(middleware.Recoverer)
2020-10-19 19:09:51 +00:00
r.Use(middleware.Compress(flate.DefaultCompression))
r.Use(middleware.StripSlashes)
r.Use(middleware.GetHead)
if !appConfig.Cache.Enable {
r.Use(middleware.NoCache)
}
2020-10-12 18:23:21 +00:00
// Profiler
if appConfig.Server.Debug {
r.Mount("/debug", middleware.Profiler())
}
// API
2020-07-31 19:44:16 +00:00
r.Route("/api", func(apiRouter chi.Router) {
apiRouter.Use(middleware.NoCache, authMiddleware)
apiRouter.Post("/hugo", apiPostCreateHugo)
2020-07-31 19:44:16 +00:00
})
2020-10-06 17:07:48 +00:00
// Micropub
r.Route(micropubPath, func(mpRouter chi.Router) {
mpRouter.Use(middleware.NoCache, checkIndieAuth)
mpRouter.Get("/", serveMicropubQuery)
mpRouter.Post("/", serveMicropubPost)
2020-10-14 19:20:17 +00:00
if appConfig.Micropub.MediaStorage != nil {
mpRouter.Post(micropubMediaSubPath, serveMicropubMedia)
}
})
2020-10-06 17:07:48 +00:00
2020-10-13 19:35:39 +00:00
// IndieAuth
r.Route("/indieauth", func(indieauthRouter chi.Router) {
indieauthRouter.Use(middleware.NoCache)
2020-10-13 19:35:39 +00:00
indieauthRouter.With(authMiddleware).Get("/", indieAuthAuth)
indieauthRouter.With(authMiddleware).Post("/accept", indieAuthAccept)
indieauthRouter.Post("/", indieAuthAuth)
indieauthRouter.Get("/token", indieAuthToken)
indieauthRouter.Post("/token", indieAuthToken)
})
// ActivityPub and stuff
if appConfig.ActivityPub.Enabled {
r.Post("/activitypub/inbox/{blog}", apHandleInbox)
r.Get("/.well-known/webfinger", apHandleWebfinger)
r.Get("/.well-known/host-meta", handleWellKnownHostMeta)
}
// Posts
allPostPaths, err := allPostPaths()
if err != nil {
return nil, err
2020-07-30 19:18:13 +00:00
}
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}
}
2020-07-30 19:18:13 +00:00
for _, path := range allPostPaths {
if path != "" {
r.With(postMW...).Get(path, servePost)
}
}
// Redirects
allRedirectPaths, err := allRedirectPaths()
if err != nil {
return nil, err
2020-07-30 19:18:13 +00:00
}
for _, path := range allRedirectPaths {
if path != "" {
r.With(cacheMiddleware, minifier.Middleware).Get(path, serveRedirect)
}
}
// Assets
for _, path := range allAssetPaths() {
2020-10-15 17:50:34 +00:00
r.Get(path, serveAsset)
}
2020-10-15 18:54:43 +00:00
paginationPath := "/page/{page:[0-9-]+}"
2020-10-15 17:50:34 +00:00
feedPath := ".{feed:rss|json|atom}"
2020-08-31 19:12:43 +00:00
2020-10-06 17:07:48 +00:00
for blog, blogConfig := range appConfig.Blogs {
2020-10-12 17:12:24 +00:00
fullBlogPath := blogConfig.Path
blogPath := fullBlogPath
2020-10-06 17:07:48 +00:00
if blogPath == "/" {
blogPath = ""
2020-08-31 19:12:43 +00:00
}
2020-10-06 17:07:48 +00:00
// Indexes, Feeds
for _, section := range blogConfig.Sections {
if section.Name != "" {
path := blogPath + "/" + section.Name
2020-11-01 17:37:21 +00:00
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)
2020-08-31 19:12:43 +00:00
}
2020-10-06 17:07:48 +00:00
}
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)
2020-11-01 17:37:21 +00:00
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)
2020-10-06 17:07:48 +00:00
}
2020-08-31 19:12:43 +00:00
}
2020-08-25 18:55:32 +00:00
}
2020-10-06 17:07:48 +00:00
// Photos
if blogConfig.Photos.Enabled {
2020-11-01 17:37:21 +00:00
handler := servePhotos(blog)
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+blogConfig.Photos.Path, handler)
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+blogConfig.Photos.Path+paginationPath, handler)
2020-10-06 17:07:48 +00:00
}
2020-09-21 16:03:05 +00:00
2020-10-06 17:07:48 +00:00
// 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}
}
2020-11-01 17:37:21 +00:00
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)
2020-10-13 11:40:16 +00:00
// Custom pages
for _, cp := range blogConfig.CustomPages {
2020-11-01 17:37:21 +00:00
handler := serveCustomPage(blogConfig, cp)
2020-10-13 11:40:16 +00:00
if cp.Cache {
2020-11-01 17:37:21 +00:00
r.With(cacheMiddleware, minifier.Middleware).Get(cp.Path, handler)
2020-10-13 11:40:16 +00:00
} else {
2020-11-01 17:37:21 +00:00
r.With(minifier.Middleware).Get(cp.Path, handler)
2020-10-13 11:40:16 +00:00
}
}
2020-08-05 17:14:10 +00:00
}
2020-09-22 15:08:34 +00:00
// Sitemap
2020-10-17 15:36:18 +00:00
r.With(cacheMiddleware, minifier.Middleware).Get(sitemapPath, serveSitemap)
2020-09-22 15:08:34 +00:00
2020-10-15 18:54:43 +00:00
// Check redirects, then serve 404
r.With(checkRegexRedirects, cacheMiddleware, minifier.Middleware).NotFound(serve404)
return r, nil
2020-07-28 19:17:07 +00:00
}
2020-10-16 13:35:38 +00:00
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) {
d.realHandler.Load().(http.Handler).ServeHTTP(w, r)
}