More refactoring
parent
cf4dd37484
commit
a7b987cca2
42
config.go
42
config.go
|
@ -9,10 +9,9 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
BlogUrl string
|
BlogUrl string
|
||||||
GiteaEndpoint string
|
|
||||||
GiteaToken string
|
|
||||||
IgnoredWebmentionUrls []string
|
IgnoredWebmentionUrls []string
|
||||||
SyndicationTargets []SyndicationTarget
|
SyndicationTargets []SyndicationTarget
|
||||||
|
SelectedStorage Storage
|
||||||
SelectedCdn Cdn
|
SelectedCdn Cdn
|
||||||
SelectedSocials Socials
|
SelectedSocials Socials
|
||||||
)
|
)
|
||||||
|
@ -29,17 +28,6 @@ func init() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
BlogUrl = blogUrl
|
BlogUrl = blogUrl
|
||||||
// Gitea (required)
|
|
||||||
giteaEndpoint, err := giteaEndpoint()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
GiteaEndpoint = giteaEndpoint
|
|
||||||
giteaToken, err := giteaToken()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
GiteaToken = giteaToken
|
|
||||||
// Ignored Webmention URLs (optional)
|
// Ignored Webmention URLs (optional)
|
||||||
ignoredWebmentionUrls, err := ignoredWebmentionUrls()
|
ignoredWebmentionUrls, err := ignoredWebmentionUrls()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -52,7 +40,23 @@ func init() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
SyndicationTargets = syndicationTargets
|
SyndicationTargets = syndicationTargets
|
||||||
// Find selected CDN (currently only BunnyCDN)
|
// Find selected storage
|
||||||
|
SelectedStorage = func() Storage {
|
||||||
|
// Gitea
|
||||||
|
giteaEndpoint, err1 := giteaEndpoint()
|
||||||
|
giteaToken, err2 := giteaToken()
|
||||||
|
if err1 == nil && err2 == nil {
|
||||||
|
return &Gitea{
|
||||||
|
endpoint: giteaEndpoint,
|
||||||
|
token: giteaToken,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}()
|
||||||
|
if SelectedStorage == nil {
|
||||||
|
log.Fatal("No storage configured")
|
||||||
|
}
|
||||||
|
// Find selected CDN (optional)
|
||||||
SelectedCdn = func() Cdn {
|
SelectedCdn = func() Cdn {
|
||||||
// BunnyCDN (optional)
|
// BunnyCDN (optional)
|
||||||
bunnyCdnKey, err := bunnyCdnKey()
|
bunnyCdnKey, err := bunnyCdnKey()
|
||||||
|
@ -61,9 +65,12 @@ func init() {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}()
|
}()
|
||||||
// Find configured social networks
|
if SelectedCdn == nil {
|
||||||
|
log.Println("No CDN configured")
|
||||||
|
}
|
||||||
|
// Find configured social networks (optional)
|
||||||
SelectedSocials = func() Socials {
|
SelectedSocials = func() Socials {
|
||||||
var socials []Social
|
var socials []Social = nil
|
||||||
// Microblog.pub
|
// Microblog.pub
|
||||||
microblogUrl, err1 := microblogUrl()
|
microblogUrl, err1 := microblogUrl()
|
||||||
microblogToken, err2 := microblogToken()
|
microblogToken, err2 := microblogToken()
|
||||||
|
@ -75,6 +82,9 @@ func init() {
|
||||||
}
|
}
|
||||||
return socials
|
return socials
|
||||||
}()
|
}()
|
||||||
|
if SelectedSocials == nil {
|
||||||
|
log.Println("No social networks configured")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func giteaEndpoint() (string, error) {
|
func giteaEndpoint() (string, error) {
|
||||||
|
|
6
entry.go
6
entry.go
|
@ -232,7 +232,7 @@ func WriteEntry(entry *Entry) (location string, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = CreateFile(entry.filename, file, entry.title)
|
err = SelectedStorage.CreateFile(entry.filename, file, entry.title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -257,8 +257,8 @@ func ReadEntry(url string) (entry *Entry, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fileContent, err := ReadFileContent(filePath)
|
fileContent, _, exists, err := SelectedStorage.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil || !exists {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
entry, err = ReadHugoPost(fileContent)
|
entry, err = ReadHugoPost(fileContent)
|
||||||
|
|
12
micropub.go
12
micropub.go
|
@ -80,14 +80,20 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
w.Header().Add("Location", location)
|
w.Header().Add("Location", location)
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
// Purge BunnyCDN in 10 seconds
|
// Purge CDN in 10 seconds, send webmentions, post to social media
|
||||||
go func() {
|
go func() {
|
||||||
|
if SelectedCdn != nil {
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
SelectedCdn.Purge(location)
|
SelectedCdn.Purge(location)
|
||||||
time.Sleep(3 * time.Second)
|
}
|
||||||
|
time.Sleep(10 * time.Second)
|
||||||
// Send webmentions
|
// Send webmentions
|
||||||
go SendWebmentions(location)
|
go SendWebmentions(location)
|
||||||
go SelectedSocials.Post(location, entry.title)
|
go func() {
|
||||||
|
if SelectedSocials != nil {
|
||||||
|
SelectedSocials.Post(location, entry.title)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}()
|
}()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,24 +11,36 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GiteaCommitRequest struct {
|
type Storage interface {
|
||||||
|
CreateFile(path string, file string, message string) (err error)
|
||||||
|
UpdateFile(path string, file string, message string) (err error)
|
||||||
|
ReadFile(path string) (content string, sha string, exists bool, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gitea
|
||||||
|
type Gitea struct {
|
||||||
|
endpoint string
|
||||||
|
token string
|
||||||
|
}
|
||||||
|
|
||||||
|
type giteaCommitRequest struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
SHA string `json:"sha,omitempty"`
|
SHA string `json:"sha,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GiteaReadRequest struct {
|
type giteaReadResponse struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
SHA string `json:"sha"`
|
SHA string `json:"sha"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GiteaErrorResponse struct {
|
type giteaErrorResponse struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFile(path string, file string, message string) error {
|
func (gitea *Gitea) CreateFile(path string, file string, message string) (err error) {
|
||||||
request := &GiteaCommitRequest{
|
request := &giteaCommitRequest{
|
||||||
Content: base64.StdEncoding.EncodeToString([]byte(file)),
|
Content: base64.StdEncoding.EncodeToString([]byte(file)),
|
||||||
Message: message,
|
Message: message,
|
||||||
}
|
}
|
||||||
|
@ -36,32 +48,32 @@ func CreateFile(path string, file string, message string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("failed to marshal json before committing")
|
return errors.New("failed to marshal json before committing")
|
||||||
}
|
}
|
||||||
resp, err := http.Post(GiteaEndpoint+url.QueryEscape(path)+"?access_token="+GiteaToken, "application/json", bytes.NewBuffer(bytesRepresentation))
|
resp, err := http.Post(gitea.endpoint+url.QueryEscape(path)+"?access_token="+gitea.token, "application/json", bytes.NewBuffer(bytesRepresentation))
|
||||||
if err != nil || resp.StatusCode != 201 {
|
if err != nil || resp.StatusCode != 201 {
|
||||||
return errors.New("failed to create file in repo")
|
return errors.New("failed to create file in repo")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateFile(path string, file string, message string) error {
|
func (gitea *Gitea) UpdateFile(path string, file string, message string) (err error) {
|
||||||
existingFile, exists, err := ReadFile(path)
|
_, sha, exists, err := gitea.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
// File doesn't exist, create it
|
// File doesn't exist, create it
|
||||||
return CreateFile(path, file, message)
|
return gitea.CreateFile(path, file, message)
|
||||||
}
|
}
|
||||||
request := &GiteaCommitRequest{
|
request := &giteaCommitRequest{
|
||||||
Content: base64.StdEncoding.EncodeToString([]byte(file)),
|
Content: base64.StdEncoding.EncodeToString([]byte(file)),
|
||||||
Message: message,
|
Message: message,
|
||||||
SHA: existingFile.SHA,
|
SHA: sha,
|
||||||
}
|
}
|
||||||
bytesRepresentation, err := json.Marshal(request)
|
bytesRepresentation, err := json.Marshal(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("failed to marshal json before committing")
|
return errors.New("failed to marshal json before committing")
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest(http.MethodPut, GiteaEndpoint+url.QueryEscape(path)+"?access_token="+GiteaToken, bytes.NewBuffer(bytesRepresentation))
|
req, err := http.NewRequest(http.MethodPut, gitea.endpoint+url.QueryEscape(path)+"?access_token="+gitea.token, bytes.NewBuffer(bytesRepresentation))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("error making update request")
|
return errors.New("error making update request")
|
||||||
}
|
}
|
||||||
|
@ -74,9 +86,9 @@ func UpdateFile(path string, file string, message string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadFile(path string) (response *GiteaReadRequest, exists bool, err error) {
|
func (gitea *Gitea) ReadFile(path string) (content string, sha string, exists bool, err error) {
|
||||||
exists = false
|
exists = false
|
||||||
resp, err := http.Get(GiteaEndpoint + url.QueryEscape(path) + "?access_token=" + GiteaToken)
|
resp, err := http.Get(gitea.endpoint + url.QueryEscape(path) + "?access_token=" + gitea.token)
|
||||||
if err != nil || resp.StatusCode != 200 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
@ -85,7 +97,7 @@ func ReadFile(path string) (response *GiteaReadRequest, exists bool, err error)
|
||||||
err = errors.New("failed reading Gitea error response")
|
err = errors.New("failed reading Gitea error response")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
errorResponse := &GiteaErrorResponse{}
|
errorResponse := &giteaErrorResponse{}
|
||||||
marshalErr := json.Unmarshal(body, &errorResponse)
|
marshalErr := json.Unmarshal(body, &errorResponse)
|
||||||
if marshalErr != nil {
|
if marshalErr != nil {
|
||||||
err = errors.New("failed parsing Gitea error response")
|
err = errors.New("failed parsing Gitea error response")
|
||||||
|
@ -106,29 +118,17 @@ func ReadFile(path string) (response *GiteaReadRequest, exists bool, err error)
|
||||||
err = errors.New("failed reading file in repo")
|
err = errors.New("failed reading file in repo")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
response = &GiteaReadRequest{}
|
readResponse := &giteaReadResponse{}
|
||||||
err = json.Unmarshal(body, &response)
|
err = json.Unmarshal(body, &readResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.New("failed parsing Gitea response")
|
err = errors.New("failed parsing Gitea response")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
decodedContentBytes, err := base64.StdEncoding.DecodeString(readResponse.Content)
|
||||||
}
|
|
||||||
|
|
||||||
func ReadFileContent(path string) (fileContent string, err error) {
|
|
||||||
giteaResponseBody, exists, err := ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !exists {
|
|
||||||
// File doesn't exist, nothing to read
|
|
||||||
err = errors.New("file does not exist")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
decodedBytes, err := base64.StdEncoding.DecodeString(giteaResponseBody.Content)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.New("failed decoding file content")
|
err = errors.New("failed decoding file content")
|
||||||
}
|
}
|
||||||
fileContent = string(decodedBytes)
|
content = string(decodedContentBytes)
|
||||||
|
sha = readResponse.SHA
|
||||||
return
|
return
|
||||||
}
|
}
|
|
@ -162,7 +162,7 @@ func saveWebmention(mention *Mention) (err error) {
|
||||||
return errors.New("failed to marshal json before committing")
|
return errors.New("failed to marshal json before committing")
|
||||||
}
|
}
|
||||||
filePath := fmt.Sprintf("data/mentions/%x/%x.json", md5.Sum([]byte(strings.ReplaceAll(mention.Target, "/", ""))), md5.Sum([]byte(mention.Source)))
|
filePath := fmt.Sprintf("data/mentions/%x/%x.json", md5.Sum([]byte(strings.ReplaceAll(mention.Target, "/", ""))), md5.Sum([]byte(mention.Source)))
|
||||||
err = UpdateFile(filePath, string(bytesRepresentation), "New webmention from "+mention.Source)
|
err = SelectedStorage.UpdateFile(filePath, string(bytesRepresentation), "New webmention from "+mention.Source)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,10 +174,12 @@ func returnSuccess(target string, w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
// Purge BunnyCDN in 10 seconds
|
// Purge CDN in 10 seconds
|
||||||
go func() {
|
go func() {
|
||||||
|
if SelectedCdn != nil {
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
SelectedCdn.Purge(target)
|
SelectedCdn.Purge(target)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
return
|
return
|
||||||
}
|
}
|
Loading…
Reference in New Issue