diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e338340 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +RELAYS="https://bsky.network" +PDS_HOST="{your host here}" +PORT="4455" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8d88ee4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:alpine AS builder + +WORKDIR /app + +COPY . . +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 go build -o subscribe-repos main.go + +FROM alpine:latest + +RUN apk --no-cache add ca-certificates + +WORKDIR /app/ +COPY --from=builder /app/subscribe-repos . + +ENTRYPOINT ["./subscribe-repos"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..88ab6f2 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## Subscribe repos + +This is a POC service that is described [here](https://willdot.leaflet.pub/3miu5jbrsrc2p). It's used as part of creating a distrbuted PDS diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..ea2c351 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,18 @@ +services: + subscribe-repos: + container_name: subscribe-repos + image: atcr.io/willdot.net/subscribe-repos:latest + ports: + - "4455:4455" + restart: always + volumes: + - ./data:/app/data + environment: + ENV_LOCATION: "/app/data/.env" + network_mode: host + healthcheck: + test: ["CMD", "wget", "--spider", "http://localhost:4455/health"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 40s diff --git a/main.go b/main.go index 45811f5..c5dba91 100644 --- a/main.go +++ b/main.go @@ -19,18 +19,28 @@ import ( ) func main() { - err := godotenv.Load(".env") + envLocation := os.Getenv("ENV_LOCATION") + if envLocation == "" { + envLocation = ".env" + } + + err := godotenv.Load(envLocation) if err != nil { if !os.IsNotExist(err) { log.Fatal("Error loading .env file") } } + hostname := os.Getenv("PDS_HOST") + slog.Info("hostname", "is", hostname) + + relays := strings.Split(os.Getenv("RELAYS"), ",") + srv := Server{ upgrader: websocket.Upgrader{}, conns: make(map[string]*websocket.Conn), - relays: strings.Split(os.Getenv("RELAYS"), ","), - hostname: os.Getenv("HOSTNAME"), + relays: relays, + hostname: hostname, } go srv.requestCrawl(context.Background()) @@ -38,8 +48,17 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("/xrpc/com.atproto.sync.subscribeRepos", srv.handleSyncSubscribeRepos) mux.HandleFunc("POST /events", srv.handleNewEvent) + mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("hello")) + }) + + port := os.Getenv("PORT") + if port == "" { + port = "3000" + slog.Info("PORT env not set - using default 3000") + } - if err := http.ListenAndServe(":3000", mux); err != nil { + if err := http.ListenAndServe(fmt.Sprintf(":%s", port), mux); err != nil { slog.Error("http listen and serve", "error", err) } }