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

Adding basic tailwind conversion code #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Go parameters
GOCMD=go
TEMPL=templ
TAILWIND=tailwindcss-linux-x64
BUILD_DIR=./tmp
URCHIN_DIR=./cmd/urchin
URCHIN_ADMIN_DIR=./cmd/urchin-admin
Expand All @@ -13,6 +14,9 @@ all: build test

build:
$(TEMPL) generate
$(TAILWIND) -i ./static/style.css -o ./static/output.css -m
$(GOCMD) build -v -o $(BUILD_DIR)/$(BINARY_NAME) $(URCHIN_DIR)
$(GOCMD) build -v -o $(BUILD_DIR)/$(ADMIN_BINARY_NAME) $(URCHIN_ADMIN_DIR)
GIN_MODE=release $(GOCMD) build -ldflags "-s" -v -o $(BUILD_DIR)/$(BINARY_NAME) $(URCHIN_DIR)
GIN_MODE=release $(GOCMD) build -ldflags "-s" -v -o $(BUILD_DIR)/$(ADMIN_BINARY_NAME) $(URCHIN_ADMIN_DIR)

Expand Down
7 changes: 7 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"net/http"
"time"

"github.com/a-h/templ"
"github.com/gin-gonic/gin"
"github.com/matheusgomes28/urchin/common"
"github.com/matheusgomes28/urchin/database"
"github.com/matheusgomes28/urchin/views"
"github.com/matheusgomes28/urchin/views/tailwind"
"github.com/rs/zerolog/log"
)

Expand All @@ -20,6 +22,8 @@ func SetupRoutes(app_settings common.AppSettings, database database.Database) *g
r := gin.Default()
r.MaxMultipartMemory = 1

r.GET("/tailwind", makeHomeHandler(app_settings, database, tailwind.MakeIndex))

// All cache-able endpoints
cache := MakeCache(4, time.Minute*10, &TimeValidator{})
addCachableHandler(r, "GET", "/", homeHandler, &cache, app_settings, database)
Expand All @@ -33,6 +37,7 @@ func SetupRoutes(app_settings common.AppSettings, database database.Database) *g
return r
}


func addCachableHandler(e *gin.Engine, method string, endpoint string, generator Generator, cache *Cache, app_settings common.AppSettings, db database.Database) {

handler := func(c *gin.Context) {
Expand All @@ -45,6 +50,7 @@ func addCachableHandler(e *gin.Engine, method string, endpoint string, generator

// Before handler call (retrieve from cache)
html_buffer, err := generator(c, app_settings, db)

if err != nil {
log.Error().Msgf("could not generate html: %v", err)
// TODO : Need a proper error page
Expand All @@ -55,6 +61,7 @@ func addCachableHandler(e *gin.Engine, method string, endpoint string, generator
return
}


// After handler (add to cache)
err = (*cache).Store(c.Request.RequestURI, html_buffer)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./**/*.templ"],
theme: {
extend: {},
fontFamily: {
'roboto': ['Roboto', 'sans-serif'],
'bangers': ['Bangers', 'sans-serif'],
}
},
plugins: [],
}
5 changes: 5 additions & 0 deletions views/tailwind/footer.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tailwind

templ MakeFooter() {
<!-- -->
}
33 changes: 33 additions & 0 deletions views/tailwind/header.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tailwind

type Link struct {
Href string
Name string
}

templ MakeNavBar(links []Link) {
<header>
<nav class="bg-gray-800">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex items-center justify-between h-16">
<div class="flex items-center">
<div class="flex-shrink-0">
<a href="#" class="text-white text-2xl font-bold">Logo</a>
</div>
<div class="hidden md:block">
<div class="ml-10 flex items-baseline space-x-4">
for _, link := range links {
<a class="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium" href={templ.URL(link.Href)}>{ link.Name }</a>
}
</div>
</div>
</div>
</div>
</div>
</nav>



</header>
<br />
}
42 changes: 42 additions & 0 deletions views/tailwind/index.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tailwind

import (
"fmt"
. "github.com/matheusgomes28/common"
)

templ MakeIndex(posts []Post) {
<!DOCTYPE html>
<html lang="en" class="font-roboto">

// This should go into Make HTML Headers
<head>
<meta charset="UTF-8" />
<title>Menu and Contact Form</title>
<link rel="stylesheet" href="/static/output.css" />
<script src="/static/htmx.min.js"></script>
<script src="/static/client-side-templates.js"></script>
</head>

<body>
@MakeNavBar([]Link{
{Name: "Home", Href: "/"},
{Name: "About", Href: "/about"},
{Name: "Services", Href: "/services"},
{Name: "Contact", Href: "/contact"},
})
<main class="flex flex-col">
for _, post := range posts {
<article class="container mx-auto rounded-lg border-4 border-solid border-slate-700 p-4 mb-4 xl:w-96">
<h2 class="text-2xl font-bold text-gray-800">{ post.Title }</h2>
<p>
{ post.Excerpt }
<a href={ templ.URL("/post/" + fmt.Sprintf("%d", post.Id)) } class="font-bold text-gray-800 dark:text-gray-800 hover:underline"> read more...</a>
</p>
</article>
}
</main>
@MakeFooter()
</body>
</html>
}
Loading