Skip to content

Commit

Permalink
feat(endpoint): add extensions to new endpoints
Browse files Browse the repository at this point in the history
Extensions may now be assigned to a given endpoint during its creation.
They are also returned when listing endpoints.
  • Loading branch information
crazybolillo committed Aug 4, 2024
1 parent 4a97380 commit 9fad17e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
12 changes: 12 additions & 0 deletions db/migrations/20240730051335_ery_extension.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- migrate:up
ALTER TABLE ps_endpoints ADD COLUMN sid SERIAL PRIMARY KEY;

CREATE TABLE ery_extension (
id SERIAL PRIMARY KEY,
endpoint_id SERIAL NOT NULL,
extension varchar UNIQUE,
FOREIGN KEY (endpoint_id) REFERENCES ps_endpoints(sid)
)

-- migrate:down

17 changes: 12 additions & 5 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ definitions:
type: array
context:
type: string
extension:
type: string
id:
type: string
max_contacts:
Expand All @@ -18,13 +20,20 @@ definitions:
transport:
type: string
type: object
handler.listEndpointsRequest:
properties:
endpoints:
items:
$ref: '#/definitions/sqlc.ListEndpointsRow'
type: array
type: object
sqlc.ListEndpointsRow:
properties:
context:
type: string
id:
extension:
type: string
transport:
id:
type: string
type: object
host: localhost:8080
Expand Down Expand Up @@ -87,9 +96,7 @@ paths:
"200":
description: OK
schema:
items:
$ref: '#/definitions/sqlc.ListEndpointsRow'
type: array
$ref: '#/definitions/handler.listEndpointsRequest'
"400":
description: Bad Request
"500":
Expand Down
31 changes: 27 additions & 4 deletions internal/handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ type createEndpointRequest struct {
ID string `json:"id"`
Password string `json:"password"`
Realm string `json:"realm,omitempty"`
Transport string `json:"transport"`
Transport string `json:"transport,omitempty"`
Context string `json:"context"`
Codecs []string `json:"codecs"`
MaxContacts int32 `json:"max_contacts,omitempty"`
Extension string `json:"extension,omitempty"`
}

type listEndpointsRequest struct {
Endpoints []sqlc.ListEndpointsRow `json:"endpoints"`
}

func (e *Endpoint) Router() chi.Router {
Expand All @@ -40,7 +45,7 @@ func (e *Endpoint) Router() chi.Router {
// @Summary List existing endpoints.
// @Param limit query int false "Limit the amount of endpoints returned" default(15)
// @Produce json
// @Success 200 {object} []sqlc.ListEndpointsRow
// @Success 200 {object} listEndpointsRequest
// @Failure 400
// @Failure 500
// @Tags endpoints
Expand All @@ -60,11 +65,18 @@ func (e *Endpoint) list(w http.ResponseWriter, r *http.Request) {
queries := sqlc.New(e.Conn)
endpoints, err := queries.ListEndpoints(r.Context(), int32(limit))
if err != nil {
slog.Error("Query execution failed", slog.String("path", r.URL.Path), slog.String("msg", err.Error()))
w.WriteHeader(http.StatusInternalServerError)
return
}
if endpoints == nil {
endpoints = []sqlc.ListEndpointsRow{}
}

content, err := json.Marshal(endpoints)
response := listEndpointsRequest{
Endpoints: endpoints,
}
content, err := json.Marshal(response)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down Expand Up @@ -120,7 +132,7 @@ func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) {
return
}

err = queries.NewEndpoint(r.Context(), sqlc.NewEndpointParams{
sid, err := queries.NewEndpoint(r.Context(), sqlc.NewEndpointParams{
ID: payload.ID,
Transport: db.Text(payload.Transport),
Context: db.Text(payload.Context),
Expand All @@ -140,6 +152,17 @@ func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) {
return
}

if payload.Extension != "" {
err = queries.NewExtension(r.Context(), sqlc.NewExtensionParams{
EndpointID: sid,
Extension: db.Text(payload.Extension),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}

err = tx.Commit(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
7 changes: 7 additions & 0 deletions internal/sqlc/models.go

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

26 changes: 23 additions & 3 deletions internal/sqlc/queries.sql.go

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

11 changes: 9 additions & 2 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ INSERT INTO ps_aors
VALUES
($1, $2);

-- name: NewEndpoint :exec
-- name: NewEndpoint :one
INSERT INTO ps_endpoints
(id, transport, aors, auth, context, disallow, allow)
VALUES
($1, $2, $1, $1, $3, 'all', $4);
($1, $2, $1, $1, $3, 'all', $4)
RETURNING sid;

-- name: DeleteEndpoint :exec
DELETE FROM ps_endpoints WHERE id = $1;
Expand All @@ -31,3 +32,9 @@ SELECT
FROM
ps_endpoints
LIMIT $1;

-- name: NewExtension :exec
INSERT INTO ery_extension
(endpoint_id, extension)
VALUES
($1, $2);

0 comments on commit 9fad17e

Please sign in to comment.