From ff1da010408d49320253d5cda24777c547621ece Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Fri, 21 Jun 2024 13:18:29 +0530 Subject: HTMX and Strapi Integration * Re-integrates HTMX * Integrate STRAPI API * Adds endpoints for fetching posts * Adds HTMX area for loading recent posts --- shard.yml | 4 +++ src/indrajith-dev-crystal.cr | 71 +++++++++++++++++++++++++++++++++++++++++++- src/views/home.ecr | 7 ++--- src/views/layout.ecr | 1 + 4 files changed, 77 insertions(+), 6 deletions(-) diff --git a/shard.yml b/shard.yml index f6c9525..228995d 100644 --- a/shard.yml +++ b/shard.yml @@ -11,6 +11,10 @@ targets: dependencies: kemal: github: kemalcr/kemal + crest: + github: mamantoha/crest + dotenv: + github: xtokio/dotenv crystal: '>= 1.12.2' diff --git a/src/indrajith-dev-crystal.cr b/src/indrajith-dev-crystal.cr index ca600f1..74fecc2 100644 --- a/src/indrajith-dev-crystal.cr +++ b/src/indrajith-dev-crystal.cr @@ -1,4 +1,11 @@ require "kemal" +require "crest" +require "json" +require "dotenv" + +Dotenv.load + +TOKEN = ENV["TOKEN"] module Indrajith::Dev::Crystal VERSION = "0.1.0" @@ -8,6 +15,11 @@ module Indrajith::Dev::Crystal render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr" end + macro render_404() + page_renderer "404", "The VOID" + end + + get "/" do |context| context.response.content_type = "text/html" page_renderer "home", "Home" @@ -28,9 +40,66 @@ module Indrajith::Dev::Crystal page_renderer "contact", "Contact" end + get "/posts" do |context| + begin + site = Crest::Resource.new("http://localhost:1337") + + response = site.get("/api/posts", + params: { + "fields[0]" => "post_title", + "fields[1]" => "slug", + "pagination[pageSize]" => 3, + }, + headers: {"Authorization" => "Bearer #{TOKEN}"} + ) + + json_data = JSON.parse(response.body) + data = json_data["data"].as_a + html_string = "" + data.each do |item| + attributes = item["attributes"] + # puts attributes["post_title"] + html_string += " +
  • +

    #{attributes["post_title"]}

    +
  • " + end + html_string + rescue ex : Crest::NotFound + puts ex.response + render_404 + end + end + + get "/post/:slug" do |context| + begin + slug = context.params.url["slug"] + site = Crest::Resource.new("http://localhost:1337") + + response = site.get("/api/posts", + params: { + "filters[slug][$eq]" => "#{slug}", + }, + headers: {"Authorization" => "Bearer #{TOKEN}"} + ) + + json_data = JSON.parse(response.body) + data = json_data["data"].as_a + + post_item = data[0] + post_attribute = post_item["attributes"] + content = post_attribute["post_content"] + page_title = post_attribute["post_title"] + render "src/views/layout.ecr" + rescue ex + puts ex + render_404 + end + end + error 404 do |context| context.response.content_type = "text/html" - page_renderer "404", "The VOID" + render_404 end Kemal.run diff --git a/src/views/home.ecr b/src/views/home.ecr index fb4973f..2928a32 100644 --- a/src/views/home.ecr +++ b/src/views/home.ecr @@ -6,17 +6,14 @@

    - +

    Books I'm reading now

    Recent Posts

    -
    diff --git a/src/views/layout.ecr b/src/views/layout.ecr index 2a537ea..abb2dae 100644 --- a/src/views/layout.ecr +++ b/src/views/layout.ecr @@ -5,6 +5,7 @@ <%= page_title %> | {indrajith.dev} + -- cgit v1.2.3