Implement Phase 6: REST API with chi router

- Login endpoint (password → bearer token + session cookie)
- Auth middleware (bearer header or session cookie)
- Notebook list endpoint (authenticated)
- Page SVG/JPG rendering endpoints (authenticated)
- Notebook PDF download endpoint (authenticated)
- Share link endpoints: view, page SVG, page JPG, PDF (no auth)
- Route registration with chi groups

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 19:55:47 -07:00
parent 5993d20995
commit 37c2d35ceb
7 changed files with 482 additions and 0 deletions

44
internal/server/server.go Normal file
View File

@@ -0,0 +1,44 @@
package server
import (
"crypto/tls"
"database/sql"
"fmt"
"net/http"
"time"
"github.com/go-chi/chi/v5"
)
type Config struct {
Addr string
TLSCert string
TLSKey string
DB *sql.DB
BaseURL string
}
func Start(cfg Config) error {
r := chi.NewRouter()
RegisterRoutes(r, cfg.DB, cfg.BaseURL)
tlsCert, err := tls.LoadX509KeyPair(cfg.TLSCert, cfg.TLSKey)
if err != nil {
return fmt.Errorf("load TLS cert: %w", err)
}
srv := &http.Server{
Addr: cfg.Addr,
Handler: r,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{tlsCert},
MinVersion: tls.VersionTLS13,
},
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}
fmt.Printf("REST API listening on %s\n", cfg.Addr)
return srv.ListenAndServeTLS("", "")
}