-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchserver.go
108 lines (92 loc) · 2.6 KB
/
couchserver.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
package couchserver
import (
"context"
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/go-kivik/kivik/v4"
)
const (
typeJSON = "application/json"
)
type db interface {
Stats(context.Context) (*kivik.DBStats, error)
Flush(context.Context) error
}
type backend interface {
AllDBs(context.Context, ...kivik.Option) ([]string, error)
CreateDB(context.Context, string, ...kivik.Option) error
DB(context.Context, string, ...kivik.Option) (db, error)
DBExists(context.Context, string, ...kivik.Option) (bool, error)
}
type clientWrapper struct {
*kivik.Client
}
var _ backend = &clientWrapper{}
func (c *clientWrapper) DB(_ context.Context, dbName string, options ...kivik.Option) (db, error) {
db := c.Client.DB(dbName, options...)
return db, db.Err()
}
// Handler is a CouchDB server handler.
type Handler struct {
client backend
// CompatVersion is the CouchDB compatibility version to report. If unset,
// defaults to the CompatVersion constant/.
CompatVersion string
// Vendor is the vendor name to report. If unset, defaults to the
// kivik.Vendor constant.
Vendor string
// VendorVersion is the vendor version to report. If unset, defaults to the
// kivik.VendorVersion constant.
VendorVersion string
Logger *log.Logger
// Favicon is the path to a favicon.ico to serve.
Favicon string
// SessionKey is a temporary solution to avoid import cycles. Soon I will move the key to another package.
SessionKey interface{}
}
func NewHandler(client *kivik.Client) *Handler {
return &Handler{client: &clientWrapper{client}}
}
// CompatVersion is the default CouchDB compatibility provided by this package.
const CompatVersion = "0.0.0"
func (h *Handler) vendor() (compatVer, vend, ver string) {
if h.CompatVersion == "" {
compatVer = CompatVersion
} else {
compatVer = h.CompatVersion
}
if h.Vendor == "" {
vend = "Kivik"
} else {
vend = h.Vendor
}
if h.VendorVersion == "" {
ver = kivik.Version
} else {
ver = h.VendorVersion
}
return compatVer, vend, ver
}
// Main returns an http.Handler to handle all CouchDB endpoints.
func (h *Handler) Main() http.Handler {
r := chi.NewRouter()
r.Get("/", h.GetRoot())
r.Get("/favicon.ico", h.GetFavicon())
r.Get("/_all_dbs", h.GetAllDBs())
r.Get("/{db}", h.GetDB())
r.Put("/{db}", h.PutDB())
r.Head("/{db}", h.HeadDB())
r.Post("/{db}/_ensure_full_commit", h.Flush())
r.Get("/_session", h.GetSession())
return r
}
type serverInfo struct {
CouchDB string `json:"couchdb"`
Version string `json:"version"`
Vendor vendorInfo `json:"vendor"`
}
type vendorInfo struct {
Name string `json:"name"`
Version string `json:"version"`
}