Skip to content

Commit

Permalink
Added more components
Browse files Browse the repository at this point in the history
  • Loading branch information
davesavic committed Dec 24, 2023
1 parent 7fce259 commit d2a813c
Show file tree
Hide file tree
Showing 18 changed files with 560 additions and 44 deletions.
46 changes: 46 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "bin"

[build]
args_bin = []
bin = "./bin/aio serve"
cmd = "make build"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ generate:
@go generate ./...
templ generate

.PHONY: build run clean test coverage
air:
@air -c .air.toml

.PHONY: build run clean test coverage generate air
Binary file modified bin/aio
Binary file not shown.
1 change: 1 addition & 0 deletions bin/build-errors.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2
15 changes: 8 additions & 7 deletions services/serve/serve.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package serve

import (
"github.com/davesavic/aio/views/layout"
"github.com/davesavic/aio/views/page"
templEngine "github.com/davesavic/aio/services/templ"
view "github.com/davesavic/aio/view"
"github.com/gin-gonic/gin"
"net/http"
)

func Run() error {
r := gin.New()
r.HTMLRender = templEngine.NewRenderer()
r.Use(gin.Recovery())
r.Use(gin.Logger())
r.GET("/", func(context *gin.Context) {
layout.UnauthorisedLayout(
"Login",
page.LoginPage(),
).Render(context, context.Writer)

r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "landing", view.LandingPage())
})

return r.Run()
}
40 changes: 40 additions & 0 deletions services/templ/renderer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package templ

import (
"context"
"github.com/a-h/templ"
"github.com/gin-gonic/gin/render"
"net/http"
)

type Render struct {
Code int
Component templ.Component
}

func (t Render) Render(w http.ResponseWriter) error {
t.WriteContentType(w)
w.WriteHeader(t.Code)
if t.Component != nil {
return t.Component.Render(context.Background(), w)
}
return nil
}

func (t Render) WriteContentType(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}

func (t Render) Instance(_ string, data interface{}) render.Render {
if templData, ok := data.(templ.Component); ok {
return &Render{
Code: http.StatusOK,
Component: templData,
}
}
return nil
}

func NewRenderer() render.HTMLRender {
return Render{}
}
7 changes: 7 additions & 0 deletions view/button.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package view

templ Button(text string, action func() string) {
<a href={ templ.SafeURL(action()) } class="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
{text}
</a>
}
17 changes: 9 additions & 8 deletions views/layout/unauthorised_templ.go → view/button_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions view/landing.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package view

templ LandingPage() {
@UnauthorisedLayout("Landing page") {
@UnauthorisedHeader()
}
}
28 changes: 17 additions & 11 deletions views/page/login_templ.go → view/landing_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions view/layout_unauthorised.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package view

templ UnauthorisedLayout(title string) {
<html>
<head>
<script src="https://unpkg.com/[email protected]" integrity="sha384-D1Kt99CQMDuVetoL1lrYwg5t+9QdHe7NLX/SoJYkXDFfX37iInKRy5xLSi8nO7UC" crossorigin="anonymous"></script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<title>{title}</title>
</head>
<body>
{ children... }
</body>
</html>
}
70 changes: 70 additions & 0 deletions view/layout_unauthorised_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions view/link.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package view

type LinkVariant string

const (
LinkVariantPrimary LinkVariant = "primary"
LinkVariantSecondary LinkVariant = "secondary"
)

templ Link(href string, text string, variant LinkVariant) {
<a href={ templ.SafeURL(href) } class="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
{text}
</a>
}
Loading

0 comments on commit d2a813c

Please sign in to comment.