Last active
March 13, 2022 09:09
-
-
Save podhmo/d034119442adafb6d99bb8390b8af705 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── 00structure.txt | |
├── Makefile | |
├── main.go | |
├── public | |
│ ├── dev | |
│ │ └── config.json | |
│ └── prod | |
│ └── config.json | |
└── statik | |
└── statik.go | |
4 directories, 6 files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"env": "production" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gen: | |
statik |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment