2011-04-02 01:20:28 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package blobserver
|
|
|
|
|
|
|
|
import (
|
2012-05-13 17:48:51 +00:00
|
|
|
"errors"
|
2011-04-03 15:07:40 +00:00
|
|
|
"fmt"
|
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-04-02 01:20:28 +00:00
|
|
|
"sync"
|
2011-04-04 02:16:11 +00:00
|
|
|
|
2015-12-01 16:19:49 +00:00
|
|
|
"go4.org/jsonconfig"
|
2011-04-16 05:25:45 +00:00
|
|
|
)
|
2011-04-03 15:07:40 +00:00
|
|
|
|
2012-05-13 17:48:51 +00:00
|
|
|
var ErrHandlerTypeNotFound = errors.New("requested handler type not loaded")
|
|
|
|
|
2012-12-29 14:51:42 +00:00
|
|
|
type FindHandlerByTyper interface {
|
|
|
|
// FindHandlerByType finds a handler by its handlerType and
|
|
|
|
// returns its prefix and handler if it's loaded. If it's not
|
|
|
|
// loaded, the error will be ErrHandlerTypeNotFound.
|
|
|
|
//
|
2013-01-11 20:42:07 +00:00
|
|
|
// This is used by handlers to find siblings (such as the "ui" type handler)
|
2012-12-29 14:51:42 +00:00
|
|
|
// which might have more knowledge about the configuration for discovery, etc.
|
2013-01-11 20:42:07 +00:00
|
|
|
//
|
|
|
|
// Note that if this is called during handler construction
|
|
|
|
// time, only the prefix may be returned with a nil handler
|
|
|
|
// and nil err. Unlike GetHandler and GetStorage, this does
|
|
|
|
// not cause the prefix to load immediately. At runtime (after
|
|
|
|
// construction of all handlers), then prefix and handler will
|
|
|
|
// both be non-nil when err is nil.
|
2012-12-29 14:51:42 +00:00
|
|
|
FindHandlerByType(handlerType string) (prefix string, handler interface{}, err error)
|
2014-03-17 03:13:47 +00:00
|
|
|
|
|
|
|
// AllHandlers returns a map from prefix to handler type, and
|
|
|
|
// a map from prefix to handler.
|
|
|
|
AllHandlers() (map[string]string, map[string]interface{})
|
2012-12-29 14:51:42 +00:00
|
|
|
}
|
|
|
|
|
2011-05-21 20:40:17 +00:00
|
|
|
type Loader interface {
|
2012-12-29 14:51:42 +00:00
|
|
|
FindHandlerByTyper
|
|
|
|
|
2014-03-30 21:51:54 +00:00
|
|
|
// MyPrefix returns the prefix of the handler currently being constructed,
|
|
|
|
// with both leading and trailing slashes (e.g. "/ui/").
|
2012-05-13 20:02:20 +00:00
|
|
|
MyPrefix() string
|
|
|
|
|
2014-03-24 00:13:40 +00:00
|
|
|
// BaseURL returns the server's base URL, without trailing slash, and not including
|
|
|
|
// the prefix (as returned by MyPrefix).
|
|
|
|
BaseURL() string
|
|
|
|
|
2013-01-11 20:42:07 +00:00
|
|
|
// GetHandlerType returns the handler's configured type, but does
|
|
|
|
// not force it to start being loaded yet.
|
2012-05-13 17:48:51 +00:00
|
|
|
GetHandlerType(prefix string) string // returns "" if unknown
|
2011-05-30 05:52:31 +00:00
|
|
|
|
2013-01-11 20:42:07 +00:00
|
|
|
// GetHandler returns either a Storage or an http.Handler.
|
|
|
|
// It forces the handler to be loaded and returns an error if
|
|
|
|
// a cycle is created.
|
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
|
|
|
GetHandler(prefix string) (interface{}, error)
|
2011-07-08 22:05:46 +00:00
|
|
|
|
2013-01-11 20:42:07 +00:00
|
|
|
// GetStorage is like GetHandler but requires that the Handler be
|
|
|
|
// a storage Handler.
|
|
|
|
GetStorage(prefix string) (Storage, error)
|
2011-05-21 20:40:17 +00:00
|
|
|
}
|
|
|
|
|
2013-10-20 19:00:15 +00:00
|
|
|
// HandlerIniter is an optional interface which can be implemented
|
|
|
|
// by Storage or http.Handlers (from StorageConstructor or HandlerConstructor)
|
|
|
|
// to be called once all the handlers have been created.
|
|
|
|
type HandlerIniter interface {
|
|
|
|
InitHandler(FindHandlerByTyper) error
|
|
|
|
}
|
|
|
|
|
2013-07-07 23:09:17 +00:00
|
|
|
// A StorageConstructor returns a Storage implementation from a Loader
|
|
|
|
// environment and a configuration.
|
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
|
|
|
type StorageConstructor func(Loader, jsonconfig.Obj) (Storage, error)
|
2013-07-07 23:09:17 +00:00
|
|
|
|
|
|
|
// A HandlerConstructor returns an http.Handler from a Loader
|
|
|
|
// environment and a configuration.
|
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
|
|
|
type HandlerConstructor func(Loader, jsonconfig.Obj) (http.Handler, error)
|
2011-04-02 01:20:28 +00:00
|
|
|
|
|
|
|
var mapLock sync.Mutex
|
|
|
|
var storageConstructors = make(map[string]StorageConstructor)
|
2011-04-16 05:25:45 +00:00
|
|
|
var handlerConstructors = make(map[string]HandlerConstructor)
|
2011-04-02 01:20:28 +00:00
|
|
|
|
|
|
|
func RegisterStorageConstructor(typ string, ctor StorageConstructor) {
|
|
|
|
mapLock.Lock()
|
|
|
|
defer mapLock.Unlock()
|
|
|
|
if _, ok := storageConstructors[typ]; ok {
|
|
|
|
panic("blobserver: StorageConstructor already registered for type: " + typ)
|
|
|
|
}
|
|
|
|
storageConstructors[typ] = ctor
|
|
|
|
}
|
2011-04-03 15:07:40 +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 CreateStorage(typ string, loader Loader, config jsonconfig.Obj) (Storage, error) {
|
2011-04-03 15:07:40 +00:00
|
|
|
mapLock.Lock()
|
|
|
|
ctor, ok := storageConstructors[typ]
|
|
|
|
mapLock.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Storage type %q not known or loaded", typ)
|
|
|
|
}
|
2011-05-21 20:40:17 +00:00
|
|
|
return ctor(loader, config)
|
2011-04-03 15:07:40 +00:00
|
|
|
}
|
2011-04-16 05:25:45 +00:00
|
|
|
|
2013-07-07 23:09:17 +00:00
|
|
|
// RegisterHandlerConstructor registers an http Handler constructor function
|
|
|
|
// for a given handler type.
|
|
|
|
//
|
|
|
|
// It is an error to register the same handler type twice.
|
2011-05-26 05:15:55 +00:00
|
|
|
func RegisterHandlerConstructor(typ string, ctor HandlerConstructor) {
|
2011-04-16 05:25:45 +00:00
|
|
|
mapLock.Lock()
|
2011-05-26 05:15:55 +00:00
|
|
|
defer mapLock.Unlock()
|
2011-04-16 05:25:45 +00:00
|
|
|
if _, ok := handlerConstructors[typ]; ok {
|
2011-05-26 05:15:55 +00:00
|
|
|
panic("blobserver: HandlerConstrutor already registered for type: " + typ)
|
|
|
|
}
|
2011-04-16 05:25:45 +00:00
|
|
|
handlerConstructors[typ] = ctor
|
|
|
|
}
|
|
|
|
|
2013-07-07 23:09:17 +00:00
|
|
|
// CreateHandler instantiates an http Handler of type 'typ' from the
|
|
|
|
// provided JSON configuration, and finding peer handlers and
|
|
|
|
// configuration from the environment in 'loader'.
|
|
|
|
//
|
2014-09-03 22:35:20 +00:00
|
|
|
// The handler 'typ' must have been previously registered with
|
2013-07-07 23:09:17 +00:00
|
|
|
// RegisterHandlerConstructor.
|
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 CreateHandler(typ string, loader Loader, config jsonconfig.Obj) (http.Handler, error) {
|
2011-05-26 05:15:55 +00:00
|
|
|
mapLock.Lock()
|
|
|
|
ctor, ok := handlerConstructors[typ]
|
|
|
|
mapLock.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("blobserver: Handler type %q not known or loaded", typ)
|
|
|
|
}
|
|
|
|
return ctor(loader, config)
|
|
|
|
}
|