github.com/go-spatial/go-wfs@v0.1.4-0.20190401000911-c9fba2bb5188/server/server_awslambda.go (about)

     1  // +build awslambda
     2  ///////////////////////////////////////////////////////////////////////////////
     3  //
     4  // The MIT License (MIT)
     5  // Copyright (c) 2018 Jivan Amara
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to
     9  // deal in the Software without restriction, including without limitation the
    10  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    11  // sell copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    18  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    19  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    20  // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    21  // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    22  // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
    23  // USE OR OTHER DEALINGS IN THE SOFTWARE.
    24  //
    25  ///////////////////////////////////////////////////////////////////////////////
    26  
    27  package server
    28  
    29  import (
    30  	"fmt"
    31  	"net/http"
    32  	"strings"
    33  
    34  	"github.com/akrylysov/algnhsa"
    35  	"github.com/go-spatial/jivan/config"
    36  	"github.com/go-spatial/jivan/data_provider"
    37  )
    38  
    39  var Provider data_provider.Provider
    40  
    41  func StartServer(p data_provider.Provider) {
    42  	Provider = p
    43  	h := setUpRoutes()
    44  	algnhsa.ListenAndServe(h, nil)
    45  }
    46  
    47  // Provides the preferred <scheme>://<host>:<port>/<base> portion of urls for use in responses.
    48  //	Normally this mirrors the request made, but may be overriden in config & via cl args.
    49  func serveSchemeHostPortBase(r *http.Request) string {
    50  	// Preferred host:port
    51  	php := config.Configuration.Server.URLHostPort
    52  	if php == "" {
    53  		php = r.Header["Host"][0]
    54  	}
    55  	php = strings.TrimRight(php, "/")
    56  
    57  	// Preferred scheme
    58  	var ps string
    59  	fproto := r.Header["X-Forwarded-Proto"]
    60  	if len(fproto) > 0 && fproto[0] != "" {
    61  		ps = fproto[0]
    62  	} else {
    63  		ps = config.Configuration.Server.URLScheme
    64  	}
    65  
    66  	// Preferred base path
    67  	pbp := strings.TrimRight(config.Configuration.Server.URLBasePath, "/")
    68  	var stage string
    69  	if ctx, ok := algnhsa.ProxyRequestFromContext(r.Context()); ok {
    70  		stage = ctx.RequestContext.Stage
    71  	}
    72  	// If you've mapped a custom domain for the API Gateway including the stage,
    73  	//	you don't want to include the stage name in the path.
    74  	stage = ""
    75  
    76  	if stage != "" {
    77  		pbp = fmt.Sprintf("/%v%v", stage, pbp)
    78  	}
    79  
    80  	// Preferred scheme / host / port / base
    81  	pshpb := fmt.Sprintf("%v://%v%v", ps, php, pbp)
    82  
    83  	return pshpb
    84  }