Skip to content

Commit

Permalink
feat: simple caching proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed Oct 20, 2024
1 parent d2f7bb7 commit d93f9e0
Show file tree
Hide file tree
Showing 36 changed files with 129 additions and 27 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/build-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Build and Publish

on:
push:

permissions:
packages: write
contents: read

jobs:
build-and-push-docker-image:
name: Build Docker image and push
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: 'stable'

- name: Setup ko
uses: ko-build/[email protected]

- name: Login to Github Packages
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build containers
run: |
ko build --bare --sbom=none -t latest .
env:
KO_DOCKER_REPO: ghcr.io/syncthing/apt-web
23 changes: 0 additions & 23 deletions .github/workflows/publish-web.yaml

This file was deleted.

4 changes: 0 additions & 4 deletions README.md

This file was deleted.

5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/syncthing/apt-web

go 1.23.2

require github.com/meerkat-dashboard/meerkat v0.0.0-20240605084813-bc196007bb0c
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/meerkat-dashboard/meerkat v0.0.0-20240605084813-bc196007bb0c h1:jwuFa8OpeTeXT3EOq5IsiTiYKlaOU4isntQlDbZkBp8=
github.com/meerkat-dashboard/meerkat v0.0.0-20240605084813-bc196007bb0c/go.mod h1:2qaHfLheTyaCMPpcjl/hUmRPdByJrZWQAAEn1ClllTA=
82 changes: 82 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"cmp"
"embed"
"io/fs"
"log/slog"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path"
"time"

cacheProxy "github.com/meerkat-dashboard/meerkat/proxy"
)

//go:embed site
var siteFS embed.FS

var (
listenAddr = cmp.Or(os.Getenv("LISTEN_ADDRESS"), ":8080")
distsHost = cmp.Or(os.Getenv("DISTS_HOST"), "https://syncthing-apt.s3.fr-par.scw.cloud")
)

func main() {
subFS, _ := fs.Sub(fs.FS(siteFS), "site")
site := http.FS(subFS)
http.Handle("/", http.FileServer(site))

proxy, err := newCachingProxy(distsHost, 5*time.Minute)
if err != nil {
slog.Error("failed to construct proxy", "error", err)
os.Exit(2)
}
filtered := validateFilename(proxy, []string{
"InRelease",
"InRelease.gz",
"Release",
"Release.gz",
"Release.gpg",
"Release.gpg.gz",
"Packages",
"Packages.gz",
"*.deb",
})
http.Handle("/dists/", filtered)

if err := http.ListenAndServe(listenAddr, nil); err != nil {
slog.Error("failed to listen", "error", err)
}
}

func validateFilename(next http.Handler, names []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
name := path.Base(req.URL.Path)
for _, valid := range names {
if ok, _ := path.Match(valid, name); ok {
next.ServeHTTP(w, req)
return
}
}
http.NotFound(w, req)
})
}

func newCachingProxy(next string, cacheTime time.Duration) (http.Handler, error) {
remote, err := url.Parse(next)
if err != nil {
return nil, err
}
rev := &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(remote)
},
}
cp := &cacheProxy.Proxy{}
cp.Next = rev
rev.ModifyResponse = cp.StoreOKResponse(cacheTime)

return cp, nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.

0 comments on commit d93f9e0

Please sign in to comment.