Skip to content

Commit

Permalink
[api] add device self-info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Oct 16, 2024
1 parent 7dd333e commit 67e5d13
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 62 deletions.
5 changes: 4 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ deployments/
build/

# Example requests files
*.http
*.http

# Ignore Go workspace
*.work
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,macos,linux,go,terraform
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,macos,linux,go,terraform
# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,terraform,macos,linux,go,dotenv
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,terraform,macos,linux,go,dotenv

### dotenv ###
.env

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
Expand Down Expand Up @@ -47,7 +50,8 @@ go.work
.LSOverride

# Icon must end with two \r
Icon
Icon


# Thumbnails
._*
Expand Down Expand Up @@ -153,7 +157,7 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,macos,linux,go,terraform
# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,terraform,macos,linux,go,dotenv

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)

Expand All @@ -166,4 +170,4 @@ $RECYCLE.BIN/

/api/docs.go

.env
go.work*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ init:
go mod download

init-dev: init
go install github.com/cosmtrek/air@latest \
go install github.com/air-verse/air@latest \
&& go install github.com/swaggo/swag/cmd/swag@latest \
&& go install github.com/pressly/goose/v3/cmd/goose@latest

Expand Down
4 changes: 4 additions & 0 deletions api/mobile.http
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@mobileToken={{$dotenv MOBILE__TOKEN}}
@phone={{$dotenv PHONE}}

###
GET {{baseUrl}}/device HTTP/1.1
Authorization: Bearer {{mobileToken}}1

###
POST {{baseUrl}}/device HTTP/1.1
Authorization: Bearer 123456789
Expand Down
57 changes: 51 additions & 6 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,30 @@
}
},
"/mobile/v1/device": {
"get": {
"description": "Returns device information",
"produces": [
"application/json"
],
"tags": [
"Device"
],
"summary": "Get device information",
"responses": {
"200": {
"description": "Device information",
"schema": {
"$ref": "#/definitions/smsgateway.MobileDeviceResponse"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/smsgateway.ErrorResponse"
}
}
}
},
"post": {
"description": "Registers new device and returns credentials",
"consumes": [
Expand Down Expand Up @@ -925,7 +949,7 @@
]
},
"simNumber": {
"description": "SIM card number (1-3)",
"description": "SIM card number (1-3), if not set - default SIM will be used",
"type": "integer",
"maximum": 3,
"example": 1
Expand Down Expand Up @@ -997,6 +1021,23 @@
}
}
},
"smsgateway.MobileDeviceResponse": {
"type": "object",
"properties": {
"device": {
"description": "Device information, empty if device is not registered on the server",
"allOf": [
{
"$ref": "#/definitions/smsgateway.Device"
}
]
},
"externalIp": {
"description": "External IP",
"type": "string"
}
}
},
"smsgateway.MobileRegisterRequest": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1165,7 +1206,7 @@
"description": "The type of event the webhook is triggered for.",
"allOf": [
{
"$ref": "#/definitions/webhooks.EventType"
"$ref": "#/definitions/smsgateway.WebhookEvent"
}
],
"example": "sms:received"
Expand All @@ -1183,17 +1224,21 @@
}
}
},
"webhooks.EventType": {
"smsgateway.WebhookEvent": {
"type": "string",
"enum": [
"sms:received",
"sms:sent",
"sms:delivered",
"sms:failed",
"system:ping"
],
"x-enum-varnames": [
"EventTypeSmsReceived",
"EventTypeSmsSent",
"EventTypeSystemPing"
"WebhookEventSmsReceived",
"WebhookEventSmsSent",
"WebhookEventSmsDelivered",
"WebhookEventSmsFailed",
"WebhookEventSystemPing"
]
}
},
Expand Down
43 changes: 37 additions & 6 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ definitions:
minItems: 1
type: array
simNumber:
description: SIM card number (1-3)
description: SIM card number (1-3), if not set - default SIM will be used
example: 1
maximum: 3
type: integer
Expand Down Expand Up @@ -211,6 +211,17 @@ definitions:
- recipients
- state
type: object
smsgateway.MobileDeviceResponse:
properties:
device:
allOf:
- $ref: '#/definitions/smsgateway.Device'
description: Device information, empty if device is not registered on the
server
externalIp:
description: External IP
type: string
type: object
smsgateway.MobileRegisterRequest:
properties:
name:
Expand Down Expand Up @@ -331,7 +342,7 @@ definitions:
properties:
event:
allOf:
- $ref: '#/definitions/webhooks.EventType'
- $ref: '#/definitions/smsgateway.WebhookEvent'
description: The type of event the webhook is triggered for.
example: sms:received
id:
Expand All @@ -347,16 +358,20 @@ definitions:
- event
- url
type: object
webhooks.EventType:
smsgateway.WebhookEvent:
enum:
- sms:received
- sms:sent
- sms:delivered
- sms:failed
- system:ping
type: string
x-enum-varnames:
- EventTypeSmsReceived
- EventTypeSmsSent
- EventTypeSystemPing
- WebhookEventSmsReceived
- WebhookEventSmsSent
- WebhookEventSmsDelivered
- WebhookEventSmsFailed
- WebhookEventSystemPing
host: api.sms-gate.app
info:
contact:
Expand Down Expand Up @@ -635,6 +650,22 @@ paths:
- User
- Webhooks
/mobile/v1/device:
get:
description: Returns device information
produces:
- application/json
responses:
"200":
description: Device information
schema:
$ref: '#/definitions/smsgateway.MobileDeviceResponse'
"500":
description: Internal server error
schema:
$ref: '#/definitions/smsgateway.ErrorResponse'
summary: Get device information
tags:
- Device
patch:
consumes:
- application/json
Expand Down
16 changes: 5 additions & 11 deletions deployments/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
condition: service_healthy

db:
image: mariadb:10.11
image: mariadb:11.4
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=sms
Expand All @@ -37,17 +37,11 @@ services:
- mariadb-data:/var/lib/mysql
restart: 'unless-stopped'
healthcheck:
test:
[
"CMD",
"mysqladmin",
"ping",
"-proot",
"-h",
"127.0.0.1"
]
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
start_period: 10s
interval: 10s
timeout: 5s
retries: 10
retries: 3

volumes:
mariadb-data:
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ go 1.22.0

require (
firebase.google.com/go/v4 v4.12.1
github.com/android-sms-gateway/client-go v1.0.5
github.com/android-sms-gateway/client-go v1.1.0
github.com/ansrivas/fiberprometheus/v2 v2.6.1
github.com/capcom6/go-helpers v0.0.0-20240521035631-865ee2879fa3
github.com/capcom6/go-infra-fx v0.0.2
github.com/go-playground/assert/v2 v2.2.0
github.com/go-playground/validator/v10 v10.16.0
github.com/go-sql-driver/mysql v1.7.1
github.com/gofiber/fiber/v2 v2.52.5
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/android-sms-gateway/client-go v1.0.4 h1:QZ72TRBJKm11WL/jim+ba7m2J5RLBaICMcy7f/RVfuQ=
github.com/android-sms-gateway/client-go v1.0.4/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
github.com/android-sms-gateway/client-go v1.0.5 h1:0jUnRJHk1VqV5K8kla1d8rOO/lXshHG5uChxj7YTzWE=
github.com/android-sms-gateway/client-go v1.0.5/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
github.com/android-sms-gateway/client-go v1.1.0 h1:mAPsueRrY/qOdQAU5yO3uLQAb7Po+3jBxB1tiqyClvg=
github.com/android-sms-gateway/client-go v1.1.0/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/ansrivas/fiberprometheus/v2 v2.6.1 h1:wac3pXaE6BYYTF04AC6K0ktk6vCD+MnDOJZ3SK66kXM=
Expand Down Expand Up @@ -193,8 +191,6 @@ github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nyaruka/phonenumbers v1.3.0 h1:IFyyJfF2Elg8xGKFghWrRXzb6qAHk+Q3uPqmIgS20JQ=
github.com/nyaruka/phonenumbers v1.3.0/go.mod h1:4jyKp/BFUokLbCHyoZag+T3S1KezFVoEKtgnbpzItC4=
github.com/nyaruka/phonenumbers v1.4.0 h1:ddhWiHnHCIX3n6ETDA58Zq5dkxkjlvgrDWM2OHHPCzU=
github.com/nyaruka/phonenumbers v1.4.0/go.mod h1:gv+CtldaFz+G3vHHnasBSirAi3O2XLqZzVWz4V1pl2E=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down Expand Up @@ -305,8 +301,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4=
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
21 changes: 21 additions & 0 deletions internal/sms-gateway/handlers/converters/devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package converters

import (
"github.com/android-sms-gateway/client-go/smsgateway"
"github.com/capcom6/sms-gateway/internal/sms-gateway/models"
)

func DeviceToDTO(device *models.Device) *smsgateway.Device {
if device.IsEmpty() {
return nil
}

return &smsgateway.Device{
ID: device.ID,
Name: *device.Name,
CreatedAt: device.CreatedAt,
UpdatedAt: device.UpdatedAt,
DeletedAt: device.DeletedAt,
LastSeen: device.LastSeen,
}
}
Loading

0 comments on commit 67e5d13

Please sign in to comment.