File tree Expand file tree Collapse file tree 8 files changed +18
-14
lines changed Expand file tree Collapse file tree 8 files changed +18
-14
lines changed Original file line number Diff line number Diff line change @@ -4,17 +4,17 @@ require "kemal"
4
4
post " /upload" do |env |
5
5
# Get the uploaded file from the "image" field in the form
6
6
# The file is initially stored in a temporary location
7
- file = env.params.files[" image" ].tempfile
7
+ uploaded_file = env.params.files[" image" ].tempfile
8
8
9
9
# Construct the destination path where we'll save the file
10
10
# - Kemal.config.public_folder is the configured public directory
11
11
# - "uploads/" is the subdirectory where we'll store uploads
12
12
# - File.basename gets just the filename from the temp file path
13
- file_path = ::File .join [Kemal .config.public_folder, " uploads/" , File .basename(file .path)]
13
+ uploaded_file_path = ::File .join [Kemal .config.public_folder, " uploads/" , File .basename(uploaded_file .path)]
14
14
15
15
# Open the destination file for writing and copy the uploaded file to it
16
- File .open(file_path , " w" ) do |f |
17
- IO .copy(file, f )
16
+ File .open(uploaded_file_path , " w" ) do |file |
17
+ IO .copy(uploaded_file, file )
18
18
end
19
19
20
20
# Return a simple success message
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ require "kemal-basic-auth"
8
8
basic_auth " username" , " password"
9
9
10
10
# Define a route for the root path "/"
11
- get " /" do |env |
11
+ get " /" do |_ |
12
12
# This route will only execute if authentication is successful
13
13
# Otherwise, the browser will show a login prompt
14
14
" This is shown if basic auth successful."
Original file line number Diff line number Diff line change 10
10
USERS = [] of Hash (String , JSON ::Any )
11
11
12
12
# GET - List all users
13
- get " /users" do |env |
13
+ get " /users" do |_ |
14
14
USERS .to_json
15
15
end
16
16
29
29
# POST - Create a new user
30
30
post " /users" do |env |
31
31
# Parse request body as JSON
32
+ # ameba:disable Lint/NotNil
32
33
user = JSON .parse(env.request.body.not_nil!.gets_to_end)
34
+ # ameba:enable Lint/NotNil
33
35
USERS << user.as_h
34
36
35
37
env.response.status_code = 201
@@ -42,7 +44,9 @@ put "/users/:id" do |env|
42
44
43
45
if id < USERS .size
44
46
# Parse request body as JSON
47
+ # ameba:disable Lint/NotNil
45
48
updated_user = JSON .parse(env.request.body.not_nil!.gets_to_end)
49
+ # ameba:enable Lint/NotNil
46
50
USERS [id] = updated_user.as_h
47
51
updated_user.to_json
48
52
else
Original file line number Diff line number Diff line change @@ -19,7 +19,9 @@ post "/" do |env|
19
19
# env.request.body contains the raw JSON data
20
20
# not_nil! ensures the body exists
21
21
# User.from_json converts the JSON string to a User object
22
+ # ameba:disable Lint/NotNil
22
23
user = User .from_json env.request.body.not_nil!
24
+ # ameba:enable Lint/NotNil
23
25
24
26
# Convert the user object back to JSON and return it
25
27
# This creates a JSON object with username and password fields
Original file line number Diff line number Diff line change @@ -20,10 +20,7 @@ class User
20
20
end
21
21
22
22
# List all users
23
- get " /users" do |env |
24
- # Initialize empty array to store User objects
25
- users = [] of User
26
-
23
+ get " /users" do |_ |
27
24
# Serialize ResultSet
28
25
users = User .from_rs(DBC .query(" SELECT * FROM users" ))
29
26
Original file line number Diff line number Diff line change @@ -20,10 +20,7 @@ class User
20
20
end
21
21
22
22
# List all users
23
- get " /users" do |env |
24
- # Initialize empty array to store User objects
25
- users = [] of User
26
-
23
+ get " /users" do |_ |
27
24
# Serialize ResultSet
28
25
users = User .from_rs(DBC .query(" SELECT * FROM users" ))
29
26
Original file line number Diff line number Diff line change 8
8
# Start Kemal with custom server configuration
9
9
Kemal .run do |config |
10
10
# Get the server instance from the config
11
+ # ameba:disable Lint/NotNil
11
12
server = config.server.not_nil!
13
+ # ameba:enable Lint/NotNil
12
14
13
15
# Bind the server to port 3000 with reuse_port enabled
14
16
# reuse_port: true allows multiple processes to listen on the same port
Original file line number Diff line number Diff line change @@ -3,7 +3,9 @@ require "kemal"
3
3
# Start Kemal with custom server configuration to use Unix Domain Socket
4
4
Kemal .run do |config |
5
5
# Get the server instance from the config
6
+ # ameba:disable Lint/NotNil
6
7
server = config.server.not_nil!
8
+ # ameba:enable Lint/NotNil
7
9
8
10
# Bind the server to a Unix Domain Socket instead of TCP port
9
11
# Unix Domain Sockets provide faster inter-process communication on the same machine
You can’t perform that action at this time.
0 commit comments