github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/core/general/static_store/static_store.go (about)

     1  package static_store
     2  
     3  import (
     4  	"github.com/k0marov/go-socnet/core/general/core_values"
     5  	"github.com/k0marov/go-socnet/core/general/core_values/ref"
     6  	"log"
     7  	"os"
     8  )
     9  
    10  type (
    11  	StaticFileCreator = func(data ref.Ref[[]byte], dir, filename string) (core_values.StaticPath, error)
    12  	StaticDirDeleter  = func(dir core_values.StaticPath) error
    13  )
    14  
    15  var StaticDir = getStaticDir()
    16  var StaticHost = getStaticHostStr()
    17  
    18  func PathToURL(path core_values.StaticPath) core_values.FileURL {
    19  	if path == "" {
    20  		return ""
    21  	}
    22  	return StaticHost + "/" + path
    23  }
    24  
    25  func getStaticHostStr() string {
    26  	const staticHostEnv = "SOCIO_STATIC_HOST"
    27  	host, exists := os.LookupEnv(staticHostEnv)
    28  	if !exists {
    29  		log.Fatalf(`Environment variable %s is not set.
    30  If this is a test, just set the environment variable to a dummy string.
    31  If this is in production, set this environment variable to point to the URL from which the static directory can be accessed.`, staticHostEnv)
    32  	}
    33  	return host
    34  }
    35  
    36  func getStaticDir() string {
    37  	const staticDirEnv = "SOCIO_STATIC_DIR"
    38  	dir, exists := os.LookupEnv(staticDirEnv)
    39  	if !exists {
    40  		log.Fatalf(`Environment variable %s is not set.
    41  If this is a test, just set the environment variable to some path like ./static/.
    42  If this is in production, set this environment variable to point to the file system path of a directory where static files will be stored`, staticDirEnv)
    43  	}
    44  	return dir
    45  }