-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
199 lines (179 loc) · 5.31 KB
/
serve.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package kivikd
import (
"context"
"encoding/json"
errs "errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"sync"
"github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivikd/v4/auth"
"github.com/go-kivik/kivikd/v4/authdb"
"github.com/go-kivik/kivikd/v4/conf"
"github.com/go-kivik/kivikd/v4/internal"
"github.com/go-kivik/kivikd/v4/logger"
)
// Service defines a CouchDB-like service to serve. You will define one of these
// per server endpoint.
type Service struct {
// Client is an instance of a driver.Client, which will be served.
Client *kivik.Client
// UserStore provides access to the user database. This is passed to auth
// handlers, and is used to authenticate sessions. If unset, a nil UserStore
// will be used which authenticates all uses. PERPETUAL ADMIN PARTY!
UserStore authdb.UserStore
// AuthHandler is a slice of authentication handlers. If no auth
// handlers are configured, the server will operate as a PERPETUAL
// ADMIN PARTY!
AuthHandlers []auth.Handler
// CompatVersion is the compatibility version to report to clients. Defaults
// to 1.6.1.
CompatVersion string
// VendorVersion is the vendor version string to report to clients. Defaults to the library
// version.
VendorVersion string
// VendorName is the vendor name string to report to clients. Defaults to the library
// vendor string.
VendorName string
// Favicon is the path to a file to serve as favicon.ico. If unset, a default
// image is used.
Favicon string
// RequestLogger receives logging information for each request.
RequestLogger logger.RequestLogger
// ConfigFile is the path to a config file to read during startup.
ConfigFile string
// Config is a complete config object. If this is set, config loading is
// bypassed.
Config *conf.Conf
conf *conf.Conf
confMU sync.RWMutex
// authHandlers is a map version of AuthHandlers for easier internal
// use.
authHandlers map[string]auth.Handler
authHandlerNames []string
}
// Init initializes a configured server. This is automatically called when
// Start() is called, so this is meant to be used if you want to bind the server
// yourself.
func (s *Service) Init() (http.Handler, error) {
s.authHandlersSetup()
if err := s.loadConf(); err != nil {
return nil, err
}
if !s.Conf().IsSet("couch_httpd_auth.secret") {
fmt.Fprintf(os.Stderr, "couch_httpd_auth.secret is not set. This is insecure!\n")
}
return s.setupRoutes()
}
func (s *Service) loadConf() error {
s.confMU.Lock()
defer s.confMU.Unlock()
if s.Config != nil {
s.conf = s.Config
return nil
}
c, err := conf.Load(s.ConfigFile)
if err != nil {
return err
}
s.conf = c
return nil
}
// Conf returns the initialized server configuration.
func (s *Service) Conf() *conf.Conf {
s.confMU.RLock()
defer s.confMU.RUnlock()
if s.Config != nil {
s.confMU.RUnlock()
if err := s.loadConf(); err != nil {
panic(err)
}
s.confMU.RLock()
}
return s.conf
}
// Start begins serving connections.
func (s *Service) Start() error {
server, err := s.Init()
if err != nil {
return err
}
addr := fmt.Sprintf("%s:%d",
s.Conf().GetString("httpd.bind_address"),
s.Conf().GetInt("httpd.port"),
)
fmt.Fprintf(os.Stderr, "Listening on %s\n", addr)
return http.ListenAndServe(addr, server)
}
func (s *Service) authHandlersSetup() {
if s.AuthHandlers == nil || len(s.AuthHandlers) == 0 {
fmt.Fprintf(os.Stderr, "No AuthHandler specified! Welcome to the PERPETUAL ADMIN PARTY!\n")
}
s.authHandlers = make(map[string]auth.Handler)
s.authHandlerNames = make([]string, 0, len(s.AuthHandlers))
for _, handler := range s.AuthHandlers {
name := handler.MethodName()
if _, ok := s.authHandlers[name]; ok {
panic(fmt.Sprintf("Multiple auth handlers for for `%s` registered", name))
}
s.authHandlers[name] = handler
s.authHandlerNames = append(s.authHandlerNames, name)
}
if s.UserStore == nil {
s.UserStore = &perpetualAdminParty{}
}
}
type perpetualAdminParty struct{}
var _ authdb.UserStore = &perpetualAdminParty{}
func (p *perpetualAdminParty) Validate(ctx context.Context, username, _ string) (*authdb.UserContext, error) {
return p.UserCtx(ctx, username)
}
func (p *perpetualAdminParty) UserCtx(_ context.Context, username string) (*authdb.UserContext, error) {
return &authdb.UserContext{
Name: username,
Roles: []string{"_admin"},
}, nil
}
// Bind sets the HTTP daemon bind address and port.
func (s *Service) Bind(addr string) error {
port := addr[strings.LastIndex(addr, ":")+1:]
if _, err := strconv.Atoi(port); err != nil {
return fmt.Errorf("invalid port '%s': %w", port, err)
}
host := strings.TrimSuffix(addr, ":"+port)
s.Conf().Set("httpd.bind_address", host)
s.Conf().Set("httpd.port", port)
return nil
}
const (
typeJSON = "application/json"
// typeText = "text/plain"
typeForm = "application/x-www-form-urlencoded"
// typeMForm = "multipart/form-data"
)
func reason(err error) string {
kerr := new(internal.Error)
if errs.As(err, &kerr) {
return kerr.Message
}
return err.Error()
}
func reportError(w http.ResponseWriter, err error) {
w.Header().Add("Content-Type", typeJSON)
status := kivik.HTTPStatus(err)
w.WriteHeader(status)
short := err.Error()
reason := reason(err)
if reason == "" {
reason = short
} else {
short = strings.ToLower(http.StatusText(status))
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"error": short,
"reason": reason,
})
}