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.
  • Loading branch information
crazybolillo committed Jul 31, 2024
1 parent 360784b commit 1227ce7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 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

2 changes: 2 additions & 0 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 Down
15 changes: 13 additions & 2 deletions internal/handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ 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"`
}

func (e *Endpoint) Router() chi.Router {
Expand Down Expand Up @@ -63,6 +64,9 @@ func (e *Endpoint) list(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
return
}
if endpoints == nil {
endpoints = []sqlc.ListEndpointsRow{}
}

content, err := json.Marshal(endpoints)
if err != nil {
Expand Down Expand Up @@ -120,7 +124,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 +144,13 @@ 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),
})
}

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 1227ce7

Please sign in to comment.