2020-07-28 19:17:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2020-07-29 15:39:46 +00:00
|
|
|
"strings"
|
2020-07-29 15:17:48 +00:00
|
|
|
"sync"
|
2020-09-25 17:23:01 +00:00
|
|
|
|
|
|
|
"github.com/caddyserver/certmagic"
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
2020-07-28 19:17:07 +00:00
|
|
|
)
|
|
|
|
|
2020-10-06 17:07:48 +00:00
|
|
|
const contentType = "Content-Type"
|
|
|
|
const contentTypeHTMLUTF8 = "text/html; charset=utf-8"
|
|
|
|
const contentTypeJSONUTF8 = "application/json; charset=utf-8"
|
|
|
|
const contentTypeJSON = "application/json"
|
|
|
|
const contentTypeWWWForm = "application/x-www-form-urlencoded"
|
|
|
|
const contentTypeMultipartForm = "multipart/form-data"
|
2020-07-31 14:23:29 +00:00
|
|
|
|
2020-07-31 19:44:16 +00:00
|
|
|
var d *dynamicHandler
|
|
|
|
|
2020-08-01 15:49:46 +00:00
|
|
|
func startServer() (err error) {
|
2020-07-31 19:44:16 +00:00
|
|
|
d = newDynamicHandler()
|
2020-07-29 15:17:48 +00:00
|
|
|
h, err := buildHandler()
|
|
|
|
if err != nil {
|
2020-08-01 15:49:46 +00:00
|
|
|
return
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
2020-07-29 15:17:48 +00:00
|
|
|
d.swapHandler(h)
|
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 {
|
2020-08-01 15:49:46 +00:00
|
|
|
initPublicHTTPS()
|
2020-08-04 17:42:09 +00:00
|
|
|
err = certmagic.HTTPS([]string{appConfig.Server.Domain}, d)
|
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-08-01 15:49:46 +00:00
|
|
|
func initPublicHTTPS() {
|
|
|
|
certmagic.Default.Storage = &certmagic.FileStorage{Path: "certmagic"}
|
|
|
|
certmagic.DefaultACME.Agreed = true
|
2020-08-04 17:42:09 +00:00
|
|
|
certmagic.DefaultACME.Email = appConfig.Server.LetsEncryptMail
|
2020-08-01 15:49:46 +00:00
|
|
|
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA
|
2020-07-29 15:17:48 +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
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:17:48 +00:00
|
|
|
func buildHandler() (http.Handler, error) {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
2020-08-04 17:42:09 +00:00
|
|
|
if appConfig.Server.Logging {
|
2020-08-05 17:14:10 +00:00
|
|
|
r.Use(middleware.RealIP, middleware.Logger)
|
2020-07-28 19:52:56 +00:00
|
|
|
}
|
2020-08-05 17:14:10 +00:00
|
|
|
r.Use(middleware.Recoverer, middleware.StripSlashes, middleware.GetHead)
|
2020-07-29 15:17:48 +00:00
|
|
|
|
2020-09-19 10:27:07 +00:00
|
|
|
// API
|
2020-07-31 19:44:16 +00:00
|
|
|
r.Route("/api", func(apiRouter chi.Router) {
|
2020-08-01 13:16:21 +00:00
|
|
|
apiRouter.Use(middleware.BasicAuth("API", map[string]string{
|
2020-08-04 17:42:09 +00:00
|
|
|
appConfig.User.Nick: appConfig.User.Password,
|
2020-08-01 13:16:21 +00:00
|
|
|
}))
|
2020-07-31 19:44:16 +00:00
|
|
|
apiRouter.Post("/post", apiPostCreate)
|
2020-09-18 10:34:52 +00:00
|
|
|
apiRouter.Get("/post", apiPostRead)
|
2020-08-01 13:16:21 +00:00
|
|
|
apiRouter.Delete("/post", apiPostDelete)
|
2020-09-18 10:34:52 +00:00
|
|
|
apiRouter.Post("/hugo", apiPostCreateHugo)
|
2020-07-31 19:44:16 +00:00
|
|
|
})
|
|
|
|
|
2020-10-06 17:07:48 +00:00
|
|
|
// Micropub
|
|
|
|
if appConfig.Micropub.Enabled {
|
|
|
|
r.Get(appConfig.Micropub.Path, serveMicropubQuery)
|
|
|
|
r.With(checkIndieAuth).Post(appConfig.Micropub.Path, serveMicropubPost)
|
|
|
|
r.With(checkIndieAuth).Post(appConfig.Micropub.Path+micropubMediaSubPath, serveMicropubMedia)
|
|
|
|
}
|
|
|
|
|
2020-09-19 10:27:07 +00:00
|
|
|
// Posts
|
2020-07-29 15:17:48 +00:00
|
|
|
allPostPaths, err := allPostPaths()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-30 19:18:13 +00:00
|
|
|
}
|
|
|
|
for _, path := range allPostPaths {
|
|
|
|
if path != "" {
|
2020-09-25 17:23:01 +00:00
|
|
|
r.With(manipulateAsPath, cacheMiddleware, minifier.Middleware).Get(path, servePost)
|
2020-07-29 15:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 10:27:07 +00:00
|
|
|
// Redirects
|
2020-07-29 15:39:46 +00:00
|
|
|
allRedirectPaths, err := allRedirectPaths()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-30 19:18:13 +00:00
|
|
|
}
|
|
|
|
for _, path := range allRedirectPaths {
|
|
|
|
if path != "" {
|
2020-07-31 19:02:47 +00:00
|
|
|
r.With(minifier.Middleware).Get(path, serveRedirect)
|
2020-07-29 15:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 10:27:07 +00:00
|
|
|
// Assets
|
|
|
|
for _, path := range allAssetPaths() {
|
|
|
|
if path != "" {
|
|
|
|
r.Get(path, serveAsset(path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 19:12:43 +00:00
|
|
|
paginationPath := "/page/{page}"
|
2020-09-14 14:07:37 +00:00
|
|
|
rssPath := ".rss"
|
|
|
|
jsonPath := ".json"
|
|
|
|
atomPath := ".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
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path, serveSection(blog, path, section, noFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+rssPath, serveSection(blog, path, section, rssFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+jsonPath, serveSection(blog, path, section, jsonFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+atomPath, serveSection(blog, path, section, atomFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveSection(blog, path, section, noFeed))
|
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 {
|
2020-10-12 16:08:50 +00:00
|
|
|
vPath := path + "/" + urlize(tv)
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, noFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+rssPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, rssFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+jsonPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, jsonFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+atomPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, atomFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+paginationPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, noFeed))
|
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 {
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+blogConfig.Photos.Path, servePhotos(blog))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+blogConfig.Photos.Path+paginationPath, servePhotos(blog))
|
|
|
|
}
|
2020-09-21 16:03:05 +00:00
|
|
|
|
2020-10-06 17:07:48 +00:00
|
|
|
// Blog
|
2020-10-12 17:12:24 +00:00
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath, serveHome(blog, blogPath, noFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+rssPath, serveHome(blog, blogPath, rssFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+jsonPath, serveHome(blog, blogPath, jsonFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+atomPath, serveHome(blog, blogPath, atomFeed))
|
|
|
|
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+paginationPath, serveHome(blog, blogPath, noFeed))
|
2020-08-05 17:14:10 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 15:08:34 +00:00
|
|
|
// Sitemap
|
|
|
|
r.With(cacheMiddleware).Get("/sitemap.xml", serveSitemap())
|
|
|
|
|
2020-07-31 19:02:47 +00:00
|
|
|
r.With(minifier.Middleware).NotFound(serve404)
|
|
|
|
|
2020-07-29 15:17:48 +00:00
|
|
|
return r, nil
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 15:17:48 +00:00
|
|
|
type dynamicHandler struct {
|
|
|
|
realHandler http.Handler
|
|
|
|
changeMutex *sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDynamicHandler() *dynamicHandler {
|
|
|
|
return &dynamicHandler{
|
|
|
|
changeMutex: &sync.Mutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dynamicHandler) swapHandler(h http.Handler) {
|
|
|
|
d.changeMutex.Lock()
|
|
|
|
d.realHandler = h
|
|
|
|
d.changeMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dynamicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d.realHandler.ServeHTTP(w, r)
|
|
|
|
}
|
2020-07-29 15:39:46 +00:00
|
|
|
|
2020-07-30 19:18:13 +00:00
|
|
|
func slashTrimmedPath(r *http.Request) string {
|
2020-07-31 19:02:47 +00:00
|
|
|
return trimSlash(r.URL.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func trimSlash(s string) string {
|
|
|
|
if len(s) > 1 {
|
|
|
|
s = strings.TrimSuffix(s, "/")
|
2020-07-29 15:55:10 +00:00
|
|
|
}
|
2020-07-31 19:02:47 +00:00
|
|
|
return s
|
2020-07-29 20:45:26 +00:00
|
|
|
}
|