Skip to content

Instantly share code, notes, and snippets.

@podhmo
Last active March 13, 2022 09:09
Show Gist options
  • Save podhmo/d034119442adafb6d99bb8390b8af705 to your computer and use it in GitHub Desktop.
Save podhmo/d034119442adafb6d99bb8390b8af705 to your computer and use it in GitHub Desktop.
.
├── 00structure.txt
├── Makefile
├── main.go
├── public
│   ├── dev
│   │   └── config.json
│   └── prod
│   └── config.json
└── statik
└── statik.go
4 directories, 6 files
{
"env": "production"
}
package main
import (
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
_ "./statik"
"github.com/rakyll/statik/fs"
)
func main() {
if err := run(); err != nil {
log.Fatalf("%+v", err)
}
}
type DataLoader struct {
prefix string
fs http.FileSystem
}
func (o *DataLoader) Open(path string) (http.File, error) {
return o.fs.Open(filepath.Join(o.prefix, path))
}
func New(prefix string) (*DataLoader, error) {
FS, err := fs.New()
if err != nil {
return nil, err
}
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
return &DataLoader{
fs: FS,
prefix: prefix,
}, nil
}
func run() error {
o, err := New("dev") // or "production"
if err != nil {
return err
}
f, err := o.Open("config.json")
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(os.Stdout, f)
if err != nil {
return err
}
return nil
}
gen:
statik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment