-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0 parents
commit 12b9c9b
Showing
9 changed files
with
657 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 .)" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/rapidsyncer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Large diffs are not rendered by default.
Oops, something went wrong.