12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package webpages
- import (
- "embed"
- _ "embed"
- "fmt"
- "io"
- "io/fs"
- "log"
- "os"
- "path"
- )
- //go:embed html cdn
- var content embed.FS
- type ServiceOption string
- const EMBED ServiceOption = "embed"
- const FILESYSTEM ServiceOption = "filesystem"
- /*
- Creates the new filesystem implementer for serving the webpages to the API
- :param opt: the service option to
- */
- func NewContentLayer(opt ServiceOption) fs.FS {
- if opt == EMBED {
- fmt.Println("Using embed files to pull html templates")
- return content
- }
- if opt == FILESYSTEM {
- fmt.Println("Using filesystem to pull html templates")
- return FilesystemWebpages{Webroot: path.Base(os.Getenv("WEB_ROOT"))}
- }
- log.Fatal("Unknown option was passed: ", opt)
- return content
- }
- type WebContentLayer interface{}
- type EmbeddedWebpages struct{}
- type FilesystemWebpages struct {
- Webroot string
- }
- /*
- Implementing the io.FS interface for interoperability
- */
- func (f FilesystemWebpages) Open(file string) (fs.File, error) {
- filePath := path.Join(os.Getenv("WEB_ROOT"), file)
- fmt.Println(filePath)
- fh, err := os.Open(filePath)
- if err != nil {
- fmt.Printf("Error opening the file: %s because %s", filePath, err)
- return nil, err
- }
- return fh, nil
- }
- /*
- Read content to a string for easy template ingestion. Will panic if the underlying os.Open call fails
- */
- func ReadToString(rdr fs.FS, name string) string {
- fh, err := rdr.Open(name)
- if err != nil {
- log.Fatal(err, "couldnt open the file: ", name)
- }
- b, err := io.ReadAll(fh)
- if err != nil {
- log.Fatal("Could not read the file: ", name)
- }
- return string(b)
- }
|