Skip to content

TrailBlazors/htmx-go-postgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Htmx + Go + PostgreSQL Starter

A modern, production-ready full-stack template using Htmx, Go, and PostgreSQL.

Build dynamic web applications without JavaScript frameworks. Use Htmx for interactivity, Go for a fast backend, and PostgreSQL for data persistence. No build step, no bundlers, just pure simplicity.

Deploy on Railway

✨ Features

  • 🎯 Htmx - Dynamic UI without JavaScript frameworks
  • πŸ”΅ Go - Fast, compiled backend with Chi router
  • 🐘 PostgreSQL - Reliable, production-ready database
  • 🎨 Tailwind CSS - Beautiful styling via CDN
  • 🐳 Docker Optimized - Multi-stage builds for small images
  • πŸš‚ Railway Ready - Zero-config deployment
  • ⚑ No Build Step - Just code and deploy
  • πŸ“ CRUD Example - Working todo list included

πŸš€ Quick Start

Deploy to Railway

Click the "Deploy on Railway" button above. Railway will automatically:

  • Build your Go application using Docker
  • Provision a PostgreSQL database
  • Connect them together
  • Generate a public URL

Local Development

Prerequisites:

  • Go 1.21 or higher
  • PostgreSQL (or use Docker)

Steps:

# Clone the repository
git clone https://github.com/YOUR_USERNAME/htmx-go-postgres.git
cd htmx-go-postgres

# Install dependencies
go mod download

# Set up PostgreSQL (or use Docker)
docker run --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres

# Set environment variables
export DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
export PORT=8080

# Run the application
go run cmd/web/main.go

# Open browser to http://localhost:8080

πŸ“ Project Structure

htmx-go-postgres/
β”œβ”€β”€ cmd/
β”‚   └── web/
β”‚       └── main.go              # Application entry point
β”œβ”€β”€ templates/
β”‚   β”œβ”€β”€ index.html               # Main page
β”‚   └── todo-list.html           # Todo list partial
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ css/                     # Custom CSS (optional)
β”‚   └── js/                      # Custom JS (optional)
β”œβ”€β”€ Dockerfile                   # Multi-stage Docker build
β”œβ”€β”€ railway.toml                 # Railway configuration
β”œβ”€β”€ go.mod                       # Go dependencies
β”œβ”€β”€ go.sum                       # Go checksums
└── README.md                    # Documentation

🎯 How It Works

Htmx Magic

Htmx allows you to build dynamic UIs using HTML attributes:

<!-- Add a todo -->
<form hx-post="/todos" 
      hx-target="#todo-list" 
      hx-swap="innerHTML">
    <input type="text" name="title" required>
    <button type="submit">Add</button>
</form>

<!-- Toggle completed -->
<input type="checkbox" 
       hx-put="/todos/123/toggle"
       hx-target="#todo-list">

<!-- Delete a todo -->
<button hx-delete="/todos/123"
        hx-target="#todo-list"
        hx-confirm="Delete this?">
    Delete
</button>

No JavaScript required! Htmx handles all the AJAX calls and DOM updates.

Go Backend

Simple, fast Go server with Chi router:

// Create todo
r.Post("/todos", app.createTodo)

// Toggle todo
r.Put("/todos/{id}/toggle", app.toggleTodo)

// Delete todo
r.Delete("/todos/{id}", app.deleteTodo)

Returns HTML fragments that Htmx swaps into the page.

PostgreSQL Database

Simple schema with auto-migration:

CREATE TABLE todos (
    id SERIAL PRIMARY KEY,
    title TEXT NOT NULL,
    completed BOOLEAN DEFAULT FALSE
);

πŸ› οΈ Customization

Add New Routes

Edit cmd/web/main.go:

// Add your route
r.Get("/mypage", app.myPageHandler)

// Create handler
func (app *Application) myPageHandler(w http.ResponseWriter, r *http.Request) {
    app.Templates.ExecuteTemplate(w, "mypage.html", data)
}

Add New Templates

Create templates/mypage.html:

<div>
    <h1>My Page</h1>
    <p>Content here...</p>
</div>

Add Database Models

Extend the schema in main.go:

func createTable(db *sql.DB) {
    query := `
        CREATE TABLE IF NOT EXISTS users (
            id SERIAL PRIMARY KEY,
            name TEXT NOT NULL,
            email TEXT UNIQUE NOT NULL
        );
    `
    db.Exec(query)
}

Add Styling

Use Tailwind classes inline, or add custom CSS in static/css/.

🌐 Why Htmx?

The Case Against SPAs

  • ❌ Complex build toolchains
  • ❌ Large JavaScript bundles
  • ❌ State management hell
  • ❌ SEO challenges
  • ❌ Slow initial load

The Htmx Advantage

  • βœ… No Build Step - Write HTML and Go
  • βœ… Small Payload - Htmx is ~14KB
  • βœ… Server-Side Rendering - SEO-friendly by default
  • βœ… Progressive Enhancement - Works without JS
  • βœ… Simple Mental Model - Just HTML attributes
  • βœ… Fast - No client-side framework overhead

πŸ“š Learn More

Htmx Resources

Go Resources

Deployment

πŸŽ“ Use Cases

Perfect For:

πŸ“Š Internal Dashboards - Admin panels, monitoring tools
πŸ“ Content-Heavy Sites - Blogs, documentation, news sites
πŸ› οΈ CRUD Applications - Data management, forms, tables
🏒 Business Applications - CRM, inventory, invoicing
πŸ“± Progressive Enhancement - Works without JavaScript

Not Ideal For:

❌ Real-time collaboration (use WebSockets instead)
❌ Heavy client-side state (use React/Vue)
❌ Offline-first apps (use PWA/WASM)
❌ Complex animations (use JavaScript)

⚑ Performance

  • Server Response: < 50ms (Go is fast!)
  • Page Load: Minimal (no framework to download)
  • Bundle Size: ~14KB (just Htmx + Tailwind CDN)
  • Memory Usage: ~20-30MB (Go binary)
  • Database Queries: Optimized with indexes

πŸ” Security

  • βœ… SQL injection protected (parameterized queries)
  • βœ… CSRF protection (add middleware if needed)
  • βœ… XSS protection (Go templates auto-escape)
  • βœ… HTTPS on Railway (automatic SSL)

🀝 Contributing

Contributions welcome! Here's how:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing)
  5. Open a Pull Request

πŸ“„ License

MIT License - see LICENSE file for details


Built with ❀️ for the Railway community πŸš‚

Tired of JavaScript complexity? This template proves you don't need React/Vue/Angular for dynamic UIs!

Questions? Open an issue on GitHub or reach out on Railway Discord.

About

Htmx + Go + PostgreSQL full-stack starter template for Railway

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published