-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathautocertcache.go
48 lines (43 loc) · 1.04 KB
/
autocertcache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package autotls
import (
"errors"
"os"
"path/filepath"
"runtime"
"golang.org/x/crypto/acme/autocert"
)
func getCacheDir() (autocert.DirCache, error) {
dir := cacheDir()
if err := os.MkdirAll(dir, 0o700); err != nil {
return "", errors.New("warning: autocert.NewListener not using a cache: " + err.Error())
}
return autocert.DirCache(dir), nil
}
func cacheDir() string {
const base = "golang-autocert"
switch runtime.GOOS {
case "darwin":
return filepath.Join(homeDir(), "Library", "Caches", base)
case "windows":
for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
if v := os.Getenv(ev); v != "" {
return filepath.Join(v, base)
}
}
// Worst case:
return filepath.Join(homeDir(), base)
}
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
return filepath.Join(xdg, base)
}
return filepath.Join(homeDir(), ".cache", base)
}
func homeDir() string {
if runtime.GOOS == "windows" {
return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
}
if h := os.Getenv("HOME"); h != "" {
return h
}
return "/"
}