github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/server/appengine/camli/main.go (about)

     1  // +build appengine
     2  
     3  /*
     4  Copyright 2011 Google Inc.
     5  
     6  Licensed under the Apache License, Version 2.0 (the "License");
     7  you may not use this file except in compliance with the License.
     8  You may obtain a copy of the License at
     9  
    10       http://www.apache.org/licenses/LICENSE-2.0
    11  
    12  Unless required by applicable law or agreed to in writing, software
    13  distributed under the License is distributed on an "AS IS" BASIS,
    14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  See the License for the specific language governing permissions and
    16  limitations under the License.
    17  */
    18  
    19  package appengine
    20  
    21  import (
    22  	"fmt"
    23  	"net/http"
    24  	"sync"
    25  
    26  	"appengine"
    27  
    28  	"camlistore.org/pkg/blobserver" // storage interface definition
    29  	_ "camlistore.org/pkg/blobserver/cond"
    30  	_ "camlistore.org/pkg/blobserver/replica"
    31  	_ "camlistore.org/pkg/blobserver/shard"
    32  	_ "camlistore.org/pkg/server"     // handlers: UI, publish, thumbnailing, etc
    33  	"camlistore.org/pkg/serverconfig" // wiring up the world from a JSON description
    34  
    35  	// TODO(bradfitz): uncomment these config setup
    36  	// Both require an App Engine context to make HTTP requests too.
    37  	//_ "camlistore.org/pkg/blobserver/remote"
    38  	//_ "camlistore.org/pkg/blobserver/s3"
    39  )
    40  
    41  // lazyInit is our root handler for App Engine. We don't have an App Engine
    42  // context until the first request and we need that context to figure out
    43  // our serving URL. So we use this to defer setting up our environment until
    44  // the first request.
    45  type lazyInit struct {
    46  	mu    sync.Mutex
    47  	ready bool
    48  	mux   *http.ServeMux
    49  }
    50  
    51  func (li *lazyInit) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    52  	c := appengine.NewContext(r)
    53  	ctxPool.HandlerBegin(c)
    54  	defer ctxPool.HandlerEnd(c)
    55  
    56  	li.mu.Lock()
    57  	if !li.ready {
    58  		li.ready = realInit(w, r)
    59  	}
    60  	li.mu.Unlock()
    61  	if li.ready {
    62  		li.mux.ServeHTTP(w, r)
    63  	}
    64  }
    65  
    66  var ctxPool ContextPool
    67  
    68  var root = new(lazyInit)
    69  
    70  func init() {
    71  	// TODO(bradfitz): rename some of this to be consistent
    72  	blobserver.RegisterStorageConstructor("appengine", blobserver.StorageConstructor(newFromConfig))
    73  	blobserver.RegisterStorageConstructor("aeindex", blobserver.StorageConstructor(indexFromConfig))
    74  	http.Handle("/", root)
    75  }
    76  
    77  func realInit(w http.ResponseWriter, r *http.Request) bool {
    78  	ctx := appengine.NewContext(r)
    79  
    80  	errf := func(format string, args ...interface{}) bool {
    81  		ctx.Errorf("In init: "+format, args...)
    82  		http.Error(w, fmt.Sprintf(format, args...), 500)
    83  		return false
    84  	}
    85  
    86  	config, err := serverconfig.Load("./config.json")
    87  	if err != nil {
    88  		return errf("Could not load server config: %v", err)
    89  	}
    90  
    91  	// Update the config to use the URL path derived from the first App Engine request.
    92  	// TODO(bslatkin): Support hostnames that aren't x.appspot.com
    93  	scheme := "http"
    94  	if r.TLS != nil {
    95  		scheme = "https"
    96  	}
    97  
    98  	baseURL := fmt.Sprintf("%s://%s/", scheme, appengine.DefaultVersionHostname(ctx))
    99  	ctx.Infof("baseurl = %q", baseURL)
   100  
   101  	root.mux = http.NewServeMux()
   102  	_, err = config.InstallHandlers(root.mux, baseURL, false, r)
   103  	if err != nil {
   104  		return errf("Error installing handlers: %v", err)
   105  	}
   106  
   107  	return true
   108  }