Some improvements
parent
965bd45f84
commit
912705e1f1
|
@ -5,14 +5,14 @@ import (
|
|||
)
|
||||
|
||||
func Purge(url string) {
|
||||
accessKey, err := GetBunnyCDNKey()
|
||||
if err != nil || len(accessKey) == 0 {
|
||||
if len(BunnyCdnKey) == 0 {
|
||||
// BunnyCdn deactivated
|
||||
return
|
||||
}
|
||||
client := &http.Client{}
|
||||
req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil)
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("AccessKey", accessKey)
|
||||
req.Header.Add("AccessKey", BunnyCdnKey)
|
||||
_, _ = client.Do(req)
|
||||
}
|
||||
|
|
61
config.go
61
config.go
|
@ -2,10 +2,52 @@ package main
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"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")
|
||||
if len(giteaEndpoint) == 0 || giteaEndpoint == "" {
|
||||
return "", errors.New("GITEA_ENDPOINT not specified")
|
||||
|
@ -13,7 +55,7 @@ func GetGiteaEndpoint() (string, error) {
|
|||
return giteaEndpoint, nil
|
||||
}
|
||||
|
||||
func GetGiteaToken() (string, error) {
|
||||
func giteaToken() (string, error) {
|
||||
giteaToken := os.Getenv("GITEA_TOKEN")
|
||||
if len(giteaToken) == 0 || giteaToken == "" {
|
||||
return "", errors.New("GITEA_TOKEN not specified")
|
||||
|
@ -21,7 +63,7 @@ func GetGiteaToken() (string, error) {
|
|||
return giteaToken, nil
|
||||
}
|
||||
|
||||
func GetBlogURL() (string, error) {
|
||||
func blogUrl() (string, error) {
|
||||
blogURL := os.Getenv("BLOG_URL")
|
||||
if len(blogURL) == 0 || blogURL == "" {
|
||||
return "", errors.New("BLOG_URL not specified")
|
||||
|
@ -29,11 +71,18 @@ func GetBlogURL() (string, error) {
|
|||
return blogURL, nil
|
||||
}
|
||||
|
||||
|
||||
func GetBunnyCDNKey() (string, error) {
|
||||
func bunnyCdnKey() (string, error) {
|
||||
bunnyCDNKey := os.Getenv("BUNNY_CDN_KEY")
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
19
entry.go
19
entry.go
|
@ -103,6 +103,9 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
|
|||
}
|
||||
|
||||
func computeExtraSettings(entry *Entry) error {
|
||||
now := time.Now()
|
||||
// Set date
|
||||
entry.date = now.Format(time.RFC3339)
|
||||
// Find settings hidden in content
|
||||
var filteredContent bytes.Buffer
|
||||
contentScanner := bufio.NewScanner(strings.NewReader(entry.content))
|
||||
|
@ -140,30 +143,25 @@ func computeExtraSettings(entry *Entry) error {
|
|||
}
|
||||
}
|
||||
entry.content = filteredContent.String()
|
||||
now := time.Now()
|
||||
// Compute slug if empty
|
||||
if len(entry.slug) == 0 || entry.slug == "" {
|
||||
random := generateRandomString(now, 5)
|
||||
entry.slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random)
|
||||
}
|
||||
// Compute filename and location
|
||||
blogURL, err := GetBlogURL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(entry.section) < 1 {
|
||||
entry.section = "micro"
|
||||
}
|
||||
entry.section = strings.ToLower(entry.section)
|
||||
if entry.section == "posts" {
|
||||
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" {
|
||||
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 {
|
||||
entry.filename = "content/" + entry.section + "/" + entry.slug + ".md"
|
||||
entry.location = blogURL + entry.section + "/" + entry.slug
|
||||
entry.location = BlogUrl + entry.section + "/" + entry.slug
|
||||
}
|
||||
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) {
|
||||
blogUrl, err := GetBlogURL()
|
||||
if err != nil || !strings.HasPrefix(url, blogUrl) {
|
||||
if !strings.HasPrefix(url, BlogUrl) {
|
||||
return
|
||||
}
|
||||
path := strings.TrimSuffix(strings.TrimPrefix(url, blogUrl), "/")
|
||||
path := strings.TrimSuffix(strings.TrimPrefix(url, BlogUrl), "/")
|
||||
pathParts := strings.Split(path, "/")
|
||||
filePath = "content/" + path + ".md"
|
||||
section = pathParts[0]
|
||||
|
|
20
gitea.go
20
gitea.go
|
@ -11,14 +11,6 @@ import (
|
|||
)
|
||||
|
||||
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": name,
|
||||
"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")
|
||||
}
|
||||
// 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 {
|
||||
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) {
|
||||
giteaEndpoint, err := GetGiteaEndpoint()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
giteaToken, err := GetGiteaToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp, err := http.Get(giteaEndpoint + url.QueryEscape(path) + "?access_token=" + giteaToken)
|
||||
resp, err := http.Get(GiteaEndpoint + url.QueryEscape(path) + "?access_token=" + GiteaToken)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
err = errors.New("failed to read file in repo")
|
||||
return
|
||||
|
|
3
main.go
3
main.go
|
@ -4,6 +4,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -91,5 +92,7 @@ func main() {
|
|||
http.HandleFunc("/", handleMicroPub)
|
||||
log.Println("Starting micropub server...")
|
||||
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))
|
||||
}
|
||||
|
|
3
post.go
3
post.go
|
@ -5,7 +5,6 @@ import (
|
|||
"errors"
|
||||
"gopkg.in/yaml.v2"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Frontmatter struct {
|
||||
|
@ -36,7 +35,7 @@ func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
|
|||
var buff bytes.Buffer
|
||||
writeFrontmatter := &Frontmatter{
|
||||
Title: entry.title,
|
||||
Date: time.Now().Format(time.RFC3339),
|
||||
Date: entry.date,
|
||||
Lastmod: entry.lastmod,
|
||||
Tags: entry.tags,
|
||||
ExternalURL: entry.link,
|
||||
|
|
8
query.go
8
query.go
|
@ -26,14 +26,10 @@ type Properties struct {
|
|||
}
|
||||
|
||||
func QueryURL(url string, limit int) ([]byte, error) {
|
||||
blogURL, err := GetBlogURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(url) == 0 {
|
||||
url = blogURL
|
||||
url = BlogUrl
|
||||
}
|
||||
if url == blogURL {
|
||||
if url == BlogUrl {
|
||||
allPosts, err := allPosts(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
// verify results of the response
|
||||
blogURL, err := GetBlogURL()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if indieAuthRes.Me != blogURL {
|
||||
if indieAuthRes.Me != BlogUrl {
|
||||
return false, errors.New("me does not match")
|
||||
}
|
||||
scopes := strings.Fields(indieAuthRes.Scope)
|
||||
|
|
|
@ -12,11 +12,8 @@ func SendWebmentions(url string) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, link := range dl {
|
||||
blogUrl, err := GetBlogURL()
|
||||
if err != nil || strings.HasPrefix(link, blogUrl) {
|
||||
continue
|
||||
}
|
||||
// Send Webmentions
|
||||
for _, link := range filterLinks(dl) {
|
||||
endpoint, err := client.DiscoverEndpoint(link)
|
||||
if err != nil || len(endpoint) < 1 {
|
||||
continue
|
||||
|
@ -29,3 +26,20 @@ func SendWebmentions(url string) {
|
|||
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
|
||||
}
|
Loading…
Reference in New Issue