Browse Source

Initial commit

master
Jan-Lukas Else 5 months ago
commit
c7cd0a1f60
6 changed files with 139 additions and 0 deletions
  1. +20
    -0
      .drone.yml
  2. +2
    -0
      .gitignore
  3. +10
    -0
      Dockerfile
  4. +35
    -0
      feed.go
  5. +3
    -0
      go.mod
  6. +69
    -0
      main.go

+ 20
- 0
.drone.yml View File

@@ -0,0 +1,20 @@
kind: pipeline
name: default

steps:
- name: publish
image: plugins/docker
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
repo: quay.io/jlelse/bunnypurge
registry: quay.io
tags: latest
when:
branch:
- master
event:
exclude:
- pull_request

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
/.idea
/BunnyPurge

+ 10
- 0
Dockerfile View File

@@ -0,0 +1,10 @@
FROM golang:1.14-alpine as build
ADD . /app
WORKDIR /app
RUN go build

FROM alpine:3.11
RUN apk add --no-cache ca-certificates
COPY --from=build /app/BunnyPurge /bin/
EXPOSE 8080
CMD ["BunnyPurge"]

+ 35
- 0
feed.go View File

@@ -0,0 +1,35 @@
package main

import (
"encoding/json"
"errors"
"net/http"
)

type Article struct {
Url string `json:"url"`
}

func LatestArticle(url string) (*Article, error) {
jsonFeed := &struct {
Items []Article `json:"items"`
}{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.New("failed to create req to get json feed")
}
req.Header.Add("User-Agent", "BunnyPurge")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.New("failed to get json feed")
}
err = json.NewDecoder(resp.Body).Decode(&jsonFeed)
_ = resp.Body.Close()
if err != nil {
return nil, errors.New("failed to parse json feed")
}
if len(jsonFeed.Items) < 1 {
return nil, errors.New("no articles in feed")
}
return &jsonFeed.Items[0], nil
}

+ 3
- 0
go.mod View File

@@ -0,0 +1,3 @@
module git.jlel.se/jlelse/BunnyPurge

go 1.14

+ 69
- 0
main.go View File

@@ -0,0 +1,69 @@
package main

import (
"fmt"
"log"
"net/http"
)

func main() {
http.HandleFunc("/hook", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Wrong HTTP method", http.StatusMethodNotAllowed)
return
}

bunnyCdnKey, keyPresent := r.URL.Query()["key"]
if !keyPresent || len(bunnyCdnKey[0]) < 1 {
http.Error(w, "Wrong HTTP method", http.StatusBadRequest)
return
}

feeds, feedsPresent := r.URL.Query()["feed"]
urls, urlsPresent := r.URL.Query()["url"]

go func() {
var allUrls []string

if feedsPresent {
for _, feed := range feeds {
article, err := LatestArticle(feed)
if err != nil {
fmt.Println(err.Error())
continue
}

if article.Url != "" {
allUrls = append(allUrls, article.Url)
}
}
}

if urlsPresent {
for _, url := range urls {
allUrls = append(allUrls, url)
}
}

purge(allUrls, bunnyCdnKey[0])
}()

})
log.Fatal(http.ListenAndServe(":8080", nil))
}

func purge(urls []string, bunnyCdnKey string) {
for _, url := range urls {
fmt.Println("Purge:", url)
client := http.DefaultClient
req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil)
req.Header.Add("User-Agent", "BunnyPurge")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("AccessKey", bunnyCdnKey)
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Failed to purge", url)
}
}
}

Loading…
Cancel
Save