This repository was archived by the owner on Aug 22, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 8 files changed +169
-5
lines changed
Expand file tree Collapse file tree 8 files changed +169
-5
lines changed Original file line number Diff line number Diff line change @@ -2,4 +2,6 @@ module app
22
33go 1.13
44
5- require github.com/markbates/pkger v0.0.0-20191016200917-09e9684b656b
5+ require github.com/markbates/pkger v0.5.0
6+
7+ replace github.com/markbates/pkger => ../../../
Original file line number Diff line number Diff line change @@ -6,8 +6,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
66github.com/kr/pty v1.1.1 /go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ =
77github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE =
88github.com/kr/text v0.1.0 /go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI =
9- github.com/markbates/pkger v0.0.0-20191016200917-09e9684b656b h1:cXYQ3JZQkRLFyN8m22Q58mOhdKyTooPqvGt8pyWR8eA =
10- github.com/markbates/pkger v0.0.0-20191016200917-09e9684b656b /go.mod h1:0nBNvgA9jJk9gWhO/BcIYz1qJ8BJ0MWCSyKFPApdWNs =
119github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
1210github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
1311github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
@@ -18,3 +16,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
1816gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 /go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0 =
1917gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw =
2018gopkg.in/yaml.v2 v2.2.2 /go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI =
19+ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I =
20+ gopkg.in/yaml.v2 v2.2.4 /go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI =
Original file line number Diff line number Diff line change @@ -14,10 +14,9 @@ func main() {
1414}
1515
1616func run () error {
17- f , err := pkger .Open ("/public" )
17+ dir , err := pkger .HTTP ("/public" )
1818 if err != nil {
1919 return err
2020 }
21- dir := http .FileServer (f )
2221 return http .ListenAndServe (":3000" , dir )
2322}
Original file line number Diff line number Diff line change 11module app
22
33go 1.13
4+
5+ require github.com/markbates/pkger v0.4.0 // indirect
Original file line number Diff line number Diff line change 1+ github.com/davecgh/go-spew v1.1.0 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
2+ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c =
3+ github.com/davecgh/go-spew v1.1.1 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
4+ github.com/kr/pretty v0.1.0 /go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo =
5+ github.com/kr/pty v1.1.1 /go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ =
6+ github.com/kr/text v0.1.0 /go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI =
7+ github.com/markbates/pkger v0.4.0 h1:fIzmOXYsJV+nFS+RtsmWZvXeH5DDGQtF/SwKJgfgVoE =
8+ github.com/markbates/pkger v0.4.0 /go.mod h1:so/QD8FeTM0IilC3nRArkwOvUT+tsJsaXLFUAKmjzJk =
9+ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
10+ github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
11+ github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
12+ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk =
13+ github.com/stretchr/testify v1.4.0 /go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4 =
14+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 /go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0 =
15+ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 /go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0 =
16+ gopkg.in/yaml.v2 v2.2.2 /go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI =
17+ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I =
18+ gopkg.in/yaml.v2 v2.2.4 /go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI =
Original file line number Diff line number Diff line change 1+ package parser
2+
3+ import (
4+ "encoding/json"
5+ "go/token"
6+ "os"
7+ "path/filepath"
8+
9+ "github.com/markbates/pkger"
10+ "github.com/markbates/pkger/here"
11+ )
12+
13+ var _ Decl = HTTPDecl {}
14+
15+ type HTTPDecl struct {
16+ file * File
17+ pos token.Pos
18+ value string
19+ }
20+
21+ func (d HTTPDecl ) MarshalJSON () ([]byte , error ) {
22+ return json .Marshal (map [string ]interface {}{
23+ "type" : "pkger.HTTP" ,
24+ "file" : d .file ,
25+ "pos" : d .pos ,
26+ "value" : d .value ,
27+ })
28+ }
29+
30+ func (d HTTPDecl ) File () (* File , error ) {
31+ if d .file == nil {
32+ return nil , os .ErrNotExist
33+ }
34+ return d .file , nil
35+ }
36+
37+ func (d HTTPDecl ) Pos () (token.Pos , error ) {
38+ if d .pos <= 0 {
39+ return - 1 , os .ErrNotExist
40+ }
41+ return d .pos , nil
42+ }
43+
44+ func (d HTTPDecl ) Value () (string , error ) {
45+ if d .value == "" {
46+ return "" , os .ErrNotExist
47+ }
48+ return d .value , nil
49+ }
50+
51+ func (d HTTPDecl ) Files () ([]* File , error ) {
52+
53+ pt , err := pkger .Parse (d .value )
54+ if err != nil {
55+ return nil , err
56+ }
57+
58+ her , err := here .Package (pt .Pkg )
59+ if err != nil {
60+ return nil , err
61+ }
62+
63+ fp := filepath .Join (her .Dir , pt .Name )
64+
65+ osf , err := os .Stat (fp )
66+ if err != nil {
67+ return nil , err
68+ }
69+
70+ if osf .IsDir () {
71+ wd := WalkDecl {
72+ file : d .file ,
73+ pos : d .pos ,
74+ value : d .value ,
75+ }
76+ return wd .Files ()
77+ }
78+
79+ var files []* File
80+ files = append (files , & File {
81+ Abs : filepath .Join (her .Dir , pt .Name ),
82+ Path : pt ,
83+ Here : her ,
84+ })
85+
86+ return files , nil
87+ }
Original file line number Diff line number Diff line change @@ -32,6 +32,10 @@ func (f *file) find() (Decls, error) {
3232 return nil , err
3333 }
3434
35+ if err := f .findHTTPCalls (); err != nil {
36+ return nil , err
37+ }
38+
3539 if err := f .findWalkCalls (); err != nil {
3640 return nil , err
3741 }
@@ -136,6 +140,48 @@ func (f *file) findWalkCalls() error {
136140 return err
137141}
138142
143+ func (f * file ) findHTTPCalls () error {
144+ var err error
145+ f .walk (func (node ast.Node ) bool {
146+ ce , ok := node .(* ast.CallExpr )
147+ if ! ok {
148+ return true
149+ }
150+
151+ exists := isPkgDot (ce .Fun , "pkger" , "HTTP" )
152+ if ! (exists ) || len (ce .Args ) != 1 {
153+ return true
154+ }
155+
156+ n := ce .Args [0 ]
157+
158+ s , err := f .asValue (n )
159+ if err != nil {
160+ return false
161+ }
162+
163+ info , err := here .Dir (filepath .Dir (f .filename ))
164+ if err != nil {
165+ return false
166+ }
167+
168+ pf := & File {
169+ Abs : f .filename ,
170+ Here : info ,
171+ }
172+
173+ decl := HTTPDecl {
174+ file : pf ,
175+ pos : n .Pos (),
176+ value : s ,
177+ }
178+
179+ f .decls = append (f .decls , decl )
180+ return true
181+ })
182+ return err
183+ }
184+
139185// helpers
140186// =======
141187func isPkgDot (expr ast.Expr , pkg , name string ) bool {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package pkger
22
33import (
44 "log"
5+ "net/http"
56 "os"
67 "path/filepath"
78 "sync"
@@ -94,3 +95,12 @@ func Remove(name string) error {
9495func RemoveAll (name string ) error {
9596 return impl ().RemoveAll (name )
9697}
98+
99+ // HTTP returns an http.FileServer for the specified path.
100+ func HTTP (p string ) (http.Handler , error ) {
101+ f , err := Open (p )
102+ if err != nil {
103+ return nil , err
104+ }
105+ return http .FileServer (f ), nil
106+ }
You can’t perform that action at this time.
0 commit comments