GoBlog/http.go

147 lines
3.5 KiB
Go
Raw Normal View History

2020-07-28 19:17:07 +00:00
package main
import (
2020-08-01 15:49:46 +00:00
"github.com/caddyserver/certmagic"
2020-07-29 14:41:36 +00:00
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
2020-07-28 19:17:07 +00:00
"net/http"
"strconv"
"strings"
"sync"
2020-07-28 19:17:07 +00:00
)
2020-08-05 17:14:10 +00:00
const contentTypeHTML = "text/html; charset=utf-8"
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()
h, err := buildHandler()
if err != nil {
2020-08-01 15:49:46 +00:00
return
2020-07-28 19:17:07 +00:00
}
d.swapHandler(h)
2020-08-04 17:42:09 +00:00
localAddress := ":" + strconv.Itoa(appConfig.Server.Port)
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)
} 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-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-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-31 19:44:16 +00:00
r.Route("/api", func(apiRouter chi.Router) {
apiRouter.Use(middleware.BasicAuth("API", map[string]string{
2020-08-04 17:42:09 +00:00
appConfig.User.Nick: appConfig.User.Password,
}))
2020-07-31 19:44:16 +00:00
apiRouter.Post("/post", apiPostCreate)
apiRouter.Delete("/post", apiPostDelete)
2020-07-31 19:44:16 +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-07-31 14:23:29 +00:00
r.With(cacheMiddleware, minifier.Middleware).Get(path, servePost)
}
}
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(minifier.Middleware).Get(path, serveRedirect)
}
}
2020-08-25 18:55:32 +00:00
for _, section := range appConfig.Blog.Sections {
if section != "" {
r.With(cacheMiddleware, minifier.Middleware).Get("/"+section, serveSection("/"+section, section))
r.With(cacheMiddleware, minifier.Middleware).Get("/"+section+"/page/{page}", serveSection("/"+section, section))
}
}
2020-08-05 17:14:10 +00:00
routePatterns := routesToStringSlice(r.Routes())
if !routePatterns.has("/") {
2020-08-25 18:55:32 +00:00
r.With(cacheMiddleware, minifier.Middleware).Get("/", serveHome("/"))
r.With(cacheMiddleware, minifier.Middleware).Get("/page/{page}", serveHome("/"))
2020-08-05 17:14:10 +00:00
} else if !routePatterns.has("/blog") {
2020-08-25 18:55:32 +00:00
r.With(cacheMiddleware, minifier.Middleware).Get("/blog", serveHome("/blog"))
r.With(cacheMiddleware, minifier.Middleware).Get("/blog/page/{page}", serveHome("/blog"))
2020-08-05 17:14:10 +00:00
}
r.With(minifier.Middleware).NotFound(serve404)
return r, nil
2020-07-28 19:17:07 +00:00
}
2020-08-05 17:14:10 +00:00
func routesToStringSlice(routes []chi.Route) (ss stringSlice) {
for _, r := range routes {
ss = append(ss, r.Pattern)
}
return
}
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-30 19:18:13 +00:00
func slashTrimmedPath(r *http.Request) string {
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
}
return s
2020-07-29 20:45:26 +00:00
}