2012-03-26 18:11:57 +00:00
|
|
|
// +build appengine
|
|
|
|
|
2011-09-30 05:25:21 +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 appengine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-01-08 01:20:38 +00:00
|
|
|
"net/http"
|
2011-10-13 06:50:52 +00:00
|
|
|
"sync"
|
2011-09-30 05:25:21 +00:00
|
|
|
|
2011-10-17 14:53:30 +00:00
|
|
|
"appengine"
|
|
|
|
|
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" // storage interface definition
|
|
|
|
"camlistore.org/pkg/serverconfig" // wiring up the world from a JSON description
|
2012-03-20 02:04:41 +00:00
|
|
|
_ "camlistore.org/pkg/blobserver/cond"
|
|
|
|
_ "camlistore.org/pkg/blobserver/replica"
|
|
|
|
_ "camlistore.org/pkg/blobserver/shard"
|
|
|
|
_ "camlistore.org/pkg/server" // handlers: UI, publish, thumbnailing, etc
|
2011-10-19 02:14:42 +00:00
|
|
|
|
|
|
|
// TODO(bradfitz): uncomment these and deal with symlinks + config setup
|
|
|
|
// Both require an App Engine context to make HTTP requests too.
|
2012-03-20 02:04:41 +00:00
|
|
|
//_ "camlistore.org/pkg/blobserver/remote"
|
|
|
|
//_ "camlistore.org/pkg/blobserver/s3"
|
2011-10-05 21:34:55 +00:00
|
|
|
)
|
2011-09-30 05:25:21 +00:00
|
|
|
|
2011-10-13 06:50:52 +00:00
|
|
|
// lazyInit is our root handler for App Engine. We don't have an App Engine
|
|
|
|
// context until the first request and we need that context to figure out
|
|
|
|
// our serving URL. So we use this to defer setting up our environment until
|
|
|
|
// the first request.
|
|
|
|
type lazyInit struct {
|
2011-10-17 14:53:30 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
ready bool
|
|
|
|
mux *http.ServeMux
|
2011-10-13 06:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (li *lazyInit) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2011-10-17 14:53:30 +00:00
|
|
|
li.mu.Lock()
|
|
|
|
if !li.ready {
|
|
|
|
li.ready = realInit(w, r)
|
|
|
|
}
|
|
|
|
li.mu.Unlock()
|
|
|
|
if li.ready {
|
|
|
|
li.mux.ServeHTTP(w, r)
|
|
|
|
}
|
2011-10-13 06:50:52 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:53:30 +00:00
|
|
|
var root = new(lazyInit)
|
2011-10-13 06:50:52 +00:00
|
|
|
|
2011-09-30 05:25:21 +00:00
|
|
|
func init() {
|
2011-10-19 02:14:42 +00:00
|
|
|
// TODO(bradfitz): rename some of this to be consistent
|
2011-10-17 14:53:30 +00:00
|
|
|
blobserver.RegisterStorageConstructor("appengine", blobserver.StorageConstructor(newFromConfig))
|
2011-10-19 02:14:42 +00:00
|
|
|
blobserver.RegisterStorageConstructor("aeindex", blobserver.StorageConstructor(indexFromConfig))
|
2011-10-13 06:50:52 +00:00
|
|
|
http.Handle("/", root)
|
2011-09-30 05:25:21 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:53:30 +00:00
|
|
|
func realInit(w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
ctx := appengine.NewContext(r)
|
2011-09-30 05:25:21 +00:00
|
|
|
|
2011-10-17 14:53:30 +00:00
|
|
|
errf := func(format string, args ...interface{}) bool {
|
|
|
|
ctx.Errorf("In init: "+format, args...)
|
|
|
|
http.Error(w, fmt.Sprintf(format, args...), 500)
|
|
|
|
return false
|
|
|
|
}
|
2011-10-05 21:34:55 +00:00
|
|
|
|
2011-10-04 23:39:02 +00:00
|
|
|
config, err := serverconfig.Load("./config.json")
|
2011-09-30 05:25:21 +00:00
|
|
|
if err != nil {
|
2011-10-17 14:53:30 +00:00
|
|
|
return errf("Could not load server config: %v", err)
|
2011-09-30 05:25:21 +00:00
|
|
|
}
|
|
|
|
|
2011-10-13 06:50:52 +00:00
|
|
|
// Update the config to use the URL path derived from the first App Engine request.
|
|
|
|
// TODO(bslatkin): Support hostnames that aren't x.appspot.com
|
2011-12-04 22:00:38 +00:00
|
|
|
scheme := "http"
|
|
|
|
if r.TLS != nil {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL := fmt.Sprintf("%s://%s/", scheme, appengine.DefaultVersionHostname(ctx))
|
2011-10-17 14:53:30 +00:00
|
|
|
ctx.Infof("baseurl = %q", baseURL)
|
2011-10-13 06:50:52 +00:00
|
|
|
|
2011-10-17 14:53:30 +00:00
|
|
|
root.mux = http.NewServeMux()
|
2011-10-26 02:40:50 +00:00
|
|
|
err = config.InstallHandlers(root.mux, baseURL, r)
|
2011-09-30 05:25:21 +00:00
|
|
|
if err != nil {
|
2011-10-17 14:53:30 +00:00
|
|
|
return errf("Error installing handlers: %v", err)
|
2011-09-30 05:25:21 +00:00
|
|
|
}
|
2011-10-17 14:53:30 +00:00
|
|
|
|
|
|
|
return true
|
2011-09-30 05:25:21 +00:00
|
|
|
}
|