0b93a985d6
Read dev/issues/*.md and dev/flows/*.md as kanban cards via new
/api/boards/{issues|flows}/cards endpoints. PATCH writes status back
to the frontmatter atomically (tmp + rename), POST .../launch proxies
to agent_runner_api.
- issues_source.go: scan + parse frontmatter (yaml.v3) into IssueCard.
Skips README/INDEX/AGENT_GUIDE. Malformed YAML yields parse-error
cards (no crash). Description = first 5 body lines (no full body).
- flows_source.go: same shape, distinct status->column mapping
(pending/running/done/deferred -> Pending/Running/Done/Deferred).
- frontmatter_edit.go: PatchFrontmatterField — atomic, preserves the
rest of the file byte-for-byte, inserts key if missing.
- handlers_boards.go: list + patch + launch endpoints, taxonomy 0103
enforced. Cache 30s in memory, thread-safe (mutex), invalidated on
PATCH. Launch returns 502 with suggestion when runner is down.
- main.go: SkipPaths += "/api/boards/" so the C++ frontend hits the
read endpoints without a kanban_web session.
Smoke (FN_REGISTRY_ROOT pointed at the worktree, 87 issues + 9 flows
on disk):
GET /api/boards/issues/cards -> 200, 87 cards
GET /api/boards/flows/cards -> 200, 9 cards
PATCH /api/boards/issues/cards/0119 {status:en-curso} -> 200,
file mtime changes, frontmatter rewritten, rest preserved
POST /api/boards/issues/cards/0119/launch -> 502
agent_runner_unreachable (expected, runner not yet implemented)
Tests: issues_source_test (3 cases incl. malformed + missing status),
flows_source_test (3 cases), frontmatter_edit_test (4 cases incl.
atomic rename + no tmp leftovers). Pre-existing tools_test failure
on TestExecuteTool_MoveCard_BetweenColumns_OpensHistory is unrelated
(CardHistoryResponse type assert, not touched here).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
168 lines
4.3 KiB
Go
168 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"flag"
|
|
"fmt"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"fn-registry/functions/infra"
|
|
)
|
|
|
|
//go:embed all:dist
|
|
var frontendDist embed.FS
|
|
|
|
func main() {
|
|
// Subcommand `kanban mcp` runs as MCP server over stdio (spawned by claude -p).
|
|
if len(os.Args) > 1 && os.Args[1] == "mcp" {
|
|
if err := runMCPServer(os.Args[2:]); err != nil {
|
|
fmt.Fprintf(os.Stderr, "kanban mcp: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
|
|
flags := flag.NewFlagSet("kanban", flag.ExitOnError)
|
|
port := flags.Int("port", 8403, "HTTP port")
|
|
dbPath := flags.String("db", "operations.db", "SQLite database path")
|
|
initialAdmin := flags.String("initial-admin", os.Getenv("KANBAN_INITIAL_ADMIN"), "Bootstrap admin in user:pass form (only if no users yet)")
|
|
flagsPath := flags.String("flags", "dev/feature_flags.json", "Feature flags JSON path (missing file → all disabled)")
|
|
flags.Parse(os.Args[1:])
|
|
|
|
featureFlags, err := loadFeatureFlags(*flagsPath)
|
|
if err != nil {
|
|
log.Fatalf("load feature flags: %v", err)
|
|
}
|
|
for name, fl := range featureFlags.Flags {
|
|
log.Printf("feature flag %q enabled=%v", name, fl.Enabled)
|
|
}
|
|
|
|
db, err := openDB(*dbPath)
|
|
if err != nil {
|
|
log.Fatalf("open db: %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
bootstrapAdmin(db, *initialAdmin)
|
|
startSessionCleanup(db)
|
|
|
|
internalToken := os.Getenv("KANBAN_INTERNAL_TOKEN")
|
|
if internalToken == "" {
|
|
internalToken = generateInternalToken()
|
|
}
|
|
|
|
wd := chatWorkdir(*dbPath)
|
|
logger := newChatLogger(filepath.Join(wd, "chat.log"))
|
|
log.Printf("chat tool log: %s", logger.path)
|
|
mux := infra.HTTPRouter(apiRoutes(db, wd, logger, internalToken, &featureFlags))
|
|
|
|
feHandler := frontendHandler()
|
|
if feHandler != nil {
|
|
mux.Handle("/", feHandler)
|
|
log.Printf("serving frontend from embedded dist/")
|
|
} else {
|
|
log.Printf("no frontend build found, API-only mode")
|
|
}
|
|
|
|
authMW := infra.HTTPSessionCookieMiddleware(infra.SessionCookieConfig{
|
|
DB: db.conn,
|
|
CookieName: cookieName,
|
|
SkipPaths: []string{"/api/auth/", "/api/tool/", "/api/flags", "/api/boards/", "/health", "/assets/", "/index.html"},
|
|
UserCtxKey: userCtxKey,
|
|
})
|
|
|
|
chain := infra.HTTPMiddlewareChain(
|
|
infra.HTTPLoggerMiddleware(os.Stdout),
|
|
infra.HTTPCORSMiddleware([]string{"*"}, []string{"GET", "POST", "PATCH", "DELETE", "OPTIONS"}),
|
|
apiOnlyAuth(authMW),
|
|
)
|
|
handler := chain(mux)
|
|
|
|
addr := fmt.Sprintf(":%d", *port)
|
|
log.Printf("kanban server starting on http://0.0.0.0%s", addr)
|
|
log.Printf("database: %s", *dbPath)
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer cancel()
|
|
|
|
if err := infra.HTTPServe(addr, handler, ctx); err != nil {
|
|
log.Fatalf("server: %v", err)
|
|
}
|
|
}
|
|
|
|
// apiOnlyAuth applies auth middleware only to /api/* paths so the SPA shell
|
|
// can be served without a session (the SPA itself handles login UI).
|
|
func apiOnlyAuth(mw infra.Middleware) infra.Middleware {
|
|
return func(next http.Handler) http.Handler {
|
|
gated := mw(next)
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.HasPrefix(r.URL.Path, "/api/") {
|
|
gated.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|
|
|
|
func bootstrapAdmin(db *DB, spec string) {
|
|
spec = strings.TrimSpace(spec)
|
|
if spec == "" {
|
|
return
|
|
}
|
|
count, err := db.CountUsers()
|
|
if err != nil {
|
|
log.Printf("bootstrap admin: count users: %v", err)
|
|
return
|
|
}
|
|
if count > 0 {
|
|
return
|
|
}
|
|
parts := strings.SplitN(spec, ":", 2)
|
|
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
|
log.Printf("bootstrap admin: invalid spec, expected user:pass")
|
|
return
|
|
}
|
|
u, err := db.CreateUser(parts[0], parts[1], parts[0])
|
|
if err != nil {
|
|
log.Printf("bootstrap admin: %v", err)
|
|
return
|
|
}
|
|
log.Printf("bootstrap admin: created user %q", u.Username)
|
|
}
|
|
|
|
func startSessionCleanup(db *DB) {
|
|
go func() {
|
|
t := time.NewTicker(1 * time.Hour)
|
|
defer t.Stop()
|
|
for range t.C {
|
|
if n, err := infra.SessionCleanup(db.conn); err != nil {
|
|
log.Printf("session cleanup: %v", err)
|
|
} else if n > 0 {
|
|
log.Printf("session cleanup: purged %d expired", n)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func frontendHandler() http.Handler {
|
|
sub, err := fs.Sub(frontendDist, "dist")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
entries, _ := fs.ReadDir(sub, ".")
|
|
if len(entries) == 0 {
|
|
return nil
|
|
}
|
|
return infra.SPAHandler(sub, "index.html")
|
|
}
|