2019-08-01 21:37:01 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-08-03 23:36:56 +03:00
|
|
|
"fmt"
|
2019-08-01 21:37:01 +03:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2019-09-22 23:47:40 +03:00
|
|
|
"os"
|
2019-08-01 21:37:01 +03:00
|
|
|
|
|
|
|
"github.com/markbates/pkger"
|
|
|
|
)
|
|
|
|
|
2019-09-22 23:47:40 +03:00
|
|
|
const host = ":3000"
|
|
|
|
|
2019-08-01 21:37:01 +03:00
|
|
|
func main() {
|
2019-09-22 23:47:40 +03:00
|
|
|
// get the currently running application's here.Info.
|
|
|
|
// this contains really, really, really useful information
|
|
|
|
// about your application, check it out. :)
|
|
|
|
// we don't need it for this example, but i thought it could
|
|
|
|
// be good to show.
|
|
|
|
current, err := pkger.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-09 20:21:54 +03:00
|
|
|
fmt.Println(current)
|
2019-08-01 21:37:01 +03:00
|
|
|
|
2019-09-22 23:47:40 +03:00
|
|
|
fmt.Printf("Walking files for %s\n", current.ImportPath)
|
|
|
|
// walk the files in this module. "/" is where the `go.mod` for this module is
|
2019-10-15 23:40:45 +03:00
|
|
|
err = pkger.Walk("github.com/gobuffalo/buffalo:/render", func(path string, info os.FileInfo, err error) error {
|
2019-09-22 23:47:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-15 23:40:45 +03:00
|
|
|
fmt.Println(">> ", path)
|
2019-09-22 23:47:40 +03:00
|
|
|
return nil
|
|
|
|
})
|
2019-08-01 21:37:01 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-08-03 23:36:56 +03:00
|
|
|
|
2019-09-22 23:47:40 +03:00
|
|
|
// find the public directory with using the full pkger path <pkg:/path> to it:
|
|
|
|
// pkg - is the module/package you want to get a file from
|
|
|
|
// if pkg is empty then it is assumed to be current.ImportPath
|
|
|
|
// : - seperator between the module/package name, pkg, and the "file path"
|
|
|
|
// path - this is the ABSOLUTE path to the file/directory you want, as relative
|
|
|
|
// to the root of the module/package's go.mod file.
|
2019-10-16 00:37:51 +03:00
|
|
|
dir, err := pkger.Open("github.com/markbates/pkger/examples/app:/public")
|
2019-09-22 23:47:40 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
// don't forget to close the file later
|
|
|
|
defer dir.Close()
|
2019-08-03 23:36:56 +03:00
|
|
|
|
2019-09-22 23:47:40 +03:00
|
|
|
fmt.Printf("\nServing %q on %s\n", dir.Path(), host)
|
2019-08-01 21:37:01 +03:00
|
|
|
|
2019-09-22 23:47:40 +03:00
|
|
|
// serve the public directory on the host (":3000")
|
|
|
|
// just like using the os package you still need to use
|
|
|
|
// http.FileServer to serve a directory.
|
|
|
|
// you DON'T, however, need to use http.Dir all pkger files
|
|
|
|
// already implement that interface.
|
|
|
|
log.Fatal(http.ListenAndServe(host, logger(http.FileServer(dir))))
|
2019-09-05 19:25:51 +03:00
|
|
|
}
|
|
|
|
|
2019-09-22 23:47:40 +03:00
|
|
|
// logger will print out the requests as they come in, otherwise its a blank
|
|
|
|
// screen, and that's no fun.
|
2019-09-05 19:25:51 +03:00
|
|
|
func logger(h http.Handler) http.HandlerFunc {
|
|
|
|
return func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
log.Println(req.Method, req.URL.String())
|
|
|
|
h.ServeHTTP(res, req)
|
|
|
|
}
|
2019-08-01 21:37:01 +03:00
|
|
|
}
|