Skip to content

Commit 776a22a

Browse files
committed
Embed static files (html, js, css)
1 parent 3649885 commit 776a22a

File tree

12 files changed

+67
-3
lines changed

12 files changed

+67
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
generated
1+
generated
2+
/specs/*

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2020
github.com/modern-go/reflect2 v1.0.1 // indirect
2121
github.com/mozillazg/request v0.8.0 // indirect
22+
github.com/rakyll/statik v0.1.7
2223
golang.org/x/mod v0.3.0
2324
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
2425
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
150150
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
151151
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
152152
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
153+
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
154+
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
153155
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
154156
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
155157
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

main.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package main
22

3+
//go:generate statik -src=./public -include="*.html,*.css,*.js"
34
import (
45
"errors"
56
"flag"
67
"fmt"
78
"io/ioutil"
9+
"mime"
810
"net/http"
911
"net/url"
1012
"os"
@@ -17,6 +19,7 @@ import (
1719
"github.com/gagliardetto/codebox/scanner"
1820
"github.com/gagliardetto/codemill/handlers/tainttracking"
1921
"github.com/gagliardetto/codemill/handlers/untrustedflowsource"
22+
_ "github.com/gagliardetto/codemill/statik"
2023
"github.com/gagliardetto/codemill/x"
2124
cqljen "github.com/gagliardetto/cqlgen/jen"
2225
"github.com/gagliardetto/feparser"
@@ -27,6 +30,7 @@ import (
2730
"github.com/gagliardetto/request"
2831
. "github.com/gagliardetto/utilz"
2932
"github.com/gin-gonic/gin"
33+
"github.com/rakyll/statik/fs"
3034
"golang.org/x/mod/modfile"
3135
"golang.org/x/tools/go/packages"
3236
)
@@ -44,8 +48,50 @@ var (
4448

4549
func main() {
4650
r := gin.Default()
47-
r.StaticFile("", "./index.html")
48-
r.Static("/static", "./static")
51+
52+
statikFS, err := fs.New()
53+
if err != nil {
54+
Fataln(err)
55+
}
56+
57+
r.GET("/", func(c *gin.Context) {
58+
r, err := statikFS.Open("/index.html")
59+
if err != nil {
60+
Q(err)
61+
Abort404(c, err.Error())
62+
return
63+
}
64+
defer r.Close()
65+
contents, err := ioutil.ReadAll(r)
66+
if err != nil {
67+
Q(err)
68+
Abort404(c, err.Error())
69+
return
70+
}
71+
c.Data(200, "text/html; charset=UTF-8", contents)
72+
})
73+
r.GET("/static/:filename", func(c *gin.Context) {
74+
name := c.Param("filename")
75+
if name == "" {
76+
c.AbortWithStatus(400)
77+
return
78+
}
79+
r, err := statikFS.Open("/static/" + name)
80+
if err != nil {
81+
c.AbortWithError(400, err)
82+
Q(err)
83+
return
84+
}
85+
defer r.Close()
86+
contents, err := ioutil.ReadAll(r)
87+
if err != nil {
88+
c.AbortWithError(400, err)
89+
Q(err)
90+
return
91+
}
92+
m := mime.TypeByExtension(filepath.Ext(name))
93+
c.Data(200, Sf("%s; charset=UTF-8", m), contents)
94+
})
4995
httpClient := new(http.Client)
5096

5197
var specFilepath string
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

statik/statik.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)