2011-09-30 05:07:04 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google 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.
|
|
|
|
*/
|
|
|
|
|
2014-01-23 16:18:22 +00:00
|
|
|
// Package serverinit is responsible for mapping from a Camlistore
|
2012-11-07 16:51:42 +00:00
|
|
|
// configuration file and instantiating HTTP Handlers for all the
|
|
|
|
// necessary endpoints.
|
2014-01-23 16:18:22 +00:00
|
|
|
package serverinit
|
2011-09-30 05:07:04 +00:00
|
|
|
|
|
|
|
import (
|
2014-08-02 23:53:58 +00:00
|
|
|
"bytes"
|
2012-04-08 17:58:34 +00:00
|
|
|
"encoding/json"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"errors"
|
2013-10-05 04:24:24 +00:00
|
|
|
"expvar"
|
2011-09-30 05:07:04 +00:00
|
|
|
"fmt"
|
2013-08-27 02:07:28 +00:00
|
|
|
"io"
|
2011-09-30 05:07:04 +00:00
|
|
|
"log"
|
2014-08-18 22:20:59 +00:00
|
|
|
"net"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
2013-08-19 03:11:10 +00:00
|
|
|
"net/http/pprof"
|
2011-09-30 05:07:04 +00:00
|
|
|
"os"
|
2014-08-17 04:40:12 +00:00
|
|
|
"regexp"
|
2013-01-11 20:42:07 +00:00
|
|
|
"runtime"
|
2013-11-30 18:01:32 +00:00
|
|
|
rpprof "runtime/pprof"
|
2012-04-08 17:58:34 +00:00
|
|
|
"strconv"
|
2011-09-30 05:07:04 +00:00
|
|
|
"strings"
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/auth"
|
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/blobserver/handlers"
|
|
|
|
"camlistore.org/pkg/httputil"
|
2013-12-24 03:07:17 +00:00
|
|
|
"camlistore.org/pkg/index"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/jsonconfig"
|
2014-05-08 14:07:29 +00:00
|
|
|
"camlistore.org/pkg/server/app"
|
2014-01-23 22:40:12 +00:00
|
|
|
"camlistore.org/pkg/types/serverconfig"
|
2011-09-30 05:07:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const camliPrefix = "/camli/"
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
var ErrCamliPath = errors.New("Invalid Camlistore request path")
|
2011-09-30 05:07:04 +00:00
|
|
|
|
|
|
|
type handlerConfig struct {
|
2014-01-19 22:50:34 +00:00
|
|
|
prefix string // "/foo/"
|
|
|
|
htype string // "localdisk", etc
|
|
|
|
conf jsonconfig.Obj // never nil
|
|
|
|
internal bool // if true, not accessible over HTTP
|
2011-09-30 05:07:04 +00:00
|
|
|
|
|
|
|
settingUp, setupDone bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type handlerLoader struct {
|
2013-11-22 21:59:16 +00:00
|
|
|
installer HandlerInstaller
|
|
|
|
baseURL string
|
|
|
|
config map[string]*handlerConfig // prefix -> config
|
|
|
|
handler map[string]interface{} // prefix -> http.Handler / func / blobserver.Storage
|
|
|
|
curPrefix string
|
|
|
|
closers []io.Closer
|
|
|
|
prefixStack []string
|
2013-12-24 03:07:17 +00:00
|
|
|
reindex bool
|
2011-10-26 02:40:50 +00:00
|
|
|
|
|
|
|
// optional context (for App Engine, the first request that
|
|
|
|
// started up the process). we may need this if setting up
|
|
|
|
// handlers involves doing datastore/memcache/blobstore
|
|
|
|
// lookups.
|
|
|
|
context *http.Request
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
2012-05-13 15:51:39 +00:00
|
|
|
// A HandlerInstaller is anything that can register an HTTP Handler at
|
|
|
|
// a prefix path. Both *http.ServeMux and camlistore.org/pkg/webserver.Server
|
|
|
|
// implement HandlerInstaller.
|
2011-09-30 05:07:04 +00:00
|
|
|
type HandlerInstaller interface {
|
2012-11-07 16:51:42 +00:00
|
|
|
Handle(path string, h http.Handler)
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storageAndConfig struct {
|
|
|
|
blobserver.Storage
|
|
|
|
config *blobserver.Config
|
|
|
|
}
|
|
|
|
|
2014-01-05 01:32:08 +00:00
|
|
|
// parseCamliPath looks for "/camli/" in the path and returns
|
|
|
|
// what follows it (the action).
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func parseCamliPath(path string) (action string, err error) {
|
2011-09-30 05:07:04 +00:00
|
|
|
camIdx := strings.Index(path, camliPrefix)
|
|
|
|
if camIdx == -1 {
|
|
|
|
return "", ErrCamliPath
|
|
|
|
}
|
|
|
|
action = path[camIdx+len(camliPrefix):]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsupportedHandler(conn http.ResponseWriter, req *http.Request) {
|
|
|
|
httputil.BadRequestError(conn, "Unsupported camlistore path or method.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storageAndConfig) Config() *blobserver.Config {
|
|
|
|
return s.config
|
|
|
|
}
|
|
|
|
|
2012-12-22 21:50:55 +00:00
|
|
|
// GetStorage returns the unwrapped blobserver.Storage interface value for
|
|
|
|
// callers to type-assert optional interface implementations on. (e.g. EnumeratorConfig)
|
|
|
|
func (s *storageAndConfig) GetStorage() blobserver.Storage {
|
|
|
|
return s.Storage
|
|
|
|
}
|
|
|
|
|
2014-01-05 01:32:08 +00:00
|
|
|
// action is the part following "/camli/" in the URL. It's either a
|
|
|
|
// string like "enumerate-blobs", "stat", "upload", or a blobref.
|
|
|
|
func camliHandlerUsingStorage(req *http.Request, action string, storage blobserver.StorageConfiger) (http.Handler, auth.Operation) {
|
|
|
|
var handler http.Handler
|
2013-02-28 23:30:16 +00:00
|
|
|
op := auth.OpAll
|
2011-09-30 05:07:04 +00:00
|
|
|
switch req.Method {
|
2013-09-15 19:12:26 +00:00
|
|
|
case "GET", "HEAD":
|
2011-09-30 05:07:04 +00:00
|
|
|
switch action {
|
|
|
|
case "enumerate-blobs":
|
2014-01-05 01:32:08 +00:00
|
|
|
handler = handlers.CreateEnumerateHandler(storage)
|
2013-02-28 23:30:16 +00:00
|
|
|
op = auth.OpGet
|
2011-09-30 05:07:04 +00:00
|
|
|
case "stat":
|
2014-01-05 01:32:08 +00:00
|
|
|
handler = handlers.CreateStatHandler(storage)
|
2014-02-11 00:59:28 +00:00
|
|
|
case "ws":
|
|
|
|
handler = nil // TODO: handlers.CreateSocketHandler(storage)
|
|
|
|
op = auth.OpDiscovery // rest of operation auth checks done in handler
|
2011-09-30 05:07:04 +00:00
|
|
|
default:
|
2014-01-05 01:32:08 +00:00
|
|
|
handler = handlers.CreateGetHandler(storage)
|
2013-02-28 23:30:16 +00:00
|
|
|
op = auth.OpGet
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
case "POST":
|
|
|
|
switch action {
|
|
|
|
case "stat":
|
2014-01-05 01:32:08 +00:00
|
|
|
handler = handlers.CreateStatHandler(storage)
|
2013-02-28 23:30:16 +00:00
|
|
|
op = auth.OpStat
|
2011-09-30 05:07:04 +00:00
|
|
|
case "upload":
|
2014-01-05 01:32:08 +00:00
|
|
|
handler = handlers.CreateBatchUploadHandler(storage)
|
2013-02-28 23:30:16 +00:00
|
|
|
op = auth.OpUpload
|
2011-09-30 05:07:04 +00:00
|
|
|
case "remove":
|
2014-01-05 01:32:08 +00:00
|
|
|
handler = handlers.CreateRemoveHandler(storage)
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
2014-01-05 01:32:08 +00:00
|
|
|
case "PUT":
|
|
|
|
handler = handlers.CreatePutUploadHandler(storage)
|
|
|
|
op = auth.OpUpload
|
|
|
|
}
|
|
|
|
if handler == nil {
|
|
|
|
handler = http.HandlerFunc(unsupportedHandler)
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
2013-02-28 23:30:16 +00:00
|
|
|
return handler, op
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// where prefix is like "/" or "/s3/" for e.g. "/camli/" or "/s3/camli/*"
|
2012-12-29 14:51:42 +00:00
|
|
|
func makeCamliHandler(prefix, baseURL string, storage blobserver.Storage, hf blobserver.FindHandlerByTyper) http.Handler {
|
2011-09-30 05:07:04 +00:00
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
|
|
panic("expected prefix to end in slash")
|
|
|
|
}
|
|
|
|
baseURL = strings.TrimRight(baseURL, "/")
|
|
|
|
|
2011-10-11 01:04:20 +00:00
|
|
|
canLongPoll := true
|
|
|
|
// TODO(bradfitz): set to false if this is App Engine, or provide some way to disable
|
|
|
|
|
2011-09-30 05:07:04 +00:00
|
|
|
storageConfig := &storageAndConfig{
|
|
|
|
storage,
|
|
|
|
&blobserver.Config{
|
2012-12-29 14:51:42 +00:00
|
|
|
Writable: true,
|
|
|
|
Readable: true,
|
Get rid of QueueCreator and all its associated complexity.
Previous TODO entry was:
-- Get rid of QueueCreator entirely. Plan:
-- sync handler still has a source and dest (one pair) but
instead of calling CreateQueue on the source, it instead
has an index.Storage (configured via a RequiredObject
so it can be a kvfile, leveldb, mysql, postgres etc)
-- make all the index.Storage types be instantiable
from a jsonconfig Object, perhaps with constructors keyed
on a "type" field.
-- make sync handler support blobserver.Receiver (or StatReceiver)
like indexes, so it can receive blobs. but all it needs to
do to acknowledge the ReceiveBlob is write and flush to its
index.Storage. the syncing is async by default. (otherwise callers
could just use "replica" if they wanted sync replication).
But maybe for ease of configuration switching, we could also
support a sync mode. when it needs to replicate a blob,
it uses the source.
-- future option: sync mirror to an alternate path on ReceiveBlob
that can delete. e.g. you're uploading to s3 and google,
but don't want to upload to both at once, so you use the localdisk
as a buffer to spread out your upstream bandwidth.
-- end result: no more hardlinks or queue creator.
Change-Id: I6244fc4f3a655f08470ae3160502659399f468ed
2013-11-22 22:33:31 +00:00
|
|
|
Deletable: false,
|
2012-12-29 14:51:42 +00:00
|
|
|
URLBase: baseURL + prefix[:len(prefix)-1],
|
|
|
|
CanLongPoll: canLongPoll,
|
|
|
|
HandlerFinder: hf,
|
2011-09-30 05:07:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(func(conn http.ResponseWriter, req *http.Request) {
|
|
|
|
action, err := parseCamliPath(req.URL.Path[len(prefix)-1:])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Invalid request for method %q, path %q",
|
|
|
|
req.Method, req.URL.Path)
|
|
|
|
unsupportedHandler(conn, req)
|
|
|
|
return
|
|
|
|
}
|
2013-02-28 23:30:16 +00:00
|
|
|
handler := auth.RequireAuth(camliHandlerUsingStorage(req, action, storageConfig))
|
2014-01-05 01:32:08 +00:00
|
|
|
handler.ServeHTTP(conn, req)
|
2011-09-30 05:07:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-05-13 17:48:51 +00:00
|
|
|
func (hl *handlerLoader) FindHandlerByType(htype string) (prefix string, handler interface{}, err error) {
|
2013-10-25 05:45:28 +00:00
|
|
|
nFound := 0
|
|
|
|
for pfx, config := range hl.config {
|
2011-09-30 05:07:04 +00:00
|
|
|
if config.htype == htype {
|
2013-10-25 05:45:28 +00:00
|
|
|
nFound++
|
|
|
|
prefix, handler = pfx, hl.handler[pfx]
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-25 05:45:28 +00:00
|
|
|
if nFound == 0 {
|
|
|
|
return "", nil, blobserver.ErrHandlerTypeNotFound
|
|
|
|
}
|
|
|
|
if htype == "jsonsign" && nFound > 1 {
|
|
|
|
// TODO: do this for all handler types later? audit
|
|
|
|
// callers of FindHandlerByType and see if that's
|
|
|
|
// feasible. For now I'm only paranoid about jsonsign.
|
|
|
|
return "", nil, fmt.Errorf("%d handlers found of type %q; ambiguous", nFound, htype)
|
|
|
|
}
|
|
|
|
return
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 03:13:47 +00:00
|
|
|
func (hl *handlerLoader) AllHandlers() (types map[string]string, handlers map[string]interface{}) {
|
|
|
|
types = make(map[string]string)
|
|
|
|
handlers = make(map[string]interface{})
|
|
|
|
for pfx, config := range hl.config {
|
|
|
|
types[pfx] = config.htype
|
|
|
|
handlers[pfx] = hl.handler[pfx]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-09-30 05:07:04 +00:00
|
|
|
func (hl *handlerLoader) setupAll() {
|
|
|
|
for prefix := range hl.config {
|
|
|
|
hl.setupHandler(prefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hl *handlerLoader) configType(prefix string) string {
|
|
|
|
if h, ok := hl.config[prefix]; ok {
|
|
|
|
return h.htype
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hl *handlerLoader) getOrSetup(prefix string) interface{} {
|
|
|
|
hl.setupHandler(prefix)
|
|
|
|
return hl.handler[prefix]
|
|
|
|
}
|
|
|
|
|
2012-05-13 20:02:20 +00:00
|
|
|
func (hl *handlerLoader) MyPrefix() string {
|
|
|
|
return hl.curPrefix
|
|
|
|
}
|
|
|
|
|
2014-03-24 00:13:40 +00:00
|
|
|
func (hl *handlerLoader) BaseURL() string {
|
|
|
|
return hl.baseURL
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (hl *handlerLoader) GetStorage(prefix string) (blobserver.Storage, error) {
|
2011-09-30 05:07:04 +00:00
|
|
|
hl.setupHandler(prefix)
|
|
|
|
if s, ok := hl.handler[prefix].(blobserver.Storage); ok {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("bogus storage handler referenced as %q", prefix)
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (hl *handlerLoader) GetHandler(prefix string) (interface{}, error) {
|
2011-09-30 05:07:04 +00:00
|
|
|
hl.setupHandler(prefix)
|
|
|
|
if s, ok := hl.handler[prefix].(blobserver.Storage); ok {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
if h, ok := hl.handler[prefix].(http.Handler); ok {
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("bogus http or storage handler referenced as %q", prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hl *handlerLoader) GetHandlerType(prefix string) string {
|
|
|
|
return hl.configType(prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func exitFailure(pattern string, args ...interface{}) {
|
|
|
|
if !strings.HasSuffix(pattern, "\n") {
|
|
|
|
pattern = pattern + "\n"
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf(pattern, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hl *handlerLoader) setupHandler(prefix string) {
|
|
|
|
h, ok := hl.config[prefix]
|
|
|
|
if !ok {
|
|
|
|
exitFailure("invalid reference to undefined handler %q", prefix)
|
|
|
|
}
|
|
|
|
if h.setupDone {
|
|
|
|
// Already setup by something else reference it and forcing it to be
|
|
|
|
// setup before the bottom loop got to it.
|
|
|
|
return
|
|
|
|
}
|
2013-11-22 21:59:16 +00:00
|
|
|
hl.prefixStack = append(hl.prefixStack, prefix)
|
2011-09-30 05:07:04 +00:00
|
|
|
if h.settingUp {
|
2013-01-11 20:42:07 +00:00
|
|
|
buf := make([]byte, 1024)
|
|
|
|
buf = buf[:runtime.Stack(buf, false)]
|
2013-11-22 21:59:16 +00:00
|
|
|
exitFailure("loop in configuration graph; %q tried to load itself indirectly: %q\nStack:\n%s",
|
|
|
|
prefix, hl.prefixStack, buf)
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
h.settingUp = true
|
|
|
|
defer func() {
|
2012-11-07 21:40:17 +00:00
|
|
|
// log.Printf("Configured handler %q", prefix)
|
2011-09-30 05:07:04 +00:00
|
|
|
h.setupDone = true
|
2013-11-22 21:59:16 +00:00
|
|
|
hl.prefixStack = hl.prefixStack[:len(hl.prefixStack)-1]
|
2011-10-18 17:42:09 +00:00
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
if hl.handler[prefix] == nil {
|
|
|
|
panic(fmt.Sprintf("setupHandler for %q didn't install a handler", prefix))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(r)
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2012-05-13 20:02:20 +00:00
|
|
|
hl.curPrefix = prefix
|
|
|
|
|
2011-09-30 05:07:04 +00:00
|
|
|
if strings.HasPrefix(h.htype, "storage-") {
|
2013-08-21 20:57:28 +00:00
|
|
|
stype := strings.TrimPrefix(h.htype, "storage-")
|
2011-09-30 05:07:04 +00:00
|
|
|
// Assume a storage interface
|
|
|
|
pstorage, err := blobserver.CreateStorage(stype, hl, h.conf)
|
|
|
|
if err != nil {
|
|
|
|
exitFailure("error instantiating storage for prefix %q, type %q: %v",
|
|
|
|
h.prefix, stype, err)
|
|
|
|
}
|
2013-12-24 03:07:17 +00:00
|
|
|
if ix, ok := pstorage.(*index.Index); ok && hl.reindex {
|
|
|
|
log.Printf("Reindexing %s ...", h.prefix)
|
|
|
|
if err := ix.Reindex(); err != nil {
|
|
|
|
exitFailure("Error reindexing %s: %v", h.prefix, err)
|
|
|
|
}
|
|
|
|
}
|
2011-09-30 05:07:04 +00:00
|
|
|
hl.handler[h.prefix] = pstorage
|
2014-01-19 22:50:34 +00:00
|
|
|
if h.internal {
|
|
|
|
hl.installer.Handle(prefix, unauthorizedHandler{})
|
|
|
|
} else {
|
|
|
|
hl.installer.Handle(prefix+"camli/", makeCamliHandler(prefix, hl.baseURL, pstorage, hl))
|
|
|
|
}
|
2013-08-27 02:07:28 +00:00
|
|
|
if cl, ok := pstorage.(blobserver.ShutdownStorage); ok {
|
|
|
|
hl.closers = append(hl.closers, cl)
|
|
|
|
}
|
2011-09-30 05:07:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:07:29 +00:00
|
|
|
var hh http.Handler
|
|
|
|
if h.htype == "app" {
|
2014-06-13 21:03:49 +00:00
|
|
|
ap, err := app.NewHandler(h.conf, hl.baseURL+"/", prefix)
|
2014-05-08 14:07:29 +00:00
|
|
|
if err != nil {
|
|
|
|
exitFailure("error setting up app for prefix %q: %v", h.prefix, err)
|
|
|
|
}
|
|
|
|
hh = ap
|
2014-05-21 17:55:42 +00:00
|
|
|
auth.AddMode(ap.AuthMode())
|
2014-06-16 23:03:01 +00:00
|
|
|
if ap.ProgramName() == "publisher" {
|
|
|
|
if err := hl.initPublisherRootNode(ap); err != nil {
|
|
|
|
exitFailure("Error looking/setting up root node for publisher on %v: %v", h.prefix, err)
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 14:07:29 +00:00
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
hh, err = blobserver.CreateHandler(h.htype, hl, h.conf)
|
|
|
|
if err != nil {
|
|
|
|
exitFailure("error instantiating handler for prefix %q, type %q: %v",
|
|
|
|
h.prefix, h.htype, err)
|
|
|
|
}
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
2013-10-19 22:48:05 +00:00
|
|
|
|
2011-09-30 05:07:04 +00:00
|
|
|
hl.handler[prefix] = hh
|
2014-01-19 22:50:34 +00:00
|
|
|
var wrappedHandler http.Handler
|
|
|
|
if h.internal {
|
|
|
|
wrappedHandler = unauthorizedHandler{}
|
|
|
|
} else {
|
|
|
|
wrappedHandler = &httputil.PrefixHandler{prefix, hh}
|
2014-05-08 14:07:29 +00:00
|
|
|
if handlerTypeWantsAuth(h.htype) {
|
2014-01-19 22:50:34 +00:00
|
|
|
wrappedHandler = auth.Handler{wrappedHandler}
|
|
|
|
}
|
2011-12-11 01:18:19 +00:00
|
|
|
}
|
|
|
|
hl.installer.Handle(prefix, wrappedHandler)
|
|
|
|
}
|
|
|
|
|
2014-01-19 22:50:34 +00:00
|
|
|
type unauthorizedHandler struct{}
|
|
|
|
|
|
|
|
func (unauthorizedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:07:29 +00:00
|
|
|
func handlerTypeWantsAuth(handlerType string) bool {
|
2011-12-11 01:18:19 +00:00
|
|
|
// TODO(bradfitz): ask the handler instead? This is a bit of a
|
|
|
|
// weird spot for this policy maybe?
|
|
|
|
switch handlerType {
|
2013-06-05 17:18:27 +00:00
|
|
|
case "ui", "search", "jsonsign", "sync", "status":
|
2011-12-11 01:18:19 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 16:51:42 +00:00
|
|
|
// A Config is the wrapper around a Camlistore JSON configuration file.
|
|
|
|
// Files on disk can be in either high-level or low-level format, but
|
|
|
|
// the Load function always returns the Config in its low-level format.
|
2011-09-30 05:07:04 +00:00
|
|
|
type Config struct {
|
|
|
|
jsonconfig.Obj
|
2014-08-02 22:10:08 +00:00
|
|
|
UIPath string // Not valid until after InstallHandlers
|
2014-05-08 14:07:29 +00:00
|
|
|
|
|
|
|
// apps is the list of server apps configured during InstallHandlers,
|
|
|
|
// and that should be started after camlistored has started serving.
|
2014-06-13 21:03:49 +00:00
|
|
|
apps []*app.Handler
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
// detectConfigChange returns an informative error if conf contains obsolete keys.
|
|
|
|
func detectConfigChange(conf jsonconfig.Obj) error {
|
|
|
|
oldHTTPSKey, oldHTTPSCert := conf.OptionalString("HTTPSKeyFile", ""), conf.OptionalString("HTTPSCertFile", "")
|
|
|
|
if oldHTTPSKey != "" || oldHTTPSCert != "" {
|
|
|
|
return fmt.Errorf("Config keys %q and %q have respectively been renamed to %q and %q, please fix your server config.",
|
|
|
|
"HTTPSKeyFile", "HTTPSCertFile", "httpsKey", "httpsCert")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-02 23:53:58 +00:00
|
|
|
// LoadFile returns a low-level "handler config" from the provided filename.
|
2012-03-19 20:09:00 +00:00
|
|
|
// If the config file doesn't contain a top-level JSON key of "handlerConfig"
|
|
|
|
// with boolean value true, the configuration is assumed to be a high-level
|
|
|
|
// "user config" file, and transformed into a low-level config.
|
2014-08-02 23:53:58 +00:00
|
|
|
func LoadFile(filename string) (*Config, error) {
|
2014-08-06 17:49:27 +00:00
|
|
|
return load(filename, nil)
|
2014-08-02 23:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type jsonFileImpl struct {
|
|
|
|
*bytes.Reader
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jsonFileImpl) Close() error { return nil }
|
|
|
|
func (f jsonFileImpl) Name() string { return f.name }
|
|
|
|
|
|
|
|
// Load returns a low-level "handler config" from the provided config.
|
|
|
|
// If the config doesn't contain a top-level JSON key of "handlerConfig"
|
|
|
|
// with boolean value true, the configuration is assumed to be a high-level
|
|
|
|
// "user config" file, and transformed into a low-level config.
|
|
|
|
func Load(config []byte) (*Config, error) {
|
2014-08-06 17:49:27 +00:00
|
|
|
return load("", func(filename string) (jsonconfig.File, error) {
|
2014-08-02 23:53:58 +00:00
|
|
|
if filename != "" {
|
|
|
|
return nil, errors.New("JSON files with includes not supported with jsonconfig.Load")
|
|
|
|
}
|
|
|
|
return jsonFileImpl{bytes.NewReader(config), "config file"}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-06 17:49:27 +00:00
|
|
|
func load(filename string, opener func(filename string) (jsonconfig.File, error)) (*Config, error) {
|
2014-08-02 23:53:58 +00:00
|
|
|
c := &jsonconfig.ConfigParser{Open: opener}
|
|
|
|
m, err := c.ReadFile(filename)
|
2011-09-30 05:07:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-02 23:53:58 +00:00
|
|
|
obj := jsonconfig.Obj(m)
|
2011-09-30 05:07:04 +00:00
|
|
|
conf := &Config{
|
2014-08-02 22:10:08 +00:00
|
|
|
Obj: obj,
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
2012-03-19 20:09:00 +00:00
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
if lowLevel := obj.OptionalBool("handlerConfig", false); lowLevel {
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2014-08-06 17:49:27 +00:00
|
|
|
// Check whether the high-level config uses the old names.
|
2014-01-23 22:40:12 +00:00
|
|
|
if err := detectConfigChange(obj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-06 17:49:27 +00:00
|
|
|
// Because the original high-level config might have expanded
|
|
|
|
// through the use of functions, we re-encode the map back to
|
|
|
|
// JSON here so we can unmarshal it into the hiLevelConf
|
|
|
|
// struct later.
|
|
|
|
highExpandedJSON, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can't re-marshal high-level JSON config: %v", err)
|
2014-01-23 22:40:12 +00:00
|
|
|
}
|
2014-08-02 23:53:58 +00:00
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
var hiLevelConf serverconfig.Config
|
2014-08-06 17:49:27 +00:00
|
|
|
if err := json.Unmarshal(highExpandedJSON, &hiLevelConf); err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not unmarshal into a serverconfig.Config: %v", err)
|
2014-01-23 22:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conf, err = genLowLevelConfig(&hiLevelConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
2014-08-02 23:53:58 +00:00
|
|
|
"failed to transform user config file into internal handler configuration: %v",
|
|
|
|
err)
|
2014-01-23 22:40:12 +00:00
|
|
|
}
|
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("CAMLI_DEBUG_CONFIG")); v {
|
|
|
|
jsconf, _ := json.MarshalIndent(conf.Obj, "", " ")
|
|
|
|
log.Printf("From high-level config, generated low-level config: %s", jsconf)
|
2012-03-19 20:09:00 +00:00
|
|
|
}
|
2011-09-30 05:07:04 +00:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2012-05-13 15:46:39 +00:00
|
|
|
func (config *Config) checkValidAuth() error {
|
2011-11-16 10:41:38 +00:00
|
|
|
authConfig := config.OptionalString("auth", "")
|
2013-01-11 07:03:46 +00:00
|
|
|
mode, err := auth.FromConfig(authConfig)
|
|
|
|
if err == nil {
|
|
|
|
auth.SetMode(mode)
|
|
|
|
}
|
2011-11-16 10:41:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-05-13 15:51:39 +00:00
|
|
|
// InstallHandlers creates and registers all the HTTP Handlers needed by config
|
|
|
|
// into the provided HandlerInstaller.
|
|
|
|
//
|
|
|
|
// baseURL is required and specifies the root of this webserver, without trailing slash.
|
|
|
|
// context may be nil (used and required by App Engine only)
|
2013-08-27 02:07:28 +00:00
|
|
|
//
|
|
|
|
// The returned shutdown value can be used to cleanly shut down the
|
|
|
|
// handlers.
|
2013-12-24 03:07:17 +00:00
|
|
|
func (config *Config) InstallHandlers(hi HandlerInstaller, baseURL string, reindex bool, context *http.Request) (shutdown io.Closer, err error) {
|
2011-09-30 05:07:04 +00:00
|
|
|
defer func() {
|
2013-10-25 05:49:18 +00:00
|
|
|
if e := recover(); e != nil {
|
|
|
|
log.Printf("Caught panic installer handlers: %v", e)
|
|
|
|
err = fmt.Errorf("Caught panic: %v", e)
|
|
|
|
}
|
2011-09-30 05:07:04 +00:00
|
|
|
}()
|
|
|
|
|
2012-05-13 15:46:39 +00:00
|
|
|
if err := config.checkValidAuth(); err != nil {
|
2013-08-27 02:07:28 +00:00
|
|
|
return nil, fmt.Errorf("error while configuring auth: %v", err)
|
2011-11-16 10:41:38 +00:00
|
|
|
}
|
2011-09-30 05:07:04 +00:00
|
|
|
prefixes := config.RequiredObject("prefixes")
|
|
|
|
if err := config.Validate(); err != nil {
|
2013-08-27 02:07:28 +00:00
|
|
|
return nil, fmt.Errorf("configuration error in root object's keys: %v", err)
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
2013-11-30 18:01:32 +00:00
|
|
|
if v := os.Getenv("CAMLI_PPROF_START"); v != "" {
|
2013-12-08 02:34:13 +00:00
|
|
|
cpuf := mustCreate(v + ".cpu")
|
|
|
|
defer cpuf.Close()
|
|
|
|
memf := mustCreate(v + ".mem")
|
|
|
|
defer memf.Close()
|
|
|
|
rpprof.StartCPUProfile(cpuf)
|
2013-11-30 18:01:32 +00:00
|
|
|
defer rpprof.StopCPUProfile()
|
2013-12-08 02:34:13 +00:00
|
|
|
defer rpprof.WriteHeapProfile(memf)
|
2013-11-30 18:01:32 +00:00
|
|
|
}
|
|
|
|
|
2011-09-30 05:07:04 +00:00
|
|
|
hl := &handlerLoader{
|
2011-10-07 00:44:30 +00:00
|
|
|
installer: hi,
|
|
|
|
baseURL: baseURL,
|
|
|
|
config: make(map[string]*handlerConfig),
|
|
|
|
handler: make(map[string]interface{}),
|
2011-10-26 02:40:50 +00:00
|
|
|
context: context,
|
2013-12-24 03:07:17 +00:00
|
|
|
reindex: reindex,
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for prefix, vei := range prefixes {
|
|
|
|
if !strings.HasPrefix(prefix, "/") {
|
|
|
|
exitFailure("prefix %q doesn't start with /", prefix)
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
|
|
exitFailure("prefix %q doesn't end with /", prefix)
|
|
|
|
}
|
|
|
|
pmap, ok := vei.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
exitFailure("prefix %q value is a %T, not an object", prefix, vei)
|
|
|
|
}
|
|
|
|
pconf := jsonconfig.Obj(pmap)
|
|
|
|
enabled := pconf.OptionalBool("enabled", true)
|
|
|
|
if !enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
handlerType := pconf.RequiredString("handler")
|
|
|
|
handlerArgs := pconf.OptionalObject("handlerArgs")
|
2014-01-19 22:50:34 +00:00
|
|
|
internal := pconf.OptionalBool("internal", false)
|
2011-09-30 05:07:04 +00:00
|
|
|
if err := pconf.Validate(); err != nil {
|
|
|
|
exitFailure("configuration error in prefix %s: %v", prefix, err)
|
|
|
|
}
|
|
|
|
h := &handlerConfig{
|
2014-01-19 22:50:34 +00:00
|
|
|
prefix: prefix,
|
|
|
|
htype: handlerType,
|
|
|
|
conf: handlerArgs,
|
|
|
|
internal: internal,
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
hl.config[prefix] = h
|
|
|
|
|
|
|
|
if handlerType == "ui" {
|
|
|
|
config.UIPath = prefix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hl.setupAll()
|
2013-08-19 03:11:10 +00:00
|
|
|
|
2013-10-20 19:00:15 +00:00
|
|
|
// Now that everything is setup, run any handlers' InitHandler
|
|
|
|
// methods.
|
2014-05-08 14:07:29 +00:00
|
|
|
// And register apps that will be started later.
|
2013-10-20 19:00:15 +00:00
|
|
|
for pfx, handler := range hl.handler {
|
2014-06-13 21:03:49 +00:00
|
|
|
if starter, ok := handler.(*app.Handler); ok {
|
2014-05-08 14:07:29 +00:00
|
|
|
config.apps = append(config.apps, starter)
|
|
|
|
}
|
2013-10-20 19:00:15 +00:00
|
|
|
if in, ok := handler.(blobserver.HandlerIniter); ok {
|
|
|
|
if err := in.InitHandler(hl); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error calling InitHandler on %s: %v", pfx, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 04:24:24 +00:00
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("CAMLI_HTTP_EXPVAR")); v {
|
|
|
|
hi.Handle("/debug/vars", expvarHandler{})
|
|
|
|
}
|
2013-08-21 03:30:57 +00:00
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("CAMLI_HTTP_PPROF")); v {
|
|
|
|
hi.Handle("/debug/pprof/", profileHandler{})
|
2013-08-19 03:11:10 +00:00
|
|
|
}
|
2014-08-17 04:40:12 +00:00
|
|
|
hi.Handle("/debug/config", auth.RequireAuth(configHandler{config}, auth.OpAll))
|
2014-08-18 22:20:59 +00:00
|
|
|
hi.Handle("/debug/logs", auth.RequireAuth(http.HandlerFunc(logsHandler), auth.OpAll))
|
2013-08-27 02:07:28 +00:00
|
|
|
return multiCloser(hl.closers), nil
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:07:29 +00:00
|
|
|
// StartApps starts all the server applications that were configured
|
|
|
|
// during InstallHandlers. It should only be called after camlistored
|
|
|
|
// has started serving, since these apps might request some configuration
|
|
|
|
// from Camlistore to finish initializing.
|
|
|
|
func (config *Config) StartApps() error {
|
|
|
|
for _, ap := range config.apps {
|
|
|
|
if err := ap.Start(); err != nil {
|
2014-06-13 21:03:49 +00:00
|
|
|
return fmt.Errorf("error starting app %v: %v", ap.ProgramName(), err)
|
2014-05-08 14:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:23:14 +00:00
|
|
|
// AppURL returns a map of app name to app base URL for all the configured
|
|
|
|
// server apps.
|
|
|
|
func (config *Config) AppURL() map[string]string {
|
|
|
|
appURL := make(map[string]string, len(config.apps))
|
|
|
|
for _, ap := range config.apps {
|
|
|
|
appURL[ap.ProgramName()] = ap.BackendURL()
|
|
|
|
}
|
|
|
|
return appURL
|
|
|
|
}
|
|
|
|
|
2013-12-08 02:34:13 +00:00
|
|
|
func mustCreate(path string) *os.File {
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create %s: %v", path, err)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2013-08-27 02:07:28 +00:00
|
|
|
type multiCloser []io.Closer
|
|
|
|
|
|
|
|
func (s multiCloser) Close() (err error) {
|
|
|
|
for _, cl := range s {
|
|
|
|
if err1 := cl.Close(); err == nil && err1 != nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
2013-08-19 03:11:10 +00:00
|
|
|
|
2013-10-05 04:24:24 +00:00
|
|
|
// expvarHandler publishes expvar stats.
|
|
|
|
type expvarHandler struct{}
|
|
|
|
|
|
|
|
func (expvarHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
fmt.Fprintf(w, "{\n")
|
|
|
|
first := true
|
|
|
|
expvar.Do(func(kv expvar.KeyValue) {
|
|
|
|
if !first {
|
|
|
|
fmt.Fprintf(w, ",\n")
|
|
|
|
}
|
|
|
|
first = false
|
|
|
|
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
|
|
|
|
})
|
|
|
|
fmt.Fprintf(w, "\n}\n")
|
|
|
|
}
|
|
|
|
|
2014-08-17 04:40:12 +00:00
|
|
|
type configHandler struct {
|
|
|
|
c *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
knownKeys = regexp.MustCompile(`(?ms)^\s+"_knownkeys": {.+?},?\n`)
|
|
|
|
sensitiveLine = regexp.MustCompile(`(?m)^\s+\"(auth|aws_secret_access_key|password)\": "[^\"]+".*\n`)
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h configHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
b, _ := json.MarshalIndent(h.c.Obj, "", " ")
|
|
|
|
b = knownKeys.ReplaceAll(b, nil)
|
|
|
|
b = sensitiveLine.ReplaceAllFunc(b, func(ln []byte) []byte {
|
|
|
|
i := bytes.IndexByte(ln, ':')
|
|
|
|
return []byte(string(ln[:i+1]) + " REDACTED\n")
|
|
|
|
})
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
2013-08-21 03:30:57 +00:00
|
|
|
// profileHandler publishes server profile information.
|
|
|
|
type profileHandler struct{}
|
2013-08-19 03:11:10 +00:00
|
|
|
|
2013-08-21 03:30:57 +00:00
|
|
|
func (profileHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2013-08-19 03:11:10 +00:00
|
|
|
switch req.URL.Path {
|
|
|
|
case "/debug/pprof/cmdline":
|
|
|
|
pprof.Cmdline(rw, req)
|
|
|
|
case "/debug/pprof/profile":
|
|
|
|
pprof.Profile(rw, req)
|
|
|
|
case "/debug/pprof/symbol":
|
|
|
|
pprof.Symbol(rw, req)
|
|
|
|
default:
|
|
|
|
pprof.Index(rw, req)
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 22:20:59 +00:00
|
|
|
|
|
|
|
func logsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Dial: func(network, addr string) (net.Conn, error) {
|
|
|
|
return net.Dial("unix", "/run/camjournald.sock")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, err := c.Get("http://journal/entries")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
io.Copy(w, res.Body)
|
|
|
|
}
|