Files
eng-pad-server/internal/server/server.go
Kyle Isom a9e6ca022e Add structured logging with log/slog
Replace fmt.Printf logging calls with slog.Info/slog.Error for structured
JSON output to stderr. Add internal/log package to initialize the default
slog handler from the config log level. Fix .gitignore to only ignore the
binary at the repo root, not the cmd/eng-pad-server directory.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 20:52:22 -07:00

51 lines
1.0 KiB
Go

package server
import (
"crypto/tls"
"database/sql"
"fmt"
"log/slog"
"net/http"
"time"
"github.com/go-chi/chi/v5"
)
type Config struct {
Addr string
TLSCert string
TLSKey string
DB *sql.DB
BaseURL string
}
// Start creates and starts the REST API server. It returns the *http.Server
// so the caller can manage graceful shutdown. The server runs in a background
// goroutine.
func Start(cfg Config) (*http.Server, error) {
r := chi.NewRouter()
RegisterRoutes(r, cfg.DB, cfg.BaseURL)
tlsCert, err := tls.LoadX509KeyPair(cfg.TLSCert, cfg.TLSKey)
if err != nil {
return nil, 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,
}
slog.Info("REST API started", "addr", cfg.Addr)
go func() { _ = srv.ListenAndServeTLS("", "") }()
return srv, nil
}