Skip to content

Commit

Permalink
feat: init go-assets usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Apr 16, 2020
1 parent b5a3568 commit f8a8e1a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
32 changes: 15 additions & 17 deletions api/server.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package api

import (
"log"
"io/ioutil"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/gobuffalo/packr/v2"
)

func registerClientRoute(router *gin.Engine, box *packr.Box) {
router.Group("/static").
StaticFS("/", box)
func registerClientRoute(router *gin.Engine) {
for name, file := range Assets.Files {
if !file.IsDir() {
println(111111, name, file.Path)

router.GET("/", func(c *gin.Context) {
html, err := box.Find("index.html")
h, _ := ioutil.ReadAll(file)

if err != nil {
log.Fatal()
router.GET(strings.ReplaceAll(name, "/client/dist", "/"), func(c *gin.Context) {
// c.Header("content-type", getMimeType(file.Path))
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte(h))
c.Abort()
})
}

c.Header("Content-Type", "text/html; charset=utf-8")
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte(html))
c.Abort()
})
}
}

// Serve launches the web client
Expand All @@ -39,8 +38,7 @@ func Serve(router *gin.Engine, disableClient bool) *gin.Engine {
GET("/numbers/:number/scan/ovh", ValidateScanURL, ovhScan)

if !disableClient {
box := packr.New("static", "../client/dist")
registerClientRoute(router, box)
registerClientRoute(router)
}

router.Use(func(c *gin.Context) {
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ require (
github.com/gobuffalo/packr/v2 v2.8.0
github.com/golang/protobuf v1.3.4 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 h1:cW/amwGEJK5MSKntPXRjX4dxs/nGxGT8gXKIsKFmHGc=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15/go.mod h1:Fdm/oWRW+CH8PRbLntksCNtmcCBximKPkVQYvmMl80k=
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f h1:K2zqtTU3T3ZX/vVeFtJ1OoxEm+gsLhu3zQ34tKgOAyk=
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f/go.mod h1:GjkD6wGIxVEccQ4pa27Ebe00zAi1EEpAcL6rL0ADvwU=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down

0 comments on commit f8a8e1a

Please sign in to comment.