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

     1  // +build !awslambda
     2  ///////////////////////////////////////////////////////////////////////////////
     3  //
     4  // The MIT License (MIT)
     5  // Copyright (c) 2018 Jivan Amara
     6  // Copyright (c) 2018 Tom Kralidis
     7  //
     8  // Permission is hereby granted, free of charge, to any person obtaining a copy
     9  // of this software and associated documentation files (the "Software"), to
    10  // deal in the Software without restriction, including without limitation the
    11  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    12  // sell copies of the Software, and to permit persons to whom the Software is
    13  // furnished to do so, subject to the following conditions:
    14  //
    15  // The above copyright notice and this permission notice shall be included in
    16  // all copies or substantial portions of the Software.
    17  //
    18  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    19  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    20  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    21  // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    22  // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    23  // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
    24  // USE OR OTHER DEALINGS IN THE SOFTWARE.
    25  //
    26  ///////////////////////////////////////////////////////////////////////////////
    27  
    28  package server
    29  
    30  import (
    31  	"fmt"
    32  	"net/http"
    33  	"strings"
    34  
    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  	sconf := config.Configuration.Server
    43  	bindAddress := fmt.Sprintf("%v:%v", sconf.BindHost, sconf.BindPort)
    44  
    45  	fmt.Printf("Bound to: %v\n", bindAddress)
    46  	if sconf.URLHostPort != "" {
    47  		fmt.Printf("Expecting traffic at %v\n", sconf.URLHostPort)
    48  	}
    49  
    50  	Provider = p
    51  	handler := setUpRoutes()
    52  	err := http.ListenAndServe(bindAddress, handler)
    53  	if err != nil {
    54  		panic(fmt.Sprintf("Problem starting web server: %v", err))
    55  	}
    56  }
    57  
    58  // Provides the preferred <scheme>://<host>:<port>/<base> portion of urls for use in responses.
    59  //	Normally this mirrors the request made, but may be overriden in config & via cl args.
    60  func serveSchemeHostPortBase(r *http.Request) string {
    61  	// Preferred host:port
    62  	php := config.Configuration.Server.URLHostPort
    63  	if php == "" {
    64  		php = r.Host
    65  	}
    66  	php = strings.TrimRight(php, "/")
    67  
    68  	// Preferred scheme
    69  	ps := config.Configuration.Server.URLScheme
    70  
    71  	// Preferred base path
    72  	pbp := strings.TrimRight(config.Configuration.Server.URLBasePath, "/")
    73  
    74  	// Preferred scheme / host / port / base
    75  	pshpb := fmt.Sprintf("%v://%v%v", ps, php, pbp)
    76  
    77  	return pshpb
    78  }