Skip to content

Commit d30835f

Browse files
authored
Moved all API endpoints to their namespace (#18)
1 parent c1dfe51 commit d30835f

File tree

5 files changed

+61
-49
lines changed

5 files changed

+61
-49
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gem 'puma', '~> 6.4', '>= 6.4.2'
1010
gem 'rake', '~> 13.2', '>= 13.2.1'
1111
gem 'rqrcode', '~> 2.0'
1212
gem 'sinatra', '~> 4.0'
13+
gem 'sinatra-contrib', '~> 4.0'
1314

1415
group :development do
1516
gem 'byebug', '~> 11.1', '>= 11.1.3'

Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ GEM
1818
json-schema (4.3.0)
1919
addressable (>= 2.8)
2020
language_server-protocol (3.17.0.3)
21+
multi_json (1.15.0)
2122
mustermann (3.0.0)
2223
ruby2_keywords (~> 0.0.1)
2324
nio4r (2.7.2)
@@ -95,6 +96,12 @@ GEM
9596
rack-protection (= 4.0.0)
9697
rack-session (>= 2.0.0, < 3)
9798
tilt (~> 2.0)
99+
sinatra-contrib (4.0.0)
100+
multi_json (>= 0.0.2)
101+
mustermann (~> 3.0)
102+
rack-protection (= 4.0.0)
103+
sinatra (= 4.0.0)
104+
tilt (~> 2.0)
98105
super_diff (0.12.1)
99106
attr_extras (>= 6.2.4)
100107
diff-lcs
@@ -122,6 +129,7 @@ DEPENDENCIES
122129
rubocop-rake (~> 0.6.0)
123130
rubocop-rspec (~> 2.29, >= 2.29.2)
124131
sinatra (~> 4.0)
132+
sinatra-contrib (~> 4.0)
125133
super_diff (~> 0.12.1)
126134

127135
BUNDLED WITH

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ By default, the application runs in single-threaded mode. This is not a solution
5454

5555
All requests are authorized using the bearer token that you specified in variable AUTH_TOKEN!
5656

57-
### GET /clients
57+
### GET /api/clients
5858

5959
Returns an array with all clients on the server
6060

@@ -79,7 +79,7 @@ Example response:
7979
]
8080
```
8181

82-
### POST /clients
82+
### POST /api/clients
8383

8484
Creates a new client. The response will be the new client created. You can pass your parameters in the request parameters. They will be in the data field.
8585

@@ -102,17 +102,17 @@ Example response:
102102
}
103103
```
104104

105-
### GET /clients/:id
105+
### GET /api/clients/:id
106106

107107
Returns a specific client by his ID. The answer will be similar to the previous one. If the client is not found, a 404 error will be returned. You can also request a QR code or a user-ready config in the form of text
108108

109-
`GET /clients/:id?format=qr`
109+
`GET /api/clients/:id?format=qr`
110110

111111
The QR code will be returned as a PNG image.
112112

113113
content_type => image/png
114114

115-
`GET /clients/:id?format=conf`
115+
`GET /api/clients/:id?format=conf`
116116

117117
A text with the config for the client will be returned. This config can already be written to a file and used in wireguard.
118118

@@ -134,11 +134,11 @@ Endpoint = endpoint
134134

135135
content_type => text/plain
136136

137-
### DELETE /clients/:id
137+
### DELETE /api/clients/:id
138138

139139
Deletes a specific one client. If the client is not found, a 404 error will be returned.
140140

141-
### PATCH /clients/:id
141+
### PATCH /api/clients/:id
142142

143143
Allows you to update specific clients by assigning them new fields. Returns the updated client in response.
144144

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.3.0
1+
v1.4.0

app.rb

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
11
# frozen_string_literal: true
22

33
require 'sinatra'
4+
require 'sinatra/contrib'
45

56
# Main app class
67
class Application < Sinatra::Base
7-
AUTH_TOKEN = "Bearer #{ENV.fetch('AUTH_TOKEN', nil)}".freeze
8-
9-
before do
10-
content_type :json
8+
register Sinatra::Namespace
119

12-
pass if request.path == '/healthz'
10+
AUTH_TOKEN = "Bearer #{ENV.fetch('AUTH_TOKEN', nil)}".freeze
1311

14-
halt 403 unless request.env['HTTP_AUTHORIZATION'] == AUTH_TOKEN
12+
namespace '/api' do # rubocop:disable Metrics/BlockLength
13+
before do
14+
content_type :json
1515

16-
@controller = ClientsController.new
17-
end
16+
halt 403 unless request.env['HTTP_AUTHORIZATION'] == AUTH_TOKEN
1817

19-
get '/clients' do
20-
controller.index
21-
end
22-
23-
get '/clients/:id' do
24-
config = controller.show(params['id'])
25-
case params['format']
26-
when 'qr'
27-
content_type 'image/png'
18+
@controller = ClientsController.new
19+
end
2820

29-
send_file Utils::QrCodeBuilder.build(config)
30-
when 'conf'
31-
content_type 'text/plain'
21+
get '/clients' do
22+
controller.index
23+
end
3224

33-
Utils::ConfigFileBuilder.build(config)
34-
else
35-
config
25+
get '/clients/:id' do
26+
config = controller.show(params['id'])
27+
case params['format']
28+
when 'qr'
29+
content_type 'image/png'
30+
31+
send_file Utils::QrCodeBuilder.build(config)
32+
when 'conf'
33+
content_type 'text/plain'
34+
35+
Utils::ConfigFileBuilder.build(config)
36+
else
37+
config
38+
end
39+
rescue Errors::ConfigNotFoundError
40+
halt 404
3641
end
37-
rescue Errors::ConfigNotFoundError
38-
halt 404
39-
end
4042

41-
delete '/clients/:id' do
42-
controller.destroy(params['id'])
43-
rescue Errors::ConfigNotFoundError
44-
halt 404
45-
end
43+
delete '/clients/:id' do
44+
controller.destroy(params['id'])
45+
rescue Errors::ConfigNotFoundError
46+
halt 404
47+
end
4648

47-
patch '/clients/:id' do
48-
controller.update(params['id'], request_body)
49-
rescue Errors::ConfigNotFoundError
50-
halt 404
51-
rescue JSON::Schema::ValidationError => e
52-
halt 400, { error: e }.to_json
53-
end
49+
patch '/clients/:id' do
50+
controller.update(params['id'], request_body)
51+
rescue Errors::ConfigNotFoundError
52+
halt 404
53+
rescue JSON::Schema::ValidationError => e
54+
halt 400, { error: e }.to_json
55+
end
5456

55-
post '/clients' do
56-
status 201
57-
controller.create(params)
57+
post '/clients' do
58+
status 201
59+
controller.create(params)
60+
end
5861
end
5962

6063
get '/healthz' do

0 commit comments

Comments
 (0)