Go to file
link e35816d3c1 Rename user endpoints that return partials 2024-02-14 17:35:39 +10:00
frontend Rename user endpoints that return partials 2024-02-14 17:35:39 +10:00
handlers Rename user endpoints that return partials 2024-02-14 17:35:39 +10:00
models Move UserTableHandler and User model to own packages 2024-02-14 16:52:46 +10:00
partials Create UserTable partial 2024-02-14 17:23:23 +10:00
.gitignore Init commit 2024-02-05 21:37:58 +10:00
Makefile Update build-server and create server-dev commands 2024-02-14 16:11:09 +10:00
README.md Update README.md 2024-02-14 16:51:52 +10:00
go.mod Install templ 2024-02-14 16:11:09 +10:00
go.sum Install templ 2024-02-14 16:11:09 +10:00
main.go Rename user endpoints that return partials 2024-02-14 17:35:39 +10:00

README.md

Echo-Todo

Experimenting with the echo server framework for golang, connecting to db hosted on turso making use of the libsql client for go and database/sql from go stdlib to do querying

Building a simple static html webpage that is stored in frontend, run cd frontend && bun run dev to boot up the vite development server.l

The Go web server is configured to serve the built vite project from frontend/dist on the index route.

The development workflow current looks something like:

  • Launch tmux
  • Open nvim in one pane
  • Open 2 split panes to the side
  • One for frontend: cd frontend && bun run dev
  • One for backend: make server-dev which will run templ command and air both simultaneously

This provides hot reload for both the front and backend. When making api requests from the frontend ensure we specify the address as these are on different ports now.

Todos

  • Give the AddUserTable component functionality to write to DB.
  • Clean up project by moving handlers, models and middleware to their own folder and files.
  • Secure the api queries with some basic auth to begin with.
  • Ensure a reliable build process that ensures the server and the static site are both bundled together and can be executed with ease.
  • Install tailwind and style components
  • Read DB URL and token from .env rather than hardcoded values.
  • Implement a basic route which returns all users from the DB: Endpoint /api/allUsers implemented
  • Make use of middleware:
    • Logging
    • Db connection: An example of this can be found in the AllUsersHandler.

Install

Quick scrapy notes for setting up the project from scratch.

  1. Clone this repo.
  2. Create a .env file with the DB_URL and DB_TOKEN.
  3. For prod builds you could run both make build-server && make build-frontend and run the server with the static files from the frontend on your platform or hardware of choice.
  4. For development make sure you have air installed go install github.com/cosmtrek/air@latest.
  5. Work on the backend and run air in the root directory of this project to get hot reloading for thego project.
  6. Work on the frontend and run cd frontend && bun run dev to get a vite hot reloading dev server.

Ideas

Build the majority of the frontend in vanilla js/html making use of HTMX. Where are hx-requests expect html back we should build and hydrate the components on the backend in golang using templ to generate the HTML.

So as the idea grows in my mind, I think I might end up slowly transitioning most of the static site to templ, the thought of managing both a static site and backend components seems a bit messy, though so does managing all HTML in go at first. I will have to strike a balance between the two, having good documentation is key I believe. Though I am enjoying using vite to handle the vanilla html/js and bundling dependencies.

Structure

A generally standard golang project structure:

  • handlers: Functions for handling the api requests.
  • models: Structs for representing data in the DB or application state
  • frontend: Vanilla JS Vite project
  • partials: Go Templ components which aren't full page views, could of been called components

main.go currently resides in the root of the project but am considering moving it to cmd/server/main.go views/ does not currently exist but as the project grows and I experiment more, view page templ views will be stored here

Libraries & Tools

A list of tools and libraries that are used to power this project.

  • Golang (Backend)
  • Bun (Node.js replacement cli)
  • Vite (HTML build tool)
  • Templ (Golang HTML templating engine)
  • Air (Go dev server)
  • Turso (DB)
  • HTMX (Hypermedia gang gang)
  • Echo (Golang web server framework)