github.com/cayleygraph/cayley@v0.7.7/appengine/appengine.go (about)

     1  // Copyright 2014 The Cayley Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build appengine appenginevm
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"time"
    23  
    24  	"github.com/cayleygraph/cayley/clog"
    25  	"github.com/cayleygraph/cayley/graph"
    26  	"github.com/cayleygraph/cayley/internal/http"
    27  
    28  	_ "github.com/cayleygraph/cayley/graph/gaedatastore"
    29  	_ "github.com/cayleygraph/cayley/writer"
    30  
    31  	// Register supported query languages
    32  	_ "github.com/cayleygraph/cayley/query/gizmo"
    33  	_ "github.com/cayleygraph/cayley/query/graphql"
    34  	_ "github.com/cayleygraph/cayley/query/mql"
    35  )
    36  
    37  var (
    38  	quadFile           = ""
    39  	quadType           = "nquads"
    40  	cpuprofile         = ""
    41  	queryLanguage      = "gizmo"
    42  	configFile         = ""
    43  	databasePath       = ""
    44  	databaseBackend    = "gaedatastore"
    45  	replicationBackend = "single"
    46  	host               = "127.0.0.1"
    47  	loadSize           = 100
    48  	port               = "64210"
    49  	readOnly           = false
    50  	timeout            = 30 * time.Second
    51  )
    52  
    53  func configFrom(file string) (*Config, error) {
    54  	// Find the file...
    55  	if file != "" {
    56  		if _, err := os.Stat(file); os.IsNotExist(err) {
    57  			return nil, fmt.Errorf("Cannot find specified configuration file", file)
    58  		}
    59  	} else if _, err := os.Stat("/cayley_appengine.cfg"); err == nil {
    60  		file = "/cayley_appengine.cfg"
    61  	}
    62  	if file == "" {
    63  		clog.Infof("Couldn't find a config file appengine.cfg. Going by flag defaults only.")
    64  	}
    65  	cfg, err := LoadConf(file)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	if cfg.DatabasePath == "" {
    71  		cfg.DatabasePath = databasePath
    72  	}
    73  
    74  	if cfg.DatabaseType == "" {
    75  		cfg.DatabaseType = databaseBackend
    76  	}
    77  
    78  	if cfg.ReplicationType == "" {
    79  		cfg.ReplicationType = replicationBackend
    80  	}
    81  
    82  	if cfg.ListenHost == "" {
    83  		cfg.ListenHost = host
    84  	}
    85  
    86  	if cfg.ListenPort == "" {
    87  		cfg.ListenPort = port
    88  	}
    89  
    90  	if cfg.Timeout == 0 {
    91  		cfg.Timeout = timeout
    92  	}
    93  
    94  	if cfg.LoadSize == 0 {
    95  		cfg.LoadSize = loadSize
    96  	}
    97  
    98  	cfg.ReadOnly = cfg.ReadOnly || readOnly
    99  
   100  	return cfg, nil
   101  }
   102  
   103  func open(cfg *Config) (*graph.Handle, error) {
   104  	qs, err := graph.NewQuadStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
   105  	// override error to make it more informative
   106  	if os.IsNotExist(err) {
   107  		err = fmt.Errorf("file does not exist: %s. Please use with --init or run ./cayley init when it is a new database (see docs for more information)", cfg.DatabasePath)
   108  	}
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	qw, err := graph.NewQuadWriter(cfg.ReplicationType, qs, cfg.ReplicationOptions)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	return &graph.Handle{QuadStore: qs, QuadWriter: qw}, nil
   117  }
   118  
   119  func init() {
   120  	cfg, err := configFrom("cayley_appengine.cfg")
   121  	if err != nil {
   122  		clog.Fatalf("Error loading config: %v", err)
   123  	}
   124  
   125  	handle, err := open(cfg)
   126  	if err != nil {
   127  		clog.Fatalf("Error opening database: %v", err)
   128  	}
   129  	if err := http.SetupRoutes(handle, cfg); err != nil {
   130  		clog.Fatalf("Error setting up routes: %v", err)
   131  	}
   132  }