Skip to content

Commit

Permalink
feat: prepare tests, restructure templates and http mux
Browse files Browse the repository at this point in the history
  • Loading branch information
kodehat committed Feb 23, 2024
1 parent 55bb152 commit e027746
Show file tree
Hide file tree
Showing 22 changed files with 278 additions and 166 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ COPY internal internal/

RUN apk add --no-cache git bash
RUN go install github.com/a-h/templ/cmd/templ@latest && templ generate
RUN sh build.sh $VERSION
RUN sh build.sh "$VERSION"

FROM alpine:3.19.1

Expand Down
3 changes: 2 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
host: localhost
port: 3000
title: "portkey"
footerText: "Works like a portal."
footer: |-
<p>made with ☕️ by <a class="no-underline border-b-2 border-solid" target="_blank" rel="nofollow" href="https://www.codehat.de">CodeHat</a></p>
sortAlphabetically: false
portals:
- title: 1password
Expand Down
28 changes: 15 additions & 13 deletions internal/components/layouts.templ
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package components

templ Base(title string) {
import "github.com/kodehat/portkey/internal/config"

templ Base(pageTitle string, config config.Config) {
<!DOCTYPE html>
<html lang="en" x-cloak x-data="{ darkMode: $persist(false) }" :class="{ 'dark': darkMode === true }">
<head>
Expand All @@ -21,7 +23,7 @@ templ Base(title string) {
<!-- Deferred JavaScript -->
<script defer src="/static/js/main.js"></script>
<!-- Set title -->
<title>{ title }</title>
<title>{ config.Title }</title>
<!-- TODO: Embed analytic script here -->
</head>
<body class="bg-slate-200 dark:bg-slate-700 text-slate-700 dark:text-slate-200 text-base m-0 p-0 leading-[2rem] tracking-wide shadow-none">
Expand All @@ -48,27 +50,27 @@ templ Base(title string) {
</span>
</button>
</div>
<h1 class="mb-4 text-center font-extrabold text-5xl md:text-6xl lg:text-7xl tracking-tight leading-none drop-shadow-xl shadow-slate-400">portkey</h1>
<h1 class="my-6 text-center font-extrabold text-5xl md:text-6xl lg:text-7xl tracking-tight leading-none drop-shadow-xl shadow-slate-400">{ config.Title }</h1>
{ children... }
</body>
</html>
}

templ HomeLayout(title string, contents templ.Component) {
@Base(title) {
templ HomeLayout(pageTitle string, config config.Config, contents templ.Component) {
@Base(pageTitle, config) {
<nav class="my-4 lg:my-8 mx-auto flex flex-col justify-center items-center">
@contents
</nav>
<footer class="w-full flex justify-center text-center bg-transparent relative bottom-0 p-4 text-base opacity-80">
<p>made with ☕️ by <a class="no-underline border-b-2 border-solid" target="_blank" rel="nofollow" href="https://www.codehat.de">CodeHat</a></p>
@templ.Raw(config.Footer)
</footer>
}
}

templ ContentLayout(title string, header string, contents templ.Component, portals []templ.Component, footerText string) {
@Base(title) {
templ ContentLayout(pageTitle string, config config.Config, contents templ.Component) {
@Base(pageTitle, config) {
<header class="flex flex-col justify-center items-start m-auto w-[90%] md:w-[45%] max-w-[64em] pt-8 lg:pt-16 pr-4 pb-4">
<h1 class="text-3xl mb-2 opacity-60 ">{ header }</h1>
<h1 class="text-3xl mb-2 opacity-60 ">{ pageTitle }</h1>
</header>
<main class="m-auto w-[90%] md:w-[45%] max-w-[64em]">
<article class="flex flex-col justify-center items-start w-full leading-[2em] tracking-[2px]">
Expand All @@ -77,17 +79,17 @@ templ ContentLayout(title string, header string, contents templ.Component, porta
<nav class="flex flex-col justify-center items-center text-center my-[1em] mx-auto">
<hr class="my-4 bg-transparent w-1/2 border border-solid opacity-30"/>
<p class="inline space-x-2">
for i, portal := range portals {
@portal
if i != len(portals) - 1 {
for i, portal := range config.Portals {
@FooterPortal(portal)
if i != len(config.Portals) - 1 {
&#183;
}
}
</p>
</nav>
</main>
<footer class="w-full flex justify-center items-center text-center bg-transparent relative bottom-0 p-4 text-base opacity-80">
{ footerText }
@templ.Raw(config.Footer)
</footer>
}
}
6 changes: 4 additions & 2 deletions internal/components/partials.templ
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package components

templ PortalPartial(portals []templ.Component) {
import "github.com/kodehat/portkey/internal/models"

templ PortalPartial(portals []models.Portal) {
for _, portal := range portals {
@portal
@HomePortal(portal)
}
}
26 changes: 14 additions & 12 deletions internal/components/portal.templ
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package components

templ HomePortal(link string, emoji string, title string, external bool) {
import "github.com/kodehat/portkey/internal/models"

templ HomePortal(portal models.Portal) {
<span class="text-3xl md:text-4xl lg:text-5xl font-normal md:font-light inline-flex items-center">
if emoji != "" {
{ emoji }
if portal.Emoji != "" {
{ portal.Emoji }
}
<a
if external {
if portal.External {
target="_blank"
rel="nofollow"
}
href={ templ.SafeURL(link) }
href={ templ.SafeURL(portal.Link) }
class="mx-4 whitespace-nowrap no-underline border-b-2 border-solid border-slate-700 dark:border-slate-300 hover:text-slate-800 dark:hover:text-slate-300"
>{ title }</a>
>{ portal.Title }</a>
</span>
}

templ FooterPortal(link string, emoji string, title string, external bool) {
templ FooterPortal(portal models.Portal) {
<span class="mx-2 inline-flex items-center">
if emoji != "" {
{ emoji }
if portal.Emoji != "" {
{ portal.Emoji }
}
<a
if external {
if portal.External {
target="_blank"
rel="nofollow"
}
href={ templ.SafeURL(link) }
href={ templ.SafeURL(portal.Link) }
class="ml-1 no-underline border-b-2 border-solid border-slate-700 dark:border-slate-300 hover:text-slate-800 dark:hover:text-slate-300"
>{ title }</a>
>{ portal.Title }</a>
</span>
}
17 changes: 11 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
Expand All @@ -14,9 +15,9 @@ import (

type Config struct {
Host string
Port int
Port string
Title string
FooterText string
Footer string
SortAlphabetically bool
Portals []models.Portal
Pages []models.Page
Expand All @@ -29,8 +30,8 @@ type Flags struct {
var C Config
var F Flags

func init() {
loadFlags()
func Load() {
LoadFlags()
configPath, err := filepath.Abs(F.ConfigPath)
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
Expand All @@ -39,9 +40,13 @@ func init() {
loadConfig(F.ConfigPath)
}

func loadFlags() {
func LoadFlags() {
var configPath string
flag.StringVar(&configPath, "config-path", ".", "path where config.yml can be found")
workDir, err := os.Getwd()
if err != nil {
workDir = "."
}
flag.StringVar(&configPath, "config-path", workDir, "path where config.yml can be found")
flag.Parse()
F = Flags{
ConfigPath: configPath,
Expand Down
30 changes: 0 additions & 30 deletions internal/routes/home_handler.go

This file was deleted.

41 changes: 0 additions & 41 deletions internal/routes/routes.go

This file was deleted.

24 changes: 0 additions & 24 deletions internal/routes/version_handler.go

This file was deleted.

10 changes: 10 additions & 0 deletions internal/server/health_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package server

import "net/http"

func healthHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
}
21 changes: 21 additions & 0 deletions internal/server/home_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package server

import (
"net/http"

"github.com/a-h/templ"
"github.com/kodehat/portkey/internal/components"
"github.com/kodehat/portkey/internal/config"
)

func homeHandler() http.HandlerFunc {
home := components.HomePage()
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
w.WriteHeader(http.StatusNotFound)
templ.Handler(components.ContentLayout("404 Not Found", config.C, components.NotFound())).ServeHTTP(w, r)
return
}
templ.Handler(components.HomeLayout("Home", config.C, home)).ServeHTTP(w, r)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package routes
package server

import (
"net/http"
"sync"

"github.com/a-h/templ"
"github.com/kodehat/portkey/internal/components"
"github.com/kodehat/portkey/internal/config"
"github.com/kodehat/portkey/internal/utils"
)

type pageHandlerInfo struct {
Expand All @@ -16,19 +14,11 @@ type pageHandlerInfo struct {
}

func pageHandler() []pageHandlerInfo {
var (
init sync.Once
allFooterPortals []templ.Component
)
init.Do(func() {
allFooterPortals = getAllFooterPortals()
})

var pageHandlerInfos = make([]pageHandlerInfo, len(config.C.Pages))
for i, page := range config.C.Pages {
pageHandlerInfos[i] = pageHandlerInfo{
pagePath: page.Path,
handlerFunc: templ.Handler(components.ContentLayout(utils.PageTitle(page.Heading, config.C.Title), page.Heading, components.ContentPage(page.Content), allFooterPortals, config.C.FooterText)).ServeHTTP,
handlerFunc: templ.Handler(components.ContentLayout(page.Heading, config.C, components.ContentPage(page.Content))).ServeHTTP,
}
}
return pageHandlerInfos
Expand Down
Loading

0 comments on commit e027746

Please sign in to comment.