1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17
18
19
20
21 package 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
36
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
54
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
72
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
82
83 func PathPrefix(r *http.Request) string {
84 if prefix := httputil.PathBase(r); prefix != "" {
85 return prefix
86 }
87 return "/"
88 }