Files
ilaandClaude Opus 4.8 6e30daceab feat(T-001): 初始化 go 模块 + go-app 最小可运行页面
- go.mod:module lingo,依赖 go-app v10.1.11
- main.go:go-app 装配入口;init() 注册路由(Handler 仅为已注册路由返回
  app shell),newHandler() 抽出便于测试,main() 起 http 服务
- main_test.go:in-process httptest 冒烟测试,验证根路径 200 +
  home 组件预渲染 + wasm 引导脚本注入
- .gitignore:忽略 /bin/ 构建产物
- 06-tasks:T-001 标 DONE;current-state 更新为 Phase 0 进行中、下一任务 T-002

验证:gofmt 干净 · go vet 通过 · go test 通过 · GOOS=js GOARCH=wasm go build 通过 · go build 通过

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 19:45:13 +08:00

71 lines
2.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 走遍美国 · 英语精听 PWA —— go-app 装配入口(T-001 最小可运行骨架)。
//
// 同一份代码两处运行:
// - 编译成 wasm 在浏览器跑(app.RunWhenOnBrowser)
// - 编译成普通二进制在服务器跑(http.Server + app.Handler)
//
// 构建:
//
// GOOS=js GOARCH=wasm go build -o web/app.wasm # 前端
// go build -o bin/lingo # 后端
package main
import (
"log"
"net/http"
"github.com/maxence-charriere/go-app/v10/pkg/app"
)
// home 是根页面组件,目前仅是最小占位骨架。
type home struct {
app.Compo
}
// Render 渲染最小首页,验证 go-app 骨架可运行。
func (h *home) Render() app.UI {
return app.Div().
Style("font-family", "system-ui, -apple-system, sans-serif").
Style("max-width", "440px").
Style("margin", "0 auto").
Style("padding", "3rem 1.5rem").
Body(
app.H1().Text("走遍美国 · 精听"),
app.P().
Style("color", "#6b6b6b").
Text("Family Album, U.S.A. — go-app 骨架已就绪(T-001)。"),
)
}
// init 注册前端路由。放在 init 而非 main,确保服务器(渲染 app shell)、
// 浏览器 wasm 入口、以及测试三处都能看到路由——go-app 的 Handler 只为
// 已注册的路由返回 app shell,未注册的路径会 404。
func init() {
app.Route("/", func() app.Composer { return &home{} })
}
// newHandler 构造服务器端的 go-app Handler,提供 app shell、wasm 与静态资源
// (默认从 web/ 目录读取 app.wasm)。抽出为函数以便测试。
func newHandler() *app.Handler {
return &app.Handler{
Name: "走遍美国精听",
ShortName: "精听",
Description: "基于「走遍美国」的英语精听 PWA",
Lang: "zh",
}
}
func main() {
// 浏览器侧:启动 go-app(此调用在 wasm 之外无副作用)。
app.RunWhenOnBrowser()
// 服务器侧。
http.Handle("/", newHandler())
const addr = ":8000"
log.Printf("serving on http://localhost%s", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatal(err)
}
}