|
| 1 | +# AI Chat Bot - Datastar + Templ + Chi |
| 2 | + |
| 3 | +A real-time chat bot application built with: |
| 4 | +- **Go Chi** - Lightweight HTTP router |
| 5 | +- **Templ** - Type-safe HTML templating |
| 6 | +- **Datastar** - Real-time reactivity |
| 7 | +- **Hot Reloading** - Development experience |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- 🤖 AI-powered chat responses |
| 12 | +- ⚡ Real-time streaming responses |
| 13 | +- 🎨 Beautiful, modern UI |
| 14 | +- 🔄 Hot reloading for development |
| 15 | +- 📱 Responsive design |
| 16 | +- ✨ Smooth typing animations |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +### Prerequisites |
| 21 | + |
| 22 | +- Go 1.24.3 or higher |
| 23 | +- Make (optional, for convenience commands) |
| 24 | + |
| 25 | +### Setup |
| 26 | + |
| 27 | +1. **Install dependencies and tools:** |
| 28 | + ```bash |
| 29 | + make setup |
| 30 | + ``` |
| 31 | + |
| 32 | +2. **Start development server with hot reloading:** |
| 33 | + ```bash |
| 34 | + make dev |
| 35 | + ``` |
| 36 | + |
| 37 | +3. **Open your browser:** |
| 38 | + - Main app: http://localhost:3000 |
| 39 | + - Hot reload proxy: http://localhost:7331 |
| 40 | + |
| 41 | +### Alternative Development Options |
| 42 | + |
| 43 | +**Option 1: Using Air (recommended)** |
| 44 | +```bash |
| 45 | +make dev |
| 46 | +``` |
| 47 | + |
| 48 | +**Option 2: Using Templ's built-in watch** |
| 49 | +```bash |
| 50 | +make dev-templ |
| 51 | +``` |
| 52 | + |
| 53 | +**Option 3: Manual run** |
| 54 | +```bash |
| 55 | +make run |
| 56 | +``` |
| 57 | + |
| 58 | +## Development Commands |
| 59 | + |
| 60 | +```bash |
| 61 | +# One-time setup |
| 62 | +make setup |
| 63 | + |
| 64 | +# Start development with hot reload |
| 65 | +make dev |
| 66 | + |
| 67 | +# Generate templ files only |
| 68 | +make templ-generate |
| 69 | + |
| 70 | +# Build the application |
| 71 | +make build |
| 72 | + |
| 73 | +# Run without hot reload |
| 74 | +make run |
| 75 | + |
| 76 | +# Clean generated files |
| 77 | +make clean |
| 78 | +``` |
| 79 | + |
| 80 | +## How It Works |
| 81 | + |
| 82 | +### Hot Reloading |
| 83 | + |
| 84 | +The application supports two hot reloading approaches: |
| 85 | + |
| 86 | +1. **Air** - Monitors file changes and rebuilds/restarts the server |
| 87 | +2. **Templ Watch** - Built-in templ file watching with proxy |
| 88 | + |
| 89 | +Both approaches provide: |
| 90 | +- Automatic template regeneration when `.templ` files change |
| 91 | +- Server restart when `.go` files change |
| 92 | +- Browser auto-refresh via proxy |
| 93 | + |
| 94 | +### File Structure |
| 95 | + |
| 96 | +``` |
| 97 | +apps/server/ |
| 98 | +├── server.go # Main Chi server |
| 99 | +├── templates.templ # Templ templates |
| 100 | +├── .air.toml # Air configuration |
| 101 | +├── Makefile # Development commands |
| 102 | +├── go.mod # Go dependencies |
| 103 | +└── README.md # This file |
| 104 | +``` |
| 105 | + |
| 106 | +### Architecture |
| 107 | + |
| 108 | +- **Chi** handles HTTP routing and middleware |
| 109 | +- **Templ** generates type-safe HTML templates |
| 110 | +- **Datastar** provides real-time reactivity via SSE |
| 111 | +- **Hot reload** proxy injects auto-refresh JavaScript |
| 112 | + |
| 113 | +## Chat Features |
| 114 | + |
| 115 | +The chat bot includes: |
| 116 | + |
| 117 | +- Keyword-based responses (hello, help, weather, etc.) |
| 118 | +- Streaming word-by-word responses |
| 119 | +- Typing indicators |
| 120 | +- Message history |
| 121 | +- Responsive design |
| 122 | + |
| 123 | +## Customization |
| 124 | + |
| 125 | +### Adding New Bot Responses |
| 126 | + |
| 127 | +Edit the `generateResponse` function in `server.go`: |
| 128 | + |
| 129 | +```go |
| 130 | +responses := map[string]string{ |
| 131 | + "your_keyword": "Your custom response", |
| 132 | + // ... existing responses |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### Styling |
| 137 | + |
| 138 | +Modify the CSS in `templates.templ` within the `<style>` block. |
| 139 | + |
| 140 | +### Templates |
| 141 | + |
| 142 | +Add new templ components in `templates.templ` or create new `.templ` files. |
| 143 | + |
| 144 | +## Production Build |
| 145 | + |
| 146 | +```bash |
| 147 | +make build |
| 148 | +./bin/server |
| 149 | +``` |
| 150 | + |
| 151 | +## Troubleshooting |
| 152 | + |
| 153 | +### Port Already in Use |
| 154 | +If port 3000 is busy, change it in `server.go`: |
| 155 | +```go |
| 156 | +log.Fatal(http.ListenAndServe(":8080", router)) // Change to your preferred port |
| 157 | +``` |
| 158 | + |
| 159 | +### Templ Generation Issues |
| 160 | +```bash |
| 161 | +make clean |
| 162 | +make templ-generate |
| 163 | +``` |
| 164 | + |
| 165 | +### Dependencies Issues |
| 166 | +```bash |
| 167 | +go mod tidy |
| 168 | +make deps |
| 169 | +``` |
| 170 | + |
| 171 | +## Learn More |
| 172 | + |
| 173 | +- [Chi Documentation](https://go-chi.io/) |
| 174 | +- [Templ Documentation](https://templ.guide/) |
| 175 | +- [Datastar Documentation](https://data-star.dev/) |
| 176 | +- [Air Documentation](https://github.com/air-verse/air) |
0 commit comments