Skip to content

Commit

Permalink
Public release
Browse files Browse the repository at this point in the history
p2004a committed Jun 19, 2022
0 parents commit 12b9c9b
Showing 9 changed files with 657 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: ci
on: [push, pull_request]
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '^1.18.0'
- name: Build
run: go build ./...
- name: Test
run: go test ./... -v
- name: Format
run: gofmt -d . && [ -z "$(gofmt -l .)" ]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/rapidsyncer
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2022 Marek Rusinowski

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Spring RTS Rapid syncer

A small stateless utility that can mirror content of a Spring RTS
[Rapid](https://springrts.com/wiki/Rapid) repository to some other hosting.

Currently it supports [Bunny Edge Storage](https://bunny.net/edge-storage/) as a
target destination for syncing. Binary runs a HTTP server with `/sync` endpoint
that performs mirroring when triggered. This allows to deploy it as a cheap
[Google Cloud Run](https://cloud.google.com/run) service triggered by the [Cloud
Scheduler](https://cloud.google.com/scheduler) periodically.

### TODO
- [ ] More automated tests
- [ ] Refactor destinations and allow at least local storage one for testing
- [ ] Write down the GCP project deployment spec in terraform/pulimi.
16 changes: 16 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM docker.io/library/golang:1.18 AS build

WORKDIR /go/src/app

COPY go.mod ./
COPY go.sum ./
RUN go mod download -x

COPY . .
RUN CGO_ENABLED=0 go build ./cmd/rapidsyncer

FROM gcr.io/distroless/static-debian11
WORKDIR /
USER nonroot:nonroot
COPY --from=build /go/src/app/rapidsyncer /
ENTRYPOINT ["/rapidsyncer"]
89 changes: 89 additions & 0 deletions cmd/rapidsyncer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-FileCopyrightText: 2022 Marek Rusinowski
// SPDX-License-Identifier: Apache-2.0

package main

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"

"github.com/p2004a/spring-rapid-syncer/pkg/syncer"
)

type Server struct {
syncer *syncer.RapidSyncer
srcRepoRoot, destRepoRoot string
}

func (s *Server) HandleSync(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Printf("Got %s, not POST request for URL: %v", r.Method, r.URL)
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
var repos []string
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&repos)
if err != nil {
log.Printf("Decoding request body: %v", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
synced := make([]int, len(repos))
var out strings.Builder
for i, repo := range repos {
srcRepo := s.srcRepoRoot + repo + "/"
dstRepo := s.destRepoRoot + repo + "/"
synced[i], err = s.syncer.Sync(r.Context(), srcRepo, dstRepo)
if err != nil {
log.Printf("Failed to sync: %v", err)
http.Error(w, "Sync Failed", http.StatusInternalServerError)
return
}
msg := fmt.Sprintf("Synced %d archives to %s", synced[i], repo)
if synced[i] > 0 {
log.Printf(msg)
}
out.WriteString(msg)
out.WriteString("\n")
}

w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
io.WriteString(w, out.String())
}

func main() {
sourceRapidRepo := os.Getenv("SOURCE_RAPID_REPO")
if sourceRapidRepo == "" {
sourceRapidRepo = "https://repos.springrts.com/"
}
bunnyStorageZone := os.Getenv("BUNNY_STORAGE_ZONE")
if bunnyStorageZone == "" {
log.Fatalf("Missing required env variable BUNNY_STORAGE_ZONE")
}
bunnyAccessKey := os.Getenv("BUNNY_ACCESS_KEY")
if bunnyAccessKey == "" {
log.Fatalf("Missing required env variable BUNNY_ACCESS_KEY")
}
server := &Server{
syncer: syncer.NewRapidSyncer(bunnyAccessKey),
srcRepoRoot: sourceRapidRepo,
destRepoRoot: fmt.Sprintf("https://storage.bunnycdn.com/%s/", bunnyStorageZone),
}

http.HandleFunc("/sync", server.HandleSync)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/p2004a/spring-rapid-syncer

go 1.18
Empty file added go.sum
Empty file.
499 changes: 499 additions & 0 deletions pkg/syncer/syncer.go

Large diffs are not rendered by default.

0 comments on commit 12b9c9b

Please sign in to comment.