pkger/examples/app/main.go

40 lines
638 B
Go
Raw Normal View History

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
"io"
"log"
"net/http"
"github.com/markbates/pkger"
)
func main() {
mux := http.NewServeMux()
2019-08-30 05:30:00 +03:00
pub, err := pkger.Open(":/public")
2019-08-01 21:37:01 +03:00
if err != nil {
log.Fatal(err)
}
2019-08-03 23:36:56 +03:00
defer pub.Close()
2019-09-05 19:22:35 +03:00
fmt.Println(pub.Path())
2019-08-03 23:36:56 +03:00
2019-08-01 21:37:01 +03:00
mux.Handle("/t", http.StripPrefix("/t", tmplHandler()))
mux.Handle("/", http.FileServer(pub))
log.Fatal(http.ListenAndServe(":3000", mux))
}
func tmplHandler() http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
2019-08-30 05:30:00 +03:00
t, err := pkger.Open(":/templates/a.txt")
2019-08-01 21:37:01 +03:00
if err != nil {
http.Error(res, err.Error(), 500)
}
defer t.Close()
io.Copy(res, t)
}
}