jlelse
/
hugo-micropub
Archived
1
Fork 0

Some improvements

This commit is contained in:
Jan-Lukas Else 2019-12-06 10:32:30 +01:00
parent 965bd45f84
commit 912705e1f1
9 changed files with 94 additions and 56 deletions

View File

@ -5,14 +5,14 @@ import (
) )
func Purge(url string) { func Purge(url string) {
accessKey, err := GetBunnyCDNKey() if len(BunnyCdnKey) == 0 {
if err != nil || len(accessKey) == 0 { // BunnyCdn deactivated
return return
} }
client := &http.Client{} client := &http.Client{}
req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil) req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil)
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
req.Header.Add("AccessKey", accessKey) req.Header.Add("AccessKey", BunnyCdnKey)
_, _ = client.Do(req) _, _ = client.Do(req)
} }

View File

@ -2,10 +2,52 @@ package main
import ( import (
"errors" "errors"
"log"
"os" "os"
"strings"
) )
func GetGiteaEndpoint() (string, error) { var (
BlogUrl string
GiteaEndpoint string
GiteaToken string
BunnyCdnKey string
IgnoredWebmentionUrls []string
)
func init() {
// Blog URL (required)
blogUrl, err := blogUrl()
if err != nil {
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
// BunnyCDN (optional)
bunnyCdnKey, err := bunnyCdnKey()
if err != nil {
log.Println(err)
}
BunnyCdnKey = bunnyCdnKey
// Ignored Webmention URLs (optional)
ignoredWebmentionUrls, err := ignoredWebmentionUrls()
if err != nil {
log.Println(err)
}
IgnoredWebmentionUrls = ignoredWebmentionUrls
}
func giteaEndpoint() (string, error) {
giteaEndpoint := os.Getenv("GITEA_ENDPOINT") giteaEndpoint := os.Getenv("GITEA_ENDPOINT")
if len(giteaEndpoint) == 0 || giteaEndpoint == "" { if len(giteaEndpoint) == 0 || giteaEndpoint == "" {
return "", errors.New("GITEA_ENDPOINT not specified") return "", errors.New("GITEA_ENDPOINT not specified")
@ -13,7 +55,7 @@ func GetGiteaEndpoint() (string, error) {
return giteaEndpoint, nil return giteaEndpoint, nil
} }
func GetGiteaToken() (string, error) { func giteaToken() (string, error) {
giteaToken := os.Getenv("GITEA_TOKEN") giteaToken := os.Getenv("GITEA_TOKEN")
if len(giteaToken) == 0 || giteaToken == "" { if len(giteaToken) == 0 || giteaToken == "" {
return "", errors.New("GITEA_TOKEN not specified") return "", errors.New("GITEA_TOKEN not specified")
@ -21,7 +63,7 @@ func GetGiteaToken() (string, error) {
return giteaToken, nil return giteaToken, nil
} }
func GetBlogURL() (string, error) { func blogUrl() (string, error) {
blogURL := os.Getenv("BLOG_URL") blogURL := os.Getenv("BLOG_URL")
if len(blogURL) == 0 || blogURL == "" { if len(blogURL) == 0 || blogURL == "" {
return "", errors.New("BLOG_URL not specified") return "", errors.New("BLOG_URL not specified")
@ -29,11 +71,18 @@ func GetBlogURL() (string, error) {
return blogURL, nil return blogURL, nil
} }
func bunnyCdnKey() (string, error) {
func GetBunnyCDNKey() (string, error) {
bunnyCDNKey := os.Getenv("BUNNY_CDN_KEY") bunnyCDNKey := os.Getenv("BUNNY_CDN_KEY")
if len(bunnyCDNKey) == 0 || bunnyCDNKey == "" { if len(bunnyCDNKey) == 0 || bunnyCDNKey == "" {
return "", errors.New("BUNNY_CDN_KEY not specified") return "", errors.New("BUNNY_CDN_KEY not specified, BunnyCDN features are deactivated")
} }
return bunnyCDNKey, nil return bunnyCDNKey, nil
} }
func ignoredWebmentionUrls() ([]string, error) {
webmentionIgnored := os.Getenv("WEBMENTION_IGNORED")
if len(webmentionIgnored) == 0 {
return nil, errors.New("WEBMENTION_IGNORED not set, no URLs are ignored on Webmention sending")
}
return strings.Split(webmentionIgnored, ","), nil
}

View File

@ -103,6 +103,9 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
} }
func computeExtraSettings(entry *Entry) error { func computeExtraSettings(entry *Entry) error {
now := time.Now()
// Set date
entry.date = now.Format(time.RFC3339)
// Find settings hidden in content // Find settings hidden in content
var filteredContent bytes.Buffer var filteredContent bytes.Buffer
contentScanner := bufio.NewScanner(strings.NewReader(entry.content)) contentScanner := bufio.NewScanner(strings.NewReader(entry.content))
@ -140,30 +143,25 @@ func computeExtraSettings(entry *Entry) error {
} }
} }
entry.content = filteredContent.String() entry.content = filteredContent.String()
now := time.Now()
// Compute slug if empty // Compute slug if empty
if len(entry.slug) == 0 || entry.slug == "" { if len(entry.slug) == 0 || entry.slug == "" {
random := generateRandomString(now, 5) random := generateRandomString(now, 5)
entry.slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random) entry.slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random)
} }
// Compute filename and location // Compute filename and location
blogURL, err := GetBlogURL()
if err != nil {
return err
}
if len(entry.section) < 1 { if len(entry.section) < 1 {
entry.section = "micro" entry.section = "micro"
} }
entry.section = strings.ToLower(entry.section) entry.section = strings.ToLower(entry.section)
if entry.section == "posts" { if entry.section == "posts" {
entry.filename = "content/" + entry.section + "/" + entry.slug + ".md" entry.filename = "content/" + entry.section + "/" + entry.slug + ".md"
entry.location = blogURL + entry.section + "/" + entry.slug entry.location = BlogUrl + entry.section + "/" + entry.slug
} else if entry.section == "thoughts" || entry.section == "links" || entry.section == "micro" { } else if entry.section == "thoughts" || entry.section == "links" || entry.section == "micro" {
entry.filename = fmt.Sprintf("content/%v/%02d/%02d/%v.md", entry.section, now.Year(), int(now.Month()), entry.slug) entry.filename = fmt.Sprintf("content/%v/%02d/%02d/%v.md", entry.section, now.Year(), int(now.Month()), entry.slug)
entry.location = fmt.Sprintf("%v%v/%02d/%02d/%v", blogURL, entry.section, now.Year(), int(now.Month()), entry.slug) entry.location = fmt.Sprintf("%v%v/%02d/%02d/%v", BlogUrl, entry.section, now.Year(), int(now.Month()), entry.slug)
} else { } else {
entry.filename = "content/" + entry.section + "/" + entry.slug + ".md" entry.filename = "content/" + entry.section + "/" + entry.slug + ".md"
entry.location = blogURL + entry.section + "/" + entry.slug entry.location = BlogUrl + entry.section + "/" + entry.slug
} }
return nil return nil
} }
@ -192,11 +190,10 @@ func WriteEntry(entry *Entry) (location string, err error) {
} }
func analyzeURL(url string) (filePath string, section string, slug string, err error) { func analyzeURL(url string) (filePath string, section string, slug string, err error) {
blogUrl, err := GetBlogURL() if !strings.HasPrefix(url, BlogUrl) {
if err != nil || !strings.HasPrefix(url, blogUrl) {
return return
} }
path := strings.TrimSuffix(strings.TrimPrefix(url, blogUrl), "/") path := strings.TrimSuffix(strings.TrimPrefix(url, BlogUrl), "/")
pathParts := strings.Split(path, "/") pathParts := strings.Split(path, "/")
filePath = "content/" + path + ".md" filePath = "content/" + path + ".md"
section = pathParts[0] section = pathParts[0]

View File

@ -11,14 +11,6 @@ import (
) )
func CreateFile(path string, file string, name string) error { func CreateFile(path string, file string, name string) error {
giteaEndpoint, err := GetGiteaEndpoint()
if err != nil {
return err
}
giteaToken, err := GetGiteaToken()
if err != nil {
return err
}
message := map[string]interface{}{ message := map[string]interface{}{
"message": name, "message": name,
"content": base64.StdEncoding.EncodeToString([]byte(file)), "content": base64.StdEncoding.EncodeToString([]byte(file)),
@ -28,7 +20,7 @@ func CreateFile(path string, file string, name string) error {
return errors.New("failed to marshal json before committing") return errors.New("failed to marshal json before committing")
} }
// TODO: handle file updating // TODO: handle file updating
resp, err := http.Post(giteaEndpoint+url.QueryEscape(path)+"?access_token="+giteaToken, "application/json", bytes.NewBuffer(bytesRepresentation)) resp, err := http.Post(GiteaEndpoint+url.QueryEscape(path)+"?access_token="+GiteaToken, "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")
} }
@ -36,15 +28,7 @@ func CreateFile(path string, file string, name string) error {
} }
func ReadFile(path string) (fileContent string, err error) { func ReadFile(path string) (fileContent string, err error) {
giteaEndpoint, err := GetGiteaEndpoint() resp, err := http.Get(GiteaEndpoint + url.QueryEscape(path) + "?access_token=" + GiteaToken)
if err != nil {
return
}
giteaToken, err := GetGiteaToken()
if err != nil {
return
}
resp, err := http.Get(giteaEndpoint + url.QueryEscape(path) + "?access_token=" + giteaToken)
if err != nil || resp.StatusCode != 200 { if err != nil || resp.StatusCode != 200 {
err = errors.New("failed to read file in repo") err = errors.New("failed to read file in repo")
return return

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -91,5 +92,7 @@ func main() {
http.HandleFunc("/", handleMicroPub) http.HandleFunc("/", handleMicroPub)
log.Println("Starting micropub server...") log.Println("Starting micropub server...")
log.Println("Current time: " + time.Now().Format(time.RFC3339)) log.Println("Current time: " + time.Now().Format(time.RFC3339))
log.Println("Blog URL: " + BlogUrl)
log.Println("Ignored URLs for Webmention: " + strings.Join(IgnoredWebmentionUrls, ", "))
log.Fatal(http.ListenAndServe(":5555", nil)) log.Fatal(http.ListenAndServe(":5555", nil))
} }

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"strings" "strings"
"time"
) )
type Frontmatter struct { type Frontmatter struct {
@ -36,7 +35,7 @@ func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
var buff bytes.Buffer var buff bytes.Buffer
writeFrontmatter := &Frontmatter{ writeFrontmatter := &Frontmatter{
Title: entry.title, Title: entry.title,
Date: time.Now().Format(time.RFC3339), Date: entry.date,
Lastmod: entry.lastmod, Lastmod: entry.lastmod,
Tags: entry.tags, Tags: entry.tags,
ExternalURL: entry.link, ExternalURL: entry.link,

View File

@ -26,14 +26,10 @@ type Properties struct {
} }
func QueryURL(url string, limit int) ([]byte, error) { func QueryURL(url string, limit int) ([]byte, error) {
blogURL, err := GetBlogURL()
if err != nil {
return nil, err
}
if len(url) == 0 { if len(url) == 0 {
url = blogURL url = BlogUrl
} }
if url == blogURL { if url == BlogUrl {
allPosts, err := allPosts(url) allPosts, err := allPosts(url)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -58,11 +58,7 @@ func checkAccess(token string) (bool, error) {
return false, errors.New("Error parsing the response into json for checking token access " + err.Error()) return false, errors.New("Error parsing the response into json for checking token access " + err.Error())
} }
// verify results of the response // verify results of the response
blogURL, err := GetBlogURL() if indieAuthRes.Me != BlogUrl {
if err != nil {
return false, err
}
if indieAuthRes.Me != blogURL {
return false, errors.New("me does not match") return false, errors.New("me does not match")
} }
scopes := strings.Fields(indieAuthRes.Scope) scopes := strings.Fields(indieAuthRes.Scope)

View File

@ -12,11 +12,8 @@ func SendWebmentions(url string) {
if err != nil { if err != nil {
return return
} }
for _, link := range dl { // Send Webmentions
blogUrl, err := GetBlogURL() for _, link := range filterLinks(dl) {
if err != nil || strings.HasPrefix(link, blogUrl) {
continue
}
endpoint, err := client.DiscoverEndpoint(link) endpoint, err := client.DiscoverEndpoint(link)
if err != nil || len(endpoint) < 1 { if err != nil || len(endpoint) < 1 {
continue continue
@ -29,3 +26,20 @@ func SendWebmentions(url string) {
log.Println("Sent webmention to " + link) log.Println("Sent webmention to " + link)
} }
} }
func filterLinks(links []string) []string {
var filteredLinks []string
LINKFILTER:
for _, link := range links {
if strings.HasPrefix(link, BlogUrl) {
continue
}
for _, ignoredURL := range IgnoredWebmentionUrls {
if strings.HasPrefix(link, ignoredURL) {
continue LINKFILTER
}
}
filteredLinks = append(filteredLinks, link)
}
return filteredLinks
}