Skip to content

Commit

Permalink
Refactor routes and controllers to better use HTTP verbs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raekye committed Jul 27, 2023
1 parent 0912716 commit f35dfcb
Show file tree
Hide file tree
Showing 67 changed files with 875 additions and 1,057 deletions.
14 changes: 11 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ Layout/EmptyLinesAroundAccessModifier:
Bundler/OrderedGems:
Enabled: false
Metrics/BlockLength:
Max: 200
Enabled: false
Metrics/MethodLength:
Max: 100
Enabled: false
Metrics/ClassLength:
Max: 200
Enabled: false
Style/Documentation:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Metrics/AbcSize:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Style/NumericPredicate:
Enabled: false
2 changes: 2 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ WORKDIR /gamocosm
ENV RAILS_ENV=production
ENV RAILS_LOG_TO_STDOUT=1

RUN bundle config set --local without development

COPY Gemfile Gemfile.lock ./

RUN bundle install
Expand Down
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ gem 'font-awesome-sass', '~> 6.4.0'
group :development do
# https://guides.rubyonrails.org/configuring.html#config-file-watcher
gem 'listen'

gem 'annotate', require: false
end
group :test do
gem 'simplecov'
Expand Down
6 changes: 1 addition & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
async (2.6.2)
console (~> 1.10)
fiber-annotation
Expand Down Expand Up @@ -168,7 +165,7 @@ GEM
importmap-rails (1.2.1)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
io-event (1.2.2)
io-event (1.2.3)
kartograph (0.2.8)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
Expand Down Expand Up @@ -314,7 +311,6 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
annotate
bcrypt_pbkdf
bootstrap-sass (~> 3.4.1)
connection_pool
Expand Down
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,38 @@ I recommend using [rbenv][rbenv] to manage Ruby installations.
- Run `git clone https://github.com/rbenv/rbenv.git ~/.rbenv`.
- Add `$HOME/.rbenv/bin` to your `$PATH`, usually done in `~/.bashrc`.

On recent versions of Fedora, `~/.bashrc` sources any files in the directory `~/.bashrc.d` (if it exists), so you don't have to edit `.bashrc` directly.
(To create the directory, run `mkdir ~/.bashrc.d`.)
For example, run ` echo 'PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc.d/rbenv` (you can replace `~/.bashrc.d/rbenv` with `~/.bashrc` to modify `.bashrc` directly).
- Additionally, add `eval "$(rbenv init - bash)"` to your shell:
`echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc.d/rbenv` (again, you may choose to modify `.bashrc` directly).
On recent versions of Fedora, `~/.bashrc` sources any files in the directory `~/.bashrc.d` (if it exists),
which is more modular than editing `.bashrc` directly.
It would look something like the following in your `~/.bashrc`.

```
# User specific aliases and functions
if [ -d ~/.bashrc.d ]; then
for rc in ~/.bashrc.d/*; do
if [ -f "$rc" ]; then
. "$rc"
fi
done
fi

unset rc
```

Assuming you have/use this setup, to proceed with the rbenv installation:

- Create the directory (without failing if it already exists):
`mkdir -p ~/.bashrc.d`.
- Create a configuration/initialization file:
`echo 'PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc.d/rbenv`.
- Additionally, ensure that rbenv gets autoloaded into any new shells:
`echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc.d/rbenv` (assuming you're following the modular convention).
- Restart (close and reopen) your shell for the changes to take effect.
- Create the plugins directory for rbenv:
`mkdir ~/.rbenv/plugins`.
- Get ruby-build:
`git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build`.
- Check that ruby-build has been installed correctly:
`rbenv install --list`.
`rbenv install --list` (lists stable ruby versions).
1. (directory sensitive) Install Ruby 3.1.2:
`rbenv install` (it reads `.ruby-version`).
1. (directory sensitive) Check that `ruby -v` gives you version 3.1.2.
Expand Down
84 changes: 84 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
class APIController < ApplicationController
#skip_before_action :verify_authenticity_token
before_action :find_server
rescue_from ActiveRecord::RecordNotFound, with: :not_found

def status
active = server.running?
status = server.pending_operation
minecraft = server.minecraft.running?
ip = server.remote.exists? ? server.remote.ip_address : nil
download = server.minecraft.world_download_url
render json: {
server: active,
status:,
minecraft:,
ip:,
domain: server.host_name,
download:,
}
end

def start
err = @server.start
@server.user.invalidate_digital_ocean_cache_droplets
@server.user.invalidate_digital_ocean_cache_volumes
@server.user.invalidate_digital_ocean_cache_snapshots
render json: {
error: err,
}
end

def stop
err = @server.stop
render json: {
error: err,
}
end

def reboot
err = @server.reboot
render json: {
error: err,
}
end

def pause
err = @server.minecraft.pause
render json: {
error: err,
}
end

def resume
err = @server.minecraft.resume
render json: {
error: err,
}
end

def backup
err = @server.minecraft.backup
render json: {
error: err,
}
end

def exec
err = @server.minecraft.exec(@server.user, params[:command])
render json: {
error: err,
}
end

private
def find_server
@server = Server.find_by(id: params[:id], api_key: params[:key])
end

def not_found
render json: {
error: 'Not found',
}, status: 404
end
end
17 changes: 17 additions & 0 deletions app/controllers/digital_ocean/droplets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class DigitalOcean::DropletsController < ApplicationController
before_action :authenticate_user!
layout false

def index
@do_droplets = current_user.digital_ocean_droplets
end

def destroy
error = current_user.digital_ocean.droplet_delete(params[:id])
current_user.invalidate_digital_ocean_cache_droplets
if error
return redirect_to servers_path, flash: { error: }
end
redirect_to servers_path, flash: { notice: 'Deleted droplet on Digital Ocean' }
end
end
17 changes: 17 additions & 0 deletions app/controllers/digital_ocean/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class DigitalOcean::ImagesController < ApplicationController
before_action :authenticate_user!
layout false

def index
@do_images = current_user.digital_ocean_images
end

def destroy
error = current_user.digital_ocean.image_delete(params[:id])
current_user.invalidate_digital_ocean_cache_images
if error
return redirect_to servers_path, flash: { error: }
end
redirect_to servers_path, flash: { notice: 'Deleted snapshot on Digital Ocean' }
end
end
17 changes: 17 additions & 0 deletions app/controllers/digital_ocean/snapshots_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class DigitalOcean::SnapshotsController < ApplicationController
before_action :authenticate_user!
layout false

def index
@do_snapshots = current_user.digital_ocean_snapshots
end

def destroy
error = current_user.digital_ocean.snapshot_delete(params[:id])
current_user.invalidate_digital_ocean_cache_snapshots
if error
return redirect_to volumes_path, flash: { error: }
end
redirect_to volumes_path, flash: { notice: 'Deleted snapshot on Digital Ocean' }
end
end
29 changes: 29 additions & 0 deletions app/controllers/digital_ocean/ssh_keys_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class DigitalOcean::SshKeysController < ApplicationController
before_action :authenticate_user!
layout false

def index
@do_ssh_keys = current_user.digital_ocean_ssh_keys
end

def create
ssh_key_name = params[:digital_ocean_ssh_key][:name]
ssh_public_key = params[:digital_ocean_ssh_key][:data]
f = { success: 'Added SSH public key to Digital Ocean' }
ssh_key = current_user.digital_ocean.ssh_key_create(ssh_key_name, ssh_public_key)
if ssh_key.error?
f = { error: ssh_key }
end
redirect_back fallback_location: servers_path, flash: f
end

def destroy
error = current_user.digital_ocean.ssh_key_delete(params[:id])
current_user.invalidate_digital_ocean_cache_ssh_keys
f = { success: 'Deleted SSH public key from Digital Ocean' }
if error
f = { error: }
end
redirect_back fallback_location: servers_path, flash: f
end
end
17 changes: 17 additions & 0 deletions app/controllers/digital_ocean/volumes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class DigitalOcean::VolumesController < ApplicationController
before_action :authenticate_user!
layout false

def index
@do_volumes = current_user.digital_ocean_volumes
end

def destroy
error = current_user.digital_ocean.volume_delete(params[:id])
current_user.invalidate_digital_ocean_cache_volumes
if error
return redirect_to volumes_path, flash: { error: }
end
redirect_to volumes_path, flash: { notice: 'Deleted volume on Digital Ocean' }
end
end
31 changes: 0 additions & 31 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,6 @@ def about

def tos; end

def digital_ocean_setup; end

def demo
@server = Mock::Mocker.new.mock_server(Server.new)
@server.domain = 'abcdefgh'
@server.remote_region_slug = 'nyc3'
@server.remote_size_slug = '1gb'
@server.setup_stage = 5
@server.remote_id = 1
@server.timezone_delta = 0
@server.scheduled_tasks = ScheduledTask.parse([
'Wednesday 8:00 pm start',
'Friday 3:30 pm start',
'Sunday 11:00 am start',
].join("\n"), @server)

minecraft = Mock::Minecraft.new
minecraft.server = @server
minecraft.autoshutdown_enabled = true
minecraft.autoshutdown_last_check = Time.now - 32.seconds
minecraft.autoshutdown_last_successful = Time.now - 32.seconds
minecraft.autoshutdown_minutes = 8
@server.minecraft = minecraft

user = User.new
user.digital_ocean_api_key = 'abc'
@server.user = user

@demo = true
end

def not_found
render status: 404
end
Expand Down

0 comments on commit f35dfcb

Please sign in to comment.