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.
|
|
|
|
*/
|
|
|
|
|
2012-11-07 16:51:42 +00:00
|
|
|
// Package serverconfig is responsible for mapping from a Camlistore
|
|
|
|
// configuration file and instantiating HTTP Handlers for all the
|
|
|
|
// necessary endpoints.
|
2011-09-30 05:07:04 +00:00
|
|
|
package serverconfig
|
|
|
|
|
|
|
|
import (
|
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"
|
2011-09-30 05:07:04 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
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"
|
2011-09-30 05:07:04 +00:00
|
|
|
"os"
|
2013-01-11 20:42:07 +00:00
|
|
|
"runtime"
|
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"
|
2012-12-29 14:51:42 +00:00
|
|
|
"camlistore.org/pkg/blobserver/gethandler"
|
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/blobserver/handlers"
|
|
|
|
"camlistore.org/pkg/httputil"
|
|
|
|
"camlistore.org/pkg/jsonconfig"
|
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 {
|
|
|
|
prefix string // "/foo/"
|
|
|
|
htype string // "localdisk", etc
|
|
|
|
conf jsonconfig.Obj // never nil
|
|
|
|
|
|
|
|
settingUp, setupDone bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type handlerLoader struct {
|
|
|
|
installer HandlerInstaller
|
2011-10-07 00:44:30 +00:00
|
|
|
baseURL string
|
|
|
|
config map[string]*handlerConfig // prefix -> config
|
|
|
|
handler map[string]interface{} // prefix -> http.Handler / func / blobserver.Storage
|
2012-05-13 20:02:20 +00:00
|
|
|
curPrefix string
|
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
|
|
|
|
}
|
|
|
|
|
2011-10-07 00:44:30 +00:00
|
|
|
var _ blobserver.ContextWrapper = (*storageAndConfig)(nil)
|
|
|
|
|
2011-10-13 05:29:42 +00:00
|
|
|
func (sc *storageAndConfig) WrapContext(req *http.Request) blobserver.Storage {
|
2011-10-07 00:44:30 +00:00
|
|
|
if w, ok := sc.Storage.(blobserver.ContextWrapper); ok {
|
2011-10-10 01:41:54 +00:00
|
|
|
return &storageAndConfig{w.WrapContext(req), sc.config}
|
2011-10-07 00:44:30 +00:00
|
|
|
}
|
2011-10-13 05:29:42 +00:00
|
|
|
return sc
|
2011-10-07 00:44:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-02-28 23:30:16 +00:00
|
|
|
func camliHandlerUsingStorage(req *http.Request, action string, storage blobserver.StorageConfiger) (func(http.ResponseWriter, *http.Request), auth.Operation) {
|
2011-09-30 05:07:04 +00:00
|
|
|
handler := unsupportedHandler
|
2013-02-28 23:30:16 +00:00
|
|
|
op := auth.OpAll
|
2011-09-30 05:07:04 +00:00
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
switch action {
|
|
|
|
case "enumerate-blobs":
|
2013-02-28 23:30:16 +00:00
|
|
|
handler = handlers.CreateEnumerateHandler(storage)
|
|
|
|
op = auth.OpGet
|
2011-09-30 05:07:04 +00:00
|
|
|
case "stat":
|
2013-02-28 23:30:16 +00:00
|
|
|
handler = handlers.CreateStatHandler(storage)
|
2011-09-30 05:07:04 +00:00
|
|
|
default:
|
2012-12-29 14:51:42 +00:00
|
|
|
handler = gethandler.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":
|
2013-02-28 23:30:16 +00:00
|
|
|
handler = handlers.CreateStatHandler(storage)
|
|
|
|
op = auth.OpStat
|
2011-09-30 05:07:04 +00:00
|
|
|
case "upload":
|
2013-02-28 23:30:16 +00:00
|
|
|
handler = handlers.CreateUploadHandler(storage)
|
|
|
|
op = auth.OpUpload
|
2011-09-30 05:07:04 +00:00
|
|
|
case "remove":
|
2013-02-28 23:30:16 +00:00
|
|
|
handler = handlers.CreateRemoveHandler(storage)
|
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,
|
|
|
|
IsQueue: false,
|
|
|
|
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))
|
|
|
|
handler(conn, req)
|
2011-09-30 05:07:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2011-10-26 02:40:50 +00:00
|
|
|
func (hl *handlerLoader) GetRequestContext() (req *http.Request, ok bool) {
|
|
|
|
return hl.context, hl.context != nil
|
|
|
|
}
|
|
|
|
|
2012-05-13 17:48:51 +00:00
|
|
|
func (hl *handlerLoader) FindHandlerByType(htype string) (prefix string, handler interface{}, err error) {
|
2011-09-30 05:07:04 +00:00
|
|
|
for prefix, config := range hl.config {
|
|
|
|
if config.htype == htype {
|
|
|
|
return prefix, hl.handler[prefix], nil
|
|
|
|
}
|
|
|
|
}
|
2012-05-13 17:48:51 +00:00
|
|
|
return "", nil, blobserver.ErrHandlerTypeNotFound
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if h.settingUp {
|
2013-01-11 20:42:07 +00:00
|
|
|
buf := make([]byte, 1024)
|
|
|
|
buf = buf[:runtime.Stack(buf, false)]
|
|
|
|
exitFailure("loop in configuration graph; %q tried to load itself indirectly. Stack:\n%s", prefix, 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
|
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-") {
|
|
|
|
stype := h.htype[len("storage-"):]
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
hl.handler[h.prefix] = pstorage
|
2012-12-29 14:51:42 +00:00
|
|
|
hl.installer.Handle(prefix+"camli/", makeCamliHandler(prefix, hl.baseURL, pstorage, hl))
|
2011-09-30 05:07:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
hl.handler[prefix] = hh
|
2011-12-11 01:18:19 +00:00
|
|
|
var wrappedHandler http.Handler = &httputil.PrefixHandler{prefix, hh}
|
|
|
|
if handerTypeWantsAuth(h.htype) {
|
|
|
|
wrappedHandler = auth.Handler{wrappedHandler}
|
|
|
|
}
|
|
|
|
hl.installer.Handle(prefix, wrappedHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handerTypeWantsAuth(handlerType string) bool {
|
|
|
|
// 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
|
2011-10-07 00:44:30 +00:00
|
|
|
UIPath string // Not valid until after InstallHandlers
|
|
|
|
configPath string // Filesystem path
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
|
2012-03-19 20:09:00 +00:00
|
|
|
// Load returns a low-level "handler config" from the provided filename.
|
|
|
|
// 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.
|
|
|
|
func Load(filename string) (*Config, error) {
|
|
|
|
obj, err := jsonconfig.ReadFile(filename)
|
2011-09-30 05:07:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conf := &Config{
|
2011-10-07 00:44:30 +00:00
|
|
|
Obj: obj,
|
2012-03-19 20:09:00 +00:00
|
|
|
configPath: filename,
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
2012-03-19 20:09:00 +00:00
|
|
|
|
|
|
|
if lowLevel := obj.OptionalBool("handlerConfig", false); !lowLevel {
|
2012-11-07 16:51:42 +00:00
|
|
|
conf, err = genLowLevelConfig(conf)
|
2012-03-19 20:09:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Failed to transform user config file %q into internal handler configuration: %v",
|
|
|
|
filename, err)
|
|
|
|
}
|
2012-04-08 17:58:34 +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)
|
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 (config *Config) InstallHandlers(hi HandlerInstaller, baseURL string, context *http.Request) (outerr error) {
|
2011-09-30 05:07:04 +00:00
|
|
|
defer func() {
|
2012-11-07 16:51:42 +00:00
|
|
|
if err := recover(); err != nil {
|
|
|
|
outerr = fmt.Errorf("%v", err)
|
2011-09-30 05:07:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2012-05-13 15:46:39 +00:00
|
|
|
if err := config.checkValidAuth(); err != nil {
|
2011-11-16 10:41:38 +00:00
|
|
|
return fmt.Errorf("error while configuring auth: %v", err)
|
|
|
|
}
|
2011-09-30 05:07:04 +00:00
|
|
|
prefixes := config.RequiredObject("prefixes")
|
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("configuration error in root object's keys: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
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")
|
|
|
|
if err := pconf.Validate(); err != nil {
|
|
|
|
exitFailure("configuration error in prefix %s: %v", prefix, err)
|
|
|
|
}
|
|
|
|
h := &handlerConfig{
|
|
|
|
prefix: prefix,
|
|
|
|
htype: handlerType,
|
|
|
|
conf: handlerArgs,
|
|
|
|
}
|
|
|
|
hl.config[prefix] = h
|
|
|
|
|
|
|
|
if handlerType == "ui" {
|
|
|
|
config.UIPath = prefix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hl.setupAll()
|
|
|
|
return nil
|
|
|
|
}
|