125 lines
3.3 KiB
Go
125 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"map-asset-gateway/api-go/internal/basemap"
|
|
"map-asset-gateway/api-go/internal/basemapapi"
|
|
"map-asset-gateway/api-go/internal/config"
|
|
)
|
|
|
|
func main() {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
log.Fatalf("load config: %v", err)
|
|
}
|
|
|
|
store, err := basemap.OpenStore(basemap.StoreConfig{
|
|
SQLitePath: cfg.SQLitePath,
|
|
ScanRoot: cfg.BasemapScanRoot,
|
|
VectorScanRoot: cfg.VectorScanRoot,
|
|
APIBaseURL: cfg.APIBaseURL,
|
|
TileBaseURL: cfg.TilePublicBaseURL,
|
|
VectorBaseURL: cfg.VectorPublicBaseURL,
|
|
AutoScanOnStart: cfg.AutoScanOnStart,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("open sqlite store: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := store.Close(); err != nil {
|
|
log.Printf("close store: %v", err)
|
|
}
|
|
}()
|
|
|
|
systemInfo := basemap.SystemInfo{
|
|
ServiceName: cfg.ServiceName,
|
|
ServiceVersion: cfg.ServiceVersion,
|
|
Commit: cfg.Commit,
|
|
Env: cfg.Env,
|
|
APIBaseURL: cfg.APIBaseURL,
|
|
TileBaseURL: cfg.TilePublicBaseURL,
|
|
VectorBaseURL: cfg.VectorPublicBaseURL,
|
|
SQLitePath: cfg.SQLitePath,
|
|
ScanRoot: cfg.BasemapScanRoot,
|
|
VectorScanRoot: cfg.VectorScanRoot,
|
|
StartedAt: time.Now().UTC(),
|
|
}
|
|
|
|
if cfg.AutoScanOnStart && strings.TrimSpace(cfg.BasemapScanRoot) != "" {
|
|
if _, err := store.RunScan(context.Background(), "local-basemaps"); err != nil {
|
|
log.Printf("startup scan skipped: %v", err)
|
|
} else {
|
|
log.Printf("startup scan completed for %s", cfg.BasemapScanRoot)
|
|
}
|
|
}
|
|
if cfg.AutoScanOnStart && strings.TrimSpace(cfg.VectorScanRoot) != "" {
|
|
if _, err := store.RunVectorScan(context.Background()); err != nil {
|
|
log.Printf("startup vector scan skipped: %v", err)
|
|
} else {
|
|
log.Printf("startup vector scan completed for %s", cfg.VectorScanRoot)
|
|
}
|
|
}
|
|
|
|
server, err := basemapapi.New(store, systemInfo)
|
|
if err != nil {
|
|
log.Fatalf("init api server: %v", err)
|
|
}
|
|
httpServer := &http.Server{
|
|
Addr: cfg.ListenAddr(),
|
|
Handler: server.Handler(),
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
}
|
|
cleanupPID, err := writePIDFile(strings.TrimSpace(os.Getenv("RS_API_PID_FILE")))
|
|
if err != nil {
|
|
log.Fatalf("write pid file: %v", err)
|
|
}
|
|
if cleanupPID != nil {
|
|
defer cleanupPID()
|
|
}
|
|
|
|
log.Printf("map asset gateway listening on http://%s", cfg.ListenAddr())
|
|
log.Printf("api base url: %s", cfg.APIBaseURL)
|
|
log.Printf("tile public base url: %s", cfg.TilePublicBaseURL)
|
|
log.Printf("vector public base url: %s", cfg.VectorPublicBaseURL)
|
|
log.Printf("sqlite path: %s", cfg.SQLitePath)
|
|
log.Printf("scan root: %s", cfg.BasemapScanRoot)
|
|
log.Printf("vector scan root: %s", cfg.VectorScanRoot)
|
|
|
|
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("listen and serve: %v", err)
|
|
}
|
|
}
|
|
|
|
func writePIDFile(path string) (func(), error) {
|
|
if path == "" {
|
|
return nil, nil
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pidText := strconv.Itoa(os.Getpid())
|
|
if err := os.WriteFile(path, []byte(pidText+"\n"), 0o644); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cleanup := func() {
|
|
content, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if strings.TrimSpace(string(content)) == pidText {
|
|
_ = os.Remove(path)
|
|
}
|
|
}
|
|
return cleanup, nil
|
|
}
|