/
ferretdb.go
111 lines (95 loc) · 3.04 KB
/
ferretdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package ferretdb provides embeddable FerretDB implementation.
package ferretdb
import (
"context"
"errors"
"fmt"
"net/url"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/FerretDB/FerretDB/internal/clientconn"
"github.com/FerretDB/FerretDB/internal/handlers/registry"
"github.com/FerretDB/FerretDB/internal/util/logging"
)
// Config represents FerretDB configuration.
type Config struct {
// Handler to use; one of `pg` or `tigris` (if enabled at compile-time).
Handler string
// PostgreSQL connection string for `pg` handler.
PostgreSQLURL string
// Tigris connection string for `tigris` handler.
TigrisURL string
}
// FerretDB represents an instance of embeddable FerretDB implementation.
type FerretDB struct {
config *Config
listenAddr string
}
// New creates a new instance of embeddable FerretDB implementation.
func New(config *Config) (*FerretDB, error) {
f := &FerretDB{
config: config,
listenAddr: "127.0.0.1:27017",
}
return f, nil
}
// Run runs FerretDB until ctx is done.
//
// When this method returns, listener and all connections are closed.
func (f *FerretDB) Run(ctx context.Context) error {
newOpts := registry.NewHandlerOpts{
Ctx: context.Background(),
Logger: logger,
PostgreSQLURL: f.config.PostgreSQLURL,
TigrisURL: f.config.TigrisURL,
}
h, err := registry.NewHandler(f.config.Handler, &newOpts)
if err != nil {
return fmt.Errorf("failed to construct handler: %s", err)
}
defer h.Close()
l := clientconn.NewListener(&clientconn.NewListenerOpts{
ListenAddr: f.listenAddr,
Mode: clientconn.NormalMode,
Handler: h,
Logger: logger,
})
if err = l.Run(ctx); err != nil {
// Do not expose internal error details.
// If you need stable error values and/or types for some cases, please create an issue.
err = errors.New(err.Error())
}
return err
}
// MongoDBURI returns MongoDB URI for this FerretDB instance.
func (f *FerretDB) MongoDBURI() string {
u := url.URL{
Scheme: "mongodb",
Host: f.listenAddr,
Path: "/",
}
return u.String()
}
// logger is a global logger used by FerretDB.
//
// If it is a problem for you, please create an issue.
var logger *zap.Logger
// Initialize the global logger there to avoid creating too many issues for zap users that initialize it in their
// `main()` functions. It is still not a full solution; eventually, we should remove the usage of the global logger.
func init() {
logging.Setup(zapcore.FatalLevel)
logger = zap.L()
}