github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/app/app.go (about)

     1  /*
     2  Copyright 2014 The Camlistore Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package app provides helpers for server applications interacting
    18  // with Camlistore.
    19  package app
    20  
    21  import (
    22  	"errors"
    23  	"fmt"
    24  	"net/http"
    25  	"os"
    26  	"strings"
    27  
    28  	"camlistore.org/pkg/auth"
    29  	"camlistore.org/pkg/client"
    30  )
    31  
    32  // Client returns a client from pkg/client, configured by environment variables
    33  // for applications, and ready to be used to connect to the Camlistore server.
    34  func Client() (*client.Client, error) {
    35  	server := os.Getenv("CAMLI_SERVER")
    36  	if server == "" {
    37  		return nil, errors.New("CAMLI_SERVER var not set")
    38  	}
    39  	authString := os.Getenv("CAMLI_AUTH")
    40  	if authString == "" {
    41  		return nil, errors.New("CAMLI_AUTH var not set")
    42  	}
    43  	userpass := strings.Split(authString, ":")
    44  	if len(userpass) != 2 {
    45  		return nil, fmt.Errorf("invalid auth string syntax. got %q, want \"username:password\"", authString)
    46  	}
    47  	cl := client.NewFromParams(server, auth.NewBasicAuth(userpass[0], userpass[1]))
    48  	cl.SetHTTPClient(&http.Client{
    49  		Transport: cl.TransportForConfig(nil),
    50  	})
    51  	return cl, nil
    52  }
    53  
    54  // ListenAddress returns the host:[port] network address, derived from the environment,
    55  // that the application should listen on.
    56  func ListenAddress() (string, error) {
    57  	baseURL := os.Getenv("CAMLI_APP_BASEURL")
    58  	if baseURL == "" {
    59  		return "", errors.New("CAMLI_APP_BASEURL is undefined")
    60  	}
    61  	defaultPort := "80"
    62  	noScheme := strings.TrimPrefix(baseURL, "http://")
    63  	if strings.HasPrefix(baseURL, "https://") {
    64  		noScheme = strings.TrimPrefix(baseURL, "https://")
    65  		defaultPort = "443"
    66  	}
    67  	hostPortPrefix := strings.SplitN(noScheme, "/", 2)
    68  	if len(hostPortPrefix) != 2 {
    69  		return "", fmt.Errorf("invalid CAMLI_APP_BASEURL: %q (no trailing slash?)", baseURL)
    70  	}
    71  	if !strings.Contains(hostPortPrefix[0], ":") {
    72  		return fmt.Sprintf("%s:%s", hostPortPrefix[0], defaultPort), nil
    73  	}
    74  	return hostPortPrefix[0], nil
    75  }