Initial commit
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
commit
c7cd0a1f60
|
@ -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
|
|
@ -0,0 +1,2 @@
|
||||||
|
/.idea
|
||||||
|
/BunnyPurge
|
|
@ -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"]
|
|
@ -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
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue