mirror of https://github.com/jlelse/GoBlog
Fix activitystream path manipulation
This commit is contained in:
parent
b96424f886
commit
d1e0916342
|
@ -2,12 +2,23 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/araddon/dateparse"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/araddon/dateparse"
|
||||
)
|
||||
|
||||
func manipulateAsPath(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if lowerAccept := strings.ToLower(r.Header.Get("Accept")); (strings.Contains(lowerAccept, "application/activity+json") || strings.Contains(lowerAccept, "application/ld+json")) && !strings.Contains(lowerAccept, "text/html") {
|
||||
// Is ActivityStream, add ".as" to differentiate cache and also trigger as function
|
||||
r.URL.Path += ".as"
|
||||
}
|
||||
next.ServeHTTP(rw, r)
|
||||
})
|
||||
}
|
||||
|
||||
type asPost struct {
|
||||
Context []string `json:"@context"`
|
||||
To []string `json:"to"`
|
||||
|
@ -19,14 +30,14 @@ type asPost struct {
|
|||
Attachment []*asAttachment `json:"attachment,omitempty"`
|
||||
Published string `json:"published"`
|
||||
Updated string `json:"updated,omitempty"`
|
||||
Id string `json:"id"`
|
||||
Url string `json:"url"`
|
||||
ID string `json:"id"`
|
||||
URL string `json:"url"`
|
||||
AttributedTo string `json:"attributedTo"`
|
||||
}
|
||||
|
||||
type asAttachment struct {
|
||||
Type string `json:"type"`
|
||||
Url string `json:"url"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
func servePostActivityStreams(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -46,8 +57,8 @@ func servePostActivityStreams(w http.ResponseWriter, r *http.Request) {
|
|||
Context: []string{"https://www.w3.org/ns/activitystreams"},
|
||||
To: []string{"https://www.w3.org/ns/activitystreams#Public"},
|
||||
MediaType: "text/html",
|
||||
Id: appConfig.Server.PublicAddress + post.Path,
|
||||
Url: appConfig.Server.PublicAddress + post.Path,
|
||||
ID: appConfig.Server.PublicAddress + post.Path,
|
||||
URL: appConfig.Server.PublicAddress + post.Path,
|
||||
AttributedTo: appConfig.Server.PublicAddress,
|
||||
}
|
||||
// Name and Type
|
||||
|
@ -69,7 +80,7 @@ func servePostActivityStreams(w http.ResponseWriter, r *http.Request) {
|
|||
for _, image := range images {
|
||||
as.Attachment = append(as.Attachment, &asAttachment{
|
||||
Type: "Image",
|
||||
Url: image,
|
||||
URL: image,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
16
cache.go
16
cache.go
|
@ -16,25 +16,13 @@ func initCache() {
|
|||
cacheMutexes = map[string]*sync.Mutex{}
|
||||
}
|
||||
|
||||
func cacheWithCheckMiddleware(cache func(r *http.Request) bool) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
if cache(request) {
|
||||
cacheMiddleware(next).ServeHTTP(writer, request)
|
||||
} else {
|
||||
next.ServeHTTP(writer, request)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func cacheMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestUrl, _ := url.ParseRequestURI(r.RequestURI)
|
||||
requestURL, _ := url.ParseRequestURI(r.RequestURI)
|
||||
path := slashTrimmedPath(r)
|
||||
if appConfig.Cache.Enable &&
|
||||
// check bypass query
|
||||
!(requestUrl != nil && requestUrl.Query().Get("cache") == "0") {
|
||||
!(requestURL != nil && requestURL.Query().Get("cache") == "0") {
|
||||
// Check cache mutex
|
||||
if cacheMutexes[path] == nil {
|
||||
cacheMutexes[path] = &sync.Mutex{}
|
||||
|
|
4
go.mod
4
go.mod
|
@ -40,9 +40,9 @@ require (
|
|||
go.uber.org/zap v1.16.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect
|
||||
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
|
||||
golang.org/x/net v0.0.0-20200923182212-328152dc79b1 // indirect
|
||||
golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321 // indirect
|
||||
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d // indirect
|
||||
golang.org/x/tools v0.0.0-20200923182640-463111b69878 // indirect
|
||||
golang.org/x/tools v0.0.0-20200924224222-8d73f17870ce // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
|
||||
gopkg.in/ini.v1 v1.61.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||
|
|
8
go.sum
8
go.sum
|
@ -362,8 +362,8 @@ golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7
|
|||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200923182212-328152dc79b1 h1:Iu68XRPd67wN4aRGGWwwq6bZo/25jR6uu52l/j2KkUE=
|
||||
golang.org/x/net v0.0.0-20200923182212-328152dc79b1/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321 h1:lleNcKRbcaC8MqgLwghIkzZ2JBQAb7QQ9MiwRt1BisA=
|
||||
golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
|
@ -426,8 +426,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
|
|||
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||
golang.org/x/tools v0.0.0-20200923182640-463111b69878 h1:VUw1+Jf6KJPf82mbTQMia6HCnNMv2BbAipkEZ4KTcqQ=
|
||||
golang.org/x/tools v0.0.0-20200923182640-463111b69878/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
|
||||
golang.org/x/tools v0.0.0-20200924224222-8d73f17870ce h1:XRr763sMfaUSNR4EsxbddvVEqYFa9picrx6ks9pJkKw=
|
||||
golang.org/x/tools v0.0.0-20200924224222-8d73f17870ce/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
|
|
16
http.go
16
http.go
|
@ -1,13 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/caddyserver/certmagic"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
)
|
||||
|
||||
const contentTypeHTML = "text/html; charset=utf-8"
|
||||
|
@ -76,14 +77,7 @@ func buildHandler() (http.Handler, error) {
|
|||
}
|
||||
for _, path := range allPostPaths {
|
||||
if path != "" {
|
||||
r.With(cacheWithCheckMiddleware(func(r *http.Request) bool {
|
||||
if lowerAccept := strings.ToLower(r.Header.Get("Accept")); (strings.Contains(lowerAccept, "application/activity+json") || strings.Contains(lowerAccept, "application/ld+json")) &&
|
||||
!strings.Contains(lowerAccept, "text/html") {
|
||||
// Is ActivityStream, add ".as" to differentiate cache and also trigger as function
|
||||
r.URL.Path += ".as"
|
||||
}
|
||||
return true
|
||||
}), minifier.Middleware).Get(path, servePost)
|
||||
r.With(manipulateAsPath, cacheMiddleware, minifier.Middleware).Get(path, servePost)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue