Skip to content

Commit 34a83a4

Browse files
committed
Game
1 parent 954078a commit 34a83a4

41 files changed

Lines changed: 9668 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

game/.air.toml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
root = "."
2+
tmp_dir = "tmp"
3+
4+
[build]
5+
bin = "./tmp/main"
6+
cmd = "templ generate && go build -o ./tmp/main ."
7+
delay = 1000
8+
exclude_dir = ["assets", "tmp", "vendor"]
9+
exclude_file = []
10+
exclude_regex = [".*_templ.go"]
11+
exclude_unchanged = false
12+
follow_symlink = false
13+
full_bin = ""
14+
include_dir = []
15+
include_ext = ["go", "tpl", "tmpl", "templ", "html"]
16+
kill_delay = "0s"
17+
log = "build-errors.log"
18+
send_interrupt = false
19+
stop_on_error = true
20+
21+
[color]
22+
app = ""
23+
build = "yellow"
24+
main = "magenta"
25+
runner = "green"
26+
watcher = "cyan"
27+
28+
[log]
29+
time = false
30+
31+
[misc]
32+
clean_on_exit = false
33+
34+
[proxy]
35+
enabled = true
36+
proxy_port = 7331
37+
app_port = 3000

game/.templui.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"componentsDir": "ui/components",
3+
"utilsDir": "utils",
4+
"moduleName": "server",
5+
"jsDir": "assets/js"
6+
}

game/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ARG GO_VERSION=1
2+
FROM golang:${GO_VERSION}-bookworm as builder
3+
4+
WORKDIR /usr/src/app
5+
COPY go.mod go.sum ./
6+
RUN go mod download && go mod verify
7+
COPY . .
8+
RUN go build -v -o /run-app .
9+
10+
11+
FROM debian:bookworm
12+
13+
COPY --from=builder /run-app /usr/local/bin/
14+
CMD ["run-app"]

game/Makefile

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.PHONY: setup install-tools deps templ-generate build run dev clean templ server tailwind-clean tailwind-watch
2+
3+
setup: install-tools deps templ-generate
4+
@echo "✅ Setup complete!"
5+
6+
install-tools:
7+
@echo "📦 Installing development tools..."
8+
@go install github.com/a-h/templ/cmd/templ@latest
9+
@go install github.com/air-verse/air@latest
10+
11+
deps:
12+
@echo "📦 Installing dependencies..."
13+
@go mod tidy
14+
@go mod download
15+
16+
templ-generate:
17+
@echo "🎨 Generating templ files..."
18+
@templ generate
19+
20+
build: templ-generate tailwind-clean
21+
@echo "🏗️ Building application..."
22+
@go build -o bin/server .
23+
24+
run: templ-generate
25+
@echo "🚀 Running application..."
26+
@go run .
27+
28+
# Run templ generation in watch mode to detect all .templ files and
29+
# re-create _templ.txt files on change, then send reload event to browser.
30+
# Default url: http://localhost:7331
31+
templ:
32+
templ generate --watch --proxy="http://localhost:3000" --open-browser=false
33+
34+
# Run air to detect any go file changes to re-build and re-run the server.
35+
server:
36+
air \
37+
--build.cmd "go build -o tmp/bin/main ./server.go" \
38+
--build.bin "tmp/bin/main" \
39+
--build.delay "100" \
40+
--build.exclude_dir "node_modules" \
41+
--build.include_ext "go" \
42+
--build.stop_on_error "false" \
43+
--misc.clean_on_exit true
44+
45+
tailwind-clean:
46+
tailwindcss -i ./assets/css/input.css -o ./assets/css/output.css --clean
47+
48+
# Run tailwindcss to generate the styles.css bundle in watch mode.
49+
tailwind-watch:
50+
tailwindcss -i ./assets/css/input.css -o ./assets/css/output.css --watch
51+
52+
# Start development server
53+
dev:
54+
make tailwind-clean
55+
make -j3 tailwind-watch templ server
56+
57+
dev-templ:
58+
@echo "🔥 Starting development with templ watch + proxy..."
59+
@echo "🌐 Server will be available at http://localhost:3000"
60+
@echo "🔄 Hot reload proxy at http://localhost:7331"
61+
@templ generate --watch --proxy="http://localhost:3000" --cmd="go run ."
62+
63+
clean:
64+
@echo "🧹 Cleaning up..."
65+
@rm -rf tmp/
66+
@rm -rf bin/
67+
@rm -f *_templ.go
68+
@rm -f assets/css/output.css
69+
70+
start-baml:
71+
npx @boundaryml/baml dev

game/README.md

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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

Comments
 (0)