Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Wicki <[email protected]>
  • Loading branch information
gandro committed Apr 29, 2024
0 parents commit 8e128b8
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Create and publish a Docker image

on:
push:
branches:
- main
pull_request:
branches:
- main
tags:
- v[0-9]+.[0-9]+.[0-9]+

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
if: ${{ (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') }}
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: ${{ (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:alpine as builder
COPY main.go .
RUN CGO_ENABLED=0 go build -o /go/bin/peephole main.go

FROM scratch
COPY --from=builder /go/bin/peephole /peephole
ENTRYPOINT ["/peephole"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Luxeria

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# jitsi-peephole-service

A simple service which exposes the
[mod_muc_census](https://github.com/jitsi/jitsi-meet/blob/6682b52a1947deb0cf28043d3816b1081ef84c2b/resources/prosody-plugins/mod_muc_census.lua)
Prosody room statistics for one single room (configured via `PEEPHOLE_ROOM_NAME`).

## Environment variables

- `PEEPHOLE_ROOM_NAME` _(required)_: Room name for which statistics are exposed (example: `[email protected]`)
- `PEEPHOLE_HTTP_ADDR` _(required)_: Address on which the peephole HTTP server will listen on (example: `:8080`)
- `XMPP_SERVER` _(required)_: Hostname of the Prosody HTTP service (example: `xmpp.meet.jitsi`)
- `PROSODY_HTTP_PORT` _(required)_: Port of the Prosody HTTP service (example `5280`)
106 changes: 106 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"net/url"
"os"
)

var (
roomName = envVar("PEEPHOLE_ROOM_NAME")
httpAddr = envVar("PEEPHOLE_HTTP_ADDR")

prosodyHTTPHost = envVar("XMPP_SERVER")
prosodyHTTPPort = envVar("PROSODY_HTTP_PORT")

roomCensusURL = (&url.URL{
Scheme: "http",
Host: net.JoinHostPort(prosodyHTTPHost, prosodyHTTPPort),
Path: "/room-census",
}).String()
)

func envVar(name string) string {
val := os.Getenv(name)
if val == "" {
slog.Error("missing environment variable", slog.String("name", name))
os.Exit(1)
}
return val
}

type room struct {
RoomName string `json:"room_name"`
Participants int `json:"participants"`
CreatedTime int64 `json:"created_time,omitempty"`
}

type roomList []room

func (l *roomList) UnmarshalJSON(data []byte) error {
// Attempt to unmarshal data as a array of rooms
err := json.Unmarshal(data, (*[]room)(l))
if err != nil {
// Check if the empty list is represented as `{}`
if emptyErr := json.Unmarshal(data, &struct{}{}); emptyErr == nil {
return nil
}
}
return err
}

func peephole(w http.ResponseWriter) error {
// Fetch "room census" from internal API
resp, err := http.Get(roomCensusURL)
if err != nil {
return fmt.Errorf("failed to fetch room census from %q: %w", roomCensusURL, err)
}
defer resp.Body.Close()

// Parse JSON response payload into roomList
var payload struct {
RoomCensus roomList `json:"room_census,omitempty"`
}
err = json.NewDecoder(resp.Body).Decode(&payload)
if err != nil {
return fmt.Errorf("failed to parse room census payload: %w", err)
}

// Extract configured room from room list. If no census for the configured
// room is found, we fall back on just displaying zero participants
var found = room{
RoomName: roomName,
Participants: 0,
}
for _, r := range payload.RoomCensus {
if r.RoomName == roomName {
found = r
break
}
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(found)
}

func main() {
slog.Info("starting HTTP server", "addr", httpAddr)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if err := peephole(w); err != nil {
slog.Error("failed to serve request", slog.Any("error", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
})
err := http.ListenAndServe(httpAddr, handler)
if !errors.Is(err, http.ErrServerClosed) {
slog.Error("listener failed", slog.Any("error", err))
os.Exit(1)
}
}

0 comments on commit 8e128b8

Please sign in to comment.