go-maymoon/rootdir.go

73 lines
1.5 KiB
Go

package maymoon
import (
"errors"
"fmt"
"os"
"path/filepath"
)
const (
publicRootDirName string = "public_root"
publicRootEnvName string = "PUBLIC_ROOT"
)
// RootDir returns the "root" directory.
//
// By default, where the "root" directory is depends which user it is running under.
//
// For example, on a Unix or Unix-like system such as Linux, with a user "joeblow" who has a home-directory at
// "/home/joeblow", RootDir might return "/home/joeblow/public_root".
//
// This can be overridden using the "PUBLIC_ROOT" environment variable.
// For example:
//
// PUBLIC_ROOT=/path/to/public_root
//
// Example usage:
//
// rootDir, err := maymoon.RootDir()
// if nil != err {
// return err
// }
//
// fmt.Printf("The maymoon root-dir is at %q \n", rootDir)
//
// One usage for this function is to use the path it returns in HTTPHandler.
// For example:
//
// rootDir, err := maymoon.RootDir()
// if nil != err {
// return err
// }
//
// // ...
//
// var handler http.Handler = maymoon.HTTPHandler(rootDir)
func RootDir() (string, error) {
{
path := os.Getenv(publicRootEnvName)
if "" != path {
/////////////////////// RETURN
return path, nil
}
}
var path string
{
homedir, err := os.UserHomeDir()
if nil != err {
/////////////////////// RETURN
return "", fmt.Errorf("problem getting user home-directory: %w", err)
}
path = filepath.Join(homedir, publicRootDirName)
if "" == path {
/////////////////////// RETURN
return "", errors.New("empty path")
}
}
return path, nil
}