feat: ping route + controller

This commit is contained in:
Juan Rodriguez
2024-05-12 21:25:57 +02:00
parent db42ed2b24
commit b7b47b133d
14 changed files with 103 additions and 11 deletions
+1
View File
@@ -0,0 +1 @@
DATABASE_URL=./sqlite/db.sqlite
+24
View File
@@ -4,6 +4,18 @@ shards:
git: https://github.com/sija/backtracer.cr.git
version: 1.2.2
crecto:
git: https://github.com/fridgerator/crecto.git
version: 0.12.1
db:
git: https://github.com/crystal-lang/crystal-db.git
version: 0.13.1
dotenv:
git: https://github.com/gdotdesign/cr-dotenv.git
version: 0.6.0
exception_page:
git: https://github.com/crystal-loot/exception_page.git
version: 0.4.1
@@ -12,7 +24,19 @@ shards:
git: https://github.com/kemalcr/kemal.git
version: 1.5.0
micrate:
git: https://github.com/amberframework/micrate.git
version: 0.3.3
radix:
git: https://github.com/luislavena/radix.git
version: 0.4.1
spec-kemal:
git: https://github.com/kemalcr/spec-kemal.git
version: 0.5.0
sqlite3:
git: https://github.com/crystal-lang/crystal-sqlite3.git
version: 0.21.0
+15 -3
View File
@@ -1,16 +1,28 @@
name: url-shortener
name: pa
version: 0.1.0
authors:
- Juan Rodriguez <sjdonado@icloud.com>
targets:
url-shortener:
main: src/url-shortener.cr
pa:
main: src/pa.cr
dependencies:
kemal:
github: kemalcr/kemal
sqlite3:
github: crystal-lang/crystal-sqlite3
crecto:
github: fridgerator/crecto
micrate:
github: amberframework/micrate
development_dependencies:
dotenv:
github: gdotdesign/cr-dotenv
spec-kemal:
github: kemalcr/spec-kemal
crystal: '>= 1.12.1'
+1 -1
View File
@@ -1,2 +1,2 @@
require "spec"
require "../src/url-shortener"
require "../src/pa"
+6
View File
@@ -0,0 +1,6 @@
ENV["APP_ENV"] ||= "development"
{% if env("APP_ENV") != "production" %}
require "dotenv"
Dotenv.load ".env.#{ENV["APP_ENV"]}" # File must exist in non-production!
{% end %}
+5
View File
@@ -0,0 +1,5 @@
require "kemal"
Kemal.config.env = ENV["ENV"]? || "development"
Kemal.config.port = ENV["PORT"]?.try(&.to_i) || 3000
Kemal.config.host_binding = ENV["HOST"]? || "0.0.0.0"
+10
View File
@@ -0,0 +1,10 @@
require "../lib/controller.cr"
module Pa::Controllers::Ping
class Get < Pa::Lib::BaseController
def call(env)
response = {"pong" => "ok"}
response.to_json
end
end
end
+4
View File
@@ -0,0 +1,4 @@
module Pa::Lib
abstract class BaseController
end
end
+10
View File
@@ -0,0 +1,10 @@
require "./config/*"
require "./services/*"
require "./routes"
error 500 { |env| {"status" => 500, "error" => "Internal Server Error"}.to_json }
error 401 { |env| {"status" => 401, "error" => "Unauthorized"}.to_json }
error 403 { |env| {"status" => 403, "error" => "Forbidden"}.to_json }
error 404 { |env| {"status" => 404, "error" => "Not Found"}.to_json }
Kemal.run
+8
View File
@@ -0,0 +1,8 @@
require "kemal"
require "./controllers/**"
module Pa
get "/api/ping" do |env|
Controllers::Ping::Get.new.call(env)
end
end
+16
View File
@@ -0,0 +1,16 @@
require "sqlite3"
require "crecto"
module Pa::Services
class Repo
extend Crecto::Repo
Query = Crecto::Repo::Query
Multi = Crecto::Repo::Multi
config do |conf|
conf.adapter = Crecto::Adapters::SQLite3
conf.database = ENV["DATABASE_URL"]
end
end
end
-7
View File
@@ -1,7 +0,0 @@
require "kemal"
get "/" do
"Hello World!"
end
Kemal.run
+3
View File
@@ -0,0 +1,3 @@
module Pa
VERSION = "0.1.0"
end