More refactoring
parent
cf4dd37484
commit
a7b987cca2
42
config.go
42
config.go
|
@ -9,10 +9,9 @@ import (
|
|||
|
||||
var (
|
||||
BlogUrl string
|
||||
GiteaEndpoint string
|
||||
GiteaToken string
|
||||
IgnoredWebmentionUrls []string
|
||||
SyndicationTargets []SyndicationTarget
|
||||
SelectedStorage Storage
|
||||
SelectedCdn Cdn
|
||||
SelectedSocials Socials
|
||||
)
|
||||
|
@ -29,17 +28,6 @@ func init() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
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)
|
||||
ignoredWebmentionUrls, err := ignoredWebmentionUrls()
|
||||
if err != nil {
|
||||
|
@ -52,7 +40,23 @@ func init() {
|
|||
log.Println(err)
|
||||
}
|
||||
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 {
|
||||
// BunnyCDN (optional)
|
||||
bunnyCdnKey, err := bunnyCdnKey()
|
||||
|
@ -61,9 +65,12 @@ func init() {
|
|||
}
|
||||
return nil
|
||||
}()
|
||||
// Find configured social networks
|
||||
if SelectedCdn == nil {
|
||||
log.Println("No CDN configured")
|
||||
}
|
||||
// Find configured social networks (optional)
|
||||
SelectedSocials = func() Socials {
|
||||
var socials []Social
|
||||
var socials []Social = nil
|
||||
// Microblog.pub
|
||||
microblogUrl, err1 := microblogUrl()
|
||||
microblogToken, err2 := microblogToken()
|
||||
|
@ -75,6 +82,9 @@ func init() {
|
|||
}
|
||||
return socials
|
||||
}()
|
||||
if SelectedSocials == nil {
|
||||
log.Println("No social networks configured")
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
return
|
||||
}
|
||||
err = CreateFile(entry.filename, file, entry.title)
|
||||
err = SelectedStorage.CreateFile(entry.filename, file, entry.title)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -257,8 +257,8 @@ func ReadEntry(url string) (entry *Entry, err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
fileContent, err := ReadFileContent(filePath)
|
||||
if err != nil {
|
||||
fileContent, _, exists, err := SelectedStorage.ReadFile(filePath)
|
||||
if err != nil || !exists {
|
||||
return
|
||||
}
|
||||
entry, err = ReadHugoPost(fileContent)
|
||||
|
|
12
micropub.go
12
micropub.go
|
@ -80,14 +80,20 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
|
|||
} else {
|
||||
w.Header().Add("Location", location)
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
// Purge BunnyCDN in 10 seconds
|
||||
// Purge CDN in 10 seconds, send webmentions, post to social media
|
||||
go func() {
|
||||
if SelectedCdn != nil {
|
||||
time.Sleep(10 * time.Second)
|
||||
SelectedCdn.Purge(location)
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
time.Sleep(10 * time.Second)
|
||||
// Send webmentions
|
||||
go SendWebmentions(location)
|
||||
go SelectedSocials.Post(location, entry.title)
|
||||
go func() {
|
||||
if SelectedSocials != nil {
|
||||
SelectedSocials.Post(location, entry.title)
|
||||
}
|
||||
}()
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
|
|
@ -11,24 +11,36 @@ import (
|
|||
"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"`
|
||||
Message string `json:"message"`
|
||||
SHA string `json:"sha,omitempty"`
|
||||
}
|
||||
|
||||
type GiteaReadRequest struct {
|
||||
type giteaReadResponse struct {
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content"`
|
||||
SHA string `json:"sha"`
|
||||
}
|
||||
|
||||
type GiteaErrorResponse struct {
|
||||
type giteaErrorResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func CreateFile(path string, file string, message string) error {
|
||||
request := &GiteaCommitRequest{
|
||||
func (gitea *Gitea) CreateFile(path string, file string, message string) (err error) {
|
||||
request := &giteaCommitRequest{
|
||||
Content: base64.StdEncoding.EncodeToString([]byte(file)),
|
||||
Message: message,
|
||||
}
|
||||
|
@ -36,32 +48,32 @@ func CreateFile(path string, file string, message string) error {
|
|||
if err != nil {
|
||||
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 {
|
||||
return errors.New("failed to create file in repo")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateFile(path string, file string, message string) error {
|
||||
existingFile, exists, err := ReadFile(path)
|
||||
func (gitea *Gitea) UpdateFile(path string, file string, message string) (err error) {
|
||||
_, sha, exists, err := gitea.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
// 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)),
|
||||
Message: message,
|
||||
SHA: existingFile.SHA,
|
||||
SHA: sha,
|
||||
}
|
||||
bytesRepresentation, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
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 {
|
||||
return errors.New("error making update request")
|
||||
}
|
||||
|
@ -74,9 +86,9 @@ func UpdateFile(path string, file string, message string) error {
|
|||
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
|
||||
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 resp != nil {
|
||||
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")
|
||||
return
|
||||
}
|
||||
errorResponse := &GiteaErrorResponse{}
|
||||
errorResponse := &giteaErrorResponse{}
|
||||
marshalErr := json.Unmarshal(body, &errorResponse)
|
||||
if marshalErr != nil {
|
||||
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")
|
||||
return
|
||||
}
|
||||
response = &GiteaReadRequest{}
|
||||
err = json.Unmarshal(body, &response)
|
||||
readResponse := &giteaReadResponse{}
|
||||
err = json.Unmarshal(body, &readResponse)
|
||||
if err != nil {
|
||||
err = errors.New("failed parsing Gitea response")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
decodedContentBytes, err := base64.StdEncoding.DecodeString(readResponse.Content)
|
||||
if err != nil {
|
||||
err = errors.New("failed decoding file content")
|
||||
}
|
||||
fileContent = string(decodedBytes)
|
||||
content = string(decodedContentBytes)
|
||||
sha = readResponse.SHA
|
||||
return
|
||||
}
|
|
@ -162,7 +162,7 @@ func saveWebmention(mention *Mention) (err error) {
|
|||
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)))
|
||||
err = UpdateFile(filePath, string(bytesRepresentation), "New webmention from "+mention.Source)
|
||||
err = SelectedStorage.UpdateFile(filePath, string(bytesRepresentation), "New webmention from "+mention.Source)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -174,10 +174,12 @@ func returnSuccess(target string, w http.ResponseWriter, r *http.Request) {
|
|||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
// Purge BunnyCDN in 10 seconds
|
||||
// Purge CDN in 10 seconds
|
||||
go func() {
|
||||
if SelectedCdn != nil {
|
||||
time.Sleep(10 * time.Second)
|
||||
SelectedCdn.Purge(target)
|
||||
}
|
||||
}()
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue