| 69 | } |
| 70 | |
| 71 | func NewCore(ctx context.Context, conf Config) (*Core, error) { |
| 72 | if conf.DefaultResponseFormat == "" { |
| 73 | conf.DefaultResponseFormat = DefaultFormat |
| 74 | } |
| 75 | if _, err := api.FormatToMediaType(conf.DefaultResponseFormat); err != nil { |
| 76 | return nil, fmt.Errorf("invalid default response format: %w", err) |
| 77 | } |
| 78 | if conf.Logger == nil { |
| 79 | conf.Logger = zap.NewNop() |
| 80 | } |
| 81 | if conf.RootContent == nil { |
| 82 | conf.RootContent = strings.NewReader(indexPage) |
| 83 | } |
| 84 | if conf.Version == "" { |
| 85 | conf.Version = "unknown" |
| 86 | } |
| 87 | |
| 88 | registry := prometheus.NewRegistry() |
| 89 | registry.MustRegister(collectors.NewGoCollector()) |
| 90 | |
| 91 | var authenticator *Auth0Authenticator |
| 92 | if conf.Auth.Enabled { |
| 93 | var err error |
| 94 | if authenticator, err = NewAuthenticator(ctx, conf.Logger, registry, conf.Auth); err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | } |
| 98 | path := conf.Root |
| 99 | if path == nil { |
| 100 | return nil, errors.New("no database root path") |
| 101 | } |
| 102 | var engine storage.Engine |
| 103 | switch storage.Scheme(path.Scheme) { |
| 104 | case storage.FileScheme: |
| 105 | engine = storage.NewLocalEngine() |
| 106 | case storage.S3Scheme: |
| 107 | engine = storage.NewRemoteEngine() |
| 108 | default: |
| 109 | return nil, fmt.Errorf("root path cannot have scheme %q", path.Scheme) |
| 110 | } |
| 111 | root, err := db.CreateOrOpen(ctx, engine, conf.Logger.Named("db"), path) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | |
| 116 | routerAux := mux.NewRouter() |
| 117 | routerAux.Use(corsMiddleware(conf.CORSAllowedOrigins)) |
| 118 | |
| 119 | routerAux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 120 | http.ServeContent(w, r, "", time.Time{}, conf.RootContent) |
| 121 | }) |
| 122 | |
| 123 | debug := routerAux.PathPrefix("/debug/pprof").Subrouter() |
| 124 | debug.HandleFunc("/cmdline", pprof.Cmdline) |
| 125 | debug.HandleFunc("/profile", pprof.Profile) |
| 126 | debug.HandleFunc("/symbol", pprof.Symbol) |
| 127 | debug.HandleFunc("/trace", pprof.Trace) |
| 128 | debug.PathPrefix("/").HandlerFunc(pprof.Index) |