30 lines
816 B
Go
30 lines
816 B
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"lingniu/vehicle-data-platform/apps/api/internal/config"
|
|
"lingniu/vehicle-data-platform/apps/api/internal/platform"
|
|
"lingniu/vehicle-data-platform/apps/api/internal/static"
|
|
)
|
|
|
|
func NewServer(cfg config.Config) http.Handler {
|
|
var store platform.Store = platform.NewMockStore()
|
|
if cfg.MySQLDSN != "" {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
db, err := platform.OpenMySQL(ctx, cfg.MySQLDSN)
|
|
if err != nil {
|
|
log.Printf("production mysql store disabled: %v", err)
|
|
} else {
|
|
store = platform.NewProductionStore(db, cfg.TDengineDatabase)
|
|
log.Printf("production mysql store enabled")
|
|
}
|
|
}
|
|
api := platform.NewHandler(platform.NewService(store))
|
|
return static.Handler(cfg.StaticDir, api)
|
|
}
|