Home Download Docs Code Community
     1	/*
     2	Copyright 2014 The Perkeep 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 Perkeep.
    19	// See also https://perkeep.org/doc/app-environment for the related
    20	// variables.
    21	package app // import "perkeep.org/pkg/app"
    22	
    23	import (
    24		"errors"
    25		"fmt"
    26		"net/http"
    27		"os"
    28		"strings"
    29	
    30		"perkeep.org/internal/httputil"
    31		"perkeep.org/pkg/auth"
    32		"perkeep.org/pkg/client"
    33	)
    34	
    35	// Auth returns the auth mode for the app to access Perkeep, as defined by
    36	// environment variables automatically supplied by the Perkeep server host.
    37	func Auth() (auth.AuthMode, error) {
    38		return basicAuth()
    39	}
    40	
    41	func basicAuth() (auth.AuthMode, error) {
    42		authString := os.Getenv("CAMLI_AUTH")
    43		if authString == "" {
    44			return nil, errors.New("CAMLI_AUTH var not set")
    45		}
    46		userpass := strings.Split(authString, ":")
    47		if len(userpass) != 2 {
    48			return nil, fmt.Errorf("invalid auth string syntax. got %q, want \"username:password\"", authString)
    49		}
    50		return auth.NewBasicAuth(userpass[0], userpass[1]), nil
    51	}
    52	
    53	// Client returns a Perkeep client as defined by environment variables
    54	// automatically supplied by the Perkeep server host.
    55	func Client() (*client.Client, error) {
    56		server := os.Getenv("CAMLI_API_HOST")
    57		if server == "" {
    58			return nil, errors.New("CAMLI_API_HOST var not set")
    59		}
    60		am, err := basicAuth()
    61		if err != nil {
    62			return nil, err
    63		}
    64		return client.New(
    65			client.OptionNoExternalConfig(),
    66			client.OptionServer(server),
    67			client.OptionAuthMode(am),
    68		)
    69	}
    70	
    71	// ListenAddress returns the host:[port] network address, derived from the environment,
    72	// that the application should listen on.
    73	func ListenAddress() (string, error) {
    74		listenAddr := os.Getenv("CAMLI_APP_LISTEN")
    75		if listenAddr == "" {
    76			return "", errors.New("CAMLI_APP_LISTEN is undefined")
    77		}
    78		return listenAddr, nil
    79	}
    80	
    81	// PathPrefix returns the app's prefix on the app handler if the request was proxied
    82	// through Perkeep, or "/" if the request went directly to the app.
    83	func PathPrefix(r *http.Request) string {
    84		if prefix := httputil.PathBase(r); prefix != "" {
    85			return prefix
    86		}
    87		return "/"
    88	}
Website layout inspired by memcached.
Content by the authors.