Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved all API endpoints to their namespace #18

Merged
merged 2 commits into from
May 24, 2024
Merged
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem 'puma', '~> 6.4', '>= 6.4.2'
gem 'rake', '~> 13.2', '>= 13.2.1'
gem 'rqrcode', '~> 2.0'
gem 'sinatra', '~> 4.0'
gem 'sinatra-contrib', '~> 4.0'

group :development do
gem 'byebug', '~> 11.1', '>= 11.1.3'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ GEM
json-schema (4.3.0)
addressable (>= 2.8)
language_server-protocol (3.17.0.3)
multi_json (1.15.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
nio4r (2.7.2)
Expand Down Expand Up @@ -95,6 +96,12 @@ GEM
rack-protection (= 4.0.0)
rack-session (>= 2.0.0, < 3)
tilt (~> 2.0)
sinatra-contrib (4.0.0)
multi_json (>= 0.0.2)
mustermann (~> 3.0)
rack-protection (= 4.0.0)
sinatra (= 4.0.0)
tilt (~> 2.0)
super_diff (0.12.1)
attr_extras (>= 6.2.4)
diff-lcs
Expand Down Expand Up @@ -122,6 +129,7 @@ DEPENDENCIES
rubocop-rake (~> 0.6.0)
rubocop-rspec (~> 2.29, >= 2.29.2)
sinatra (~> 4.0)
sinatra-contrib (~> 4.0)
super_diff (~> 0.12.1)

BUNDLED WITH
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ By default, the application runs in single-threaded mode. This is not a solution

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

### GET /clients
### GET /api/clients

Returns an array with all clients on the server

Expand All @@ -79,7 +79,7 @@ Example response:
]
```

### POST /clients
### POST /api/clients

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.

Expand All @@ -102,17 +102,17 @@ Example response:
}
```

### GET /clients/:id
### GET /api/clients/:id

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

`GET /clients/:id?format=qr`
`GET /api/clients/:id?format=qr`

The QR code will be returned as a PNG image.

content_type => image/png

`GET /clients/:id?format=conf`
`GET /api/clients/:id?format=conf`

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

Expand All @@ -134,11 +134,11 @@ Endpoint = endpoint

content_type => text/plain

### DELETE /clients/:id
### DELETE /api/clients/:id

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

### PATCH /clients/:id
### PATCH /api/clients/:id

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

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.3.0
v1.4.0
85 changes: 44 additions & 41 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
# frozen_string_literal: true

require 'sinatra'
require 'sinatra/contrib'

# Main app class
class Application < Sinatra::Base
AUTH_TOKEN = "Bearer #{ENV.fetch('AUTH_TOKEN', nil)}".freeze

before do
content_type :json
register Sinatra::Namespace

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

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

@controller = ClientsController.new
end
halt 403 unless request.env['HTTP_AUTHORIZATION'] == AUTH_TOKEN

get '/clients' do
controller.index
end

get '/clients/:id' do
config = controller.show(params['id'])
case params['format']
when 'qr'
content_type 'image/png'
@controller = ClientsController.new
end

send_file Utils::QrCodeBuilder.build(config)
when 'conf'
content_type 'text/plain'
get '/clients' do
controller.index
end

Utils::ConfigFileBuilder.build(config)
else
config
get '/clients/:id' do
config = controller.show(params['id'])
case params['format']
when 'qr'
content_type 'image/png'

send_file Utils::QrCodeBuilder.build(config)
when 'conf'
content_type 'text/plain'

Utils::ConfigFileBuilder.build(config)
else
config
end
rescue Errors::ConfigNotFoundError
halt 404
end
rescue Errors::ConfigNotFoundError
halt 404
end

delete '/clients/:id' do
controller.destroy(params['id'])
rescue Errors::ConfigNotFoundError
halt 404
end
delete '/clients/:id' do
controller.destroy(params['id'])
rescue Errors::ConfigNotFoundError
halt 404
end

patch '/clients/:id' do
controller.update(params['id'], request_body)
rescue Errors::ConfigNotFoundError
halt 404
rescue JSON::Schema::ValidationError => e
halt 400, { error: e }.to_json
end
patch '/clients/:id' do
controller.update(params['id'], request_body)
rescue Errors::ConfigNotFoundError
halt 404
rescue JSON::Schema::ValidationError => e
halt 400, { error: e }.to_json
end

post '/clients' do
status 201
controller.create(params)
post '/clients' do
status 201
controller.create(params)
end
end

get '/healthz' do
Expand Down
Loading