...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package couchserver
16
17 import (
18 "context"
19 "log"
20 "net/http"
21
22 "github.com/go-chi/chi"
23
24 "github.com/go-kivik/kivik/v4"
25 )
26
27 const (
28 typeJSON = "application/json"
29 )
30
31 type db interface {
32 Stats(context.Context) (*kivik.DBStats, error)
33 Flush(context.Context) error
34 }
35
36 type backend interface {
37 AllDBs(context.Context, ...kivik.Option) ([]string, error)
38 CreateDB(context.Context, string, ...kivik.Option) error
39 DB(context.Context, string, ...kivik.Option) (db, error)
40 DBExists(context.Context, string, ...kivik.Option) (bool, error)
41 }
42
43 type clientWrapper struct {
44 *kivik.Client
45 }
46
47 var _ backend = &clientWrapper{}
48
49 func (c *clientWrapper) DB(_ context.Context, dbName string, options ...kivik.Option) (db, error) {
50 db := c.Client.DB(dbName, options...)
51 return db, db.Err()
52 }
53
54
55 type Handler struct {
56 client backend
57
58
59 CompatVersion string
60
61
62 Vendor string
63
64
65 VendorVersion string
66 Logger *log.Logger
67
68 Favicon string
69
70 SessionKey interface{}
71 }
72
73
74 func NewHandler(client *kivik.Client) *Handler {
75 return &Handler{client: &clientWrapper{client}}
76 }
77
78
79 const CompatVersion = "0.0.0"
80
81 func (h *Handler) vendor() (compatVer, vend, ver string) {
82 if h.CompatVersion == "" {
83 compatVer = CompatVersion
84 } else {
85 compatVer = h.CompatVersion
86 }
87 if h.Vendor == "" {
88 vend = "Kivik"
89 } else {
90 vend = h.Vendor
91 }
92 if h.VendorVersion == "" {
93 ver = kivik.Version
94 } else {
95 ver = h.VendorVersion
96 }
97 return compatVer, vend, ver
98 }
99
100
101 func (h *Handler) Main() http.Handler {
102 r := chi.NewRouter()
103 r.Get("/", h.GetRoot())
104 r.Get("/favicon.ico", h.GetFavicon())
105 r.Get("/_all_dbs", h.GetAllDBs())
106 r.Get("/{db}", h.GetDB())
107 r.Put("/{db}", h.PutDB())
108 r.Head("/{db}", h.HeadDB())
109 r.Post("/{db}/_ensure_full_commit", h.Flush())
110 r.Get("/_session", h.GetSession())
111 return r
112 }
113
114 type serverInfo struct {
115 CouchDB string `json:"couchdb"`
116 Version string `json:"version"`
117 Vendor vendorInfo `json:"vendor"`
118 }
119
120 type vendorInfo struct {
121 Name string `json:"name"`
122 Version string `json:"version"`
123 }
124
View as plain text