Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SPDX-FileCopyrightText: 2023 OPAL-RT Germany GmbH
# SPDX-License-Identifier: Apache-2.0

export_or_unset()
{
local var=$1

if [ -z "${!var+x}" ]; then
return
fi

if [ -n "$2" ]; then
export $var="$2"
else
unset $var
fi

}

if direnv_version "2.30.0" \
&& has nix \
&& nix show-config experimental-features 2>/dev/null | grep -wqF flakes
then
local oldtmp="$TMP"
local oldtemp="$TEMP"
local oldtmpdir="$TMPDIR"
local oldtempdir="$TEMPDIR"

watch_file ./nix/*.nix
use flake .

export_or_unset TMP "$oldtmp"
export_or_unset TEMP "$oldtemp"
export_or_unset TMPDIR "$oldtmpdir"
export_or_unset TEMPDIR "$oldtempdir"
fi
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-FileCopyrightText: 2023 OPAL-RT Germany GmbH
# SPDX-License-Identifier: Apache-2.0

/server
/server.exe
/turn-api-auth
/turn-api-auth.exe

.direnv
11 changes: 11 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ variables:

stages:

- test
- packaging
- latest

test:reuse:
stage: test
image:
name: fsfe/reuse:latest
entrypoint: [""]
script:
- reuse lint
tags:
- docker

pkg:docker:
stage: packaging
image: docker:20.10
Expand Down
8 changes: 8 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: VILLASnode WebRTC Signaling Server
Upstream-Contact: Steffen Vogel <post@steffenvogel.de>
Source: https://fein-aachen.org/en/projects/villas-node/

Files: flake.lock
Copyright: 2023 OPAL-RT Germany GmbH
License: Apache-2.0
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
// SPDX-FileCopyrightText: 2023 OPAL-RT Germany GmbH
// SPDX-License-Identifier: Apache-2.0

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2023 Institute for Automation of Complex Power Systems
# SPDX-License-Identifier: Apache-2.0

FROM golang:1.20-alpine AS builder
FROM golang:1.22-alpine AS builder

WORKDIR /app

Expand Down
3 changes: 2 additions & 1 deletion cmd/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package main
import (
"errors"
"fmt"
"log/slog"
"net/http"

"github.com/VILLASframework/signaling/pkg"
"github.com/gorilla/mux"
"golang.org/x/exp/slog"
)

type apiErrorResponse struct {
Expand Down Expand Up @@ -116,6 +116,7 @@ func handleAPIPeer(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
req := &apiPeerRequest{}
slog.Info("Reading request body", slog.Any("req", req))
if !readJSON(w, r, req) {
return
}
Expand Down
16 changes: 9 additions & 7 deletions cmd/server/api_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package main
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"

"golang.org/x/exp/slog"
)

func writeJSON(w http.ResponseWriter, resp any) bool {
Expand All @@ -25,11 +24,12 @@ func writeJSON(w http.ResponseWriter, resp any) bool {
}

func readJSON(w http.ResponseWriter, r *http.Request, req any) bool {
slog.Info("Reading request body")
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
writeError(w, http.StatusBadRequest, fmt.Errorf("failed to parse request body: %w", err))
return false
}

slog.Info("Read request body", slog.Any("req", req))
return true
}

Expand All @@ -38,14 +38,16 @@ func writeError(w http.ResponseWriter, code int, err error) bool {
Error: err.Error(),
Status: http.StatusText(code),
}
slog.Info("Writing error response", slog.Any("resp", resp))

slog.Error("Request failed", slog.Any("error", err))

if writeJSON(w, resp) {
w.WriteHeader(code)
// if writeJSON(w, resp) {
// slog.Info("wrote json")
// w.WriteHeader(code)

return true
}
// return true
// }

return false
}
4 changes: 2 additions & 2 deletions cmd/server/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package main
import (
"errors"
"fmt"
"log/slog"
"net/http"
"time"

"github.com/VILLASframework/signaling/pkg"
"github.com/gorilla/websocket"
"golang.org/x/exp/slog"
)

type Connection struct {
Expand Down Expand Up @@ -225,7 +225,7 @@ func (c *Connection) closed() {
c.logger.Error("Failed to send control message", slog.Any("error", err))
}

// Remove peer if it does not have any signal metadata associated
// Remove peer if it does not have any signal metadata associated.
if c.peer.signals == nil {
if err := c.peer.session.RemovePeer(c.peer); err != nil {
c.logger.Error("Failed to close peer", slog.Any("error", err))
Expand Down
53 changes: 27 additions & 26 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
Expand All @@ -18,7 +19,6 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/exp/slog"
)

type relayInfos []pkg.RelayInfo
Expand Down Expand Up @@ -74,15 +74,35 @@ func main() {

r := mux.NewRouter()

r.Use(
a := r.PathPrefix("/api/v1").Subrouter()

a.Use(
func(next http.Handler) http.Handler {
return promhttp.InstrumentHandlerCounter(metricHttpRequestsTotal, next)
},
func(next http.Handler) http.Handler {
return promhttp.InstrumentHandlerDuration(metricHttpRequestDuration, next)
},
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
},
)

a.Path("/sessions").
Methods("GET").
HandlerFunc(basicAuth(handleAPISessions))

a.Path("/session/{session}").
Methods("GET").
HandlerFunc(handleAPISession)

a.Path("/peer/{session}/{peer}").
Methods("GET", "POST", "DELETE").
HandlerFunc(handleAPIPeer)

r.Path("/metrics").
Methods("GET").
Handler(promhttp.Handler())
Expand All @@ -105,31 +125,12 @@ func main() {
r.Path("/{session}/{peer}").
HandlerFunc(handleWebsocket)

a := r.PathPrefix("/api/v1").Subrouter()

a.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
})

a.Path("/sessions").
Methods("GET").
HandlerFunc(basicAuth(handleAPISessions))

a.Path("/session/{session}").
Methods("GET").
HandlerFunc(handleAPISession)

a.Path("/peer/{session}/{peer}").
Methods("GET", "POST", "DELETE").
HandlerFunc(handleAPIPeer)

r.PathPrefix("/").
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, fmt.Errorf("invalid request"))
})
HandlerFunc(handleWebsocket)
// HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// slog.Info("Invalid request", slog.Any("path", r.URL.Path))
// writeError(w, http.StatusBadRequest, fmt.Errorf("invalid request"))
// })

expiryTicker := time.NewTicker(10 * time.Second)

Expand Down
2 changes: 1 addition & 1 deletion cmd/server/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package main

import (
"log/slog"
"sync"
"time"

"github.com/VILLASframework/signaling/pkg"
"golang.org/x/exp/slog"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package main

import (
"fmt"
"log/slog"
"sync"
"sync/atomic"
"time"

"github.com/VILLASframework/signaling/pkg"
"golang.org/x/exp/slog"
)

const sessionExpiryAge = time.Hour
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func handleWebsocket(w http.ResponseWriter, r *http.Request) {
}

if err := peer.Connect(w, r); err != nil {
writeError(w, http.StatusBadRequest, fmt.Errorf("failed to connect: %w", err))
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to connect peer: %w", err))
return
}
}
27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading