Skip to content

Commit

Permalink
✨ Added version/build information to header and about page
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Carr committed Apr 4, 2022
1 parent 8522ffb commit 85f2d45
Show file tree
Hide file tree
Showing 16 changed files with 796 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ builds:
- env:
- CGO_ENABLED=0
binary: tiny-todo
ldflags:
- -s -w -X github.com/lukecarr/tiny-todo/internal/info.Version={{.Tag}} -X github.com/lukecarr/tiny-todo/internal/info.Commit={{.FullCommit}} -X github.com/lukecarr/tiny-todo/internal/info.Date={{.Date}}
goarch:
- amd64
- '386'
Expand Down
30 changes: 30 additions & 0 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/lukecarr/tiny-todo/internal/db"
"github.com/spf13/cobra"
)

func MakeMigrateCmd() *cobra.Command {
return &cobra.Command{
Use: "migrate",
Short: "Performs tiny-todo's SQLite database migrations",
Run: func(cmd *cobra.Command, args []string) {
n, err := db.Migrate(args[0])

if err != nil {
cmd.PrintErrf("%s\n", err)
} else if n == 0 {
cmd.Println("The provided SQLite database is already up to date!")
} else {
cmd.Printf("Applied %d migrations successfully!\n", n)
}
},
}
}

var migrateCmd = MakeMigrateCmd()

func init() {
rootCmd.AddCommand(migrateCmd)
}
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func MakeServeCmd() *cobra.Command {
Use: "serve",
Short: "Serves tiny-todo's server",
Run: func(cmd *cobra.Command, args []string) {
srv := server.New("")
srv := server.New(osx.Getenv("TINY_TODO_DSN", ":memory:"))
srv.Listen(osx.Getenv("TINY_TODO_ADDR", ":3000"))
},
}
Expand Down
3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"dependencies": {
"preact": "10.7.0",
"preact-async-route": "2.2.1",
"preact-router": "4.0.1"
"preact-router": "4.0.1",
"swr": "1.2.2"
},
"devDependencies": {
"@preact/preset-vite": "2.2.0",
Expand Down
8 changes: 8 additions & 0 deletions frontend/pnpm-lock.yaml

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

19 changes: 18 additions & 1 deletion frontend/src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import useSWR from 'swr'

import type { FunctionalComponent } from 'preact'

const fetcher = (url: string) => fetch(url).then(r => r.json())

const Version: FunctionalComponent = () => {
const { data, error } = useSWR<{
version: string
}>('/api/version', fetcher)

if (error) return null
if (!data?.version) return null

const version = data.version.startsWith('v') ? data.version.slice(1) : data.version
return <span class="ml-4 px-3 py-1 text-white bg-black text-base">{version}</span>
}


const Header: FunctionalComponent = () => {
return <header class="border-b-1 border-gray-400 py-8">
<div class="container max-w-screen-md">
<h1 class="font-extrabold text-3xl">tiny-todo</h1>
<h1 class="font-extrabold text-3xl flex items-center">tiny-todo <Version /></h1>
<nav class="mt-4">
<a href="/">todo</a>
<span> &#9679; </span>
Expand Down
17 changes: 16 additions & 1 deletion frontend/src/routes/about.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import useSWR from 'swr'
import { useEffect } from 'preact/hooks'

import type { FunctionalComponent } from 'preact'

const fetcher = (url: string) => fetch(url).then(r => r.json())

const Version: FunctionalComponent = () => {
const { data, error } = useSWR<{
version: string
}>('/api/version', fetcher)

if (error) return <p>Failed to load tiny-todo version!</p>
if (!data) return <p>Version: loading...</p>
return <p>Version: {data.version}</p>
}

const About: FunctionalComponent = () => {
useEffect(() => {
document.title = 'about :: tiny-todo'
}, [])

return (
<>
<ul>
<ul class="mb-8">
<li class="font-bold">REST API and HTTP server built using Go:</li>
<ul class="ml-8 mb-4">
<li>HTTP framework: Fiber.</li>
Expand All @@ -24,6 +37,8 @@ const About: FunctionalComponent = () => {
<li>Styled with WindiCSS.</li>
</ul>
</ul>

<Version />
</>
)
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
Expand Down
21 changes: 13 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ module github.com/lukecarr/tiny-todo

go 1.17

require (
github.com/gofiber/fiber/v2 v2.31.0
github.com/moducate/x v0.0.0-20210723175934-0d481c1ebf85
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.1
)

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/gofiber/fiber/v2 v2.31.0 // indirect
github.com/gofiber/rewrite/v2 v2.1.21 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-gorp/gorp/v3 v3.0.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/klauspost/compress v1.15.1 // indirect
github.com/moducate/x v0.0.0-20210723175934-0d481c1ebf85 // indirect
github.com/mattn/go-sqlite3 v1.14.12 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/rubenv/sql-migrate v1.1.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.1.0 // indirect
github.com/stretchr/testify v1.7.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.34.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220403205710-6acee93ad0eb // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 85f2d45

Please sign in to comment.