This commit is contained in:
Chris Sexton 2019-10-28 10:35:14 -04:00
parent 066ca8b19e
commit 359d4efe3c
6 changed files with 276 additions and 8 deletions

217
data/assets_vfsdata.go Normal file

File diff suppressed because one or more lines are too long

7
data/data.go Normal file
View File

@ -0,0 +1,7 @@
// +build dev
package data
import "net/http"
var Assets = http.Dir("../frontend/dist")

21
data/generate.go Normal file
View File

@ -0,0 +1,21 @@
// +build ignore
package main
import (
"log"
"github.com/chrissexton/happy/data"
"github.com/shurcooL/vfsgen"
)
func main() {
err := vfsgen.Generate(data.Assets, vfsgen.Options{
PackageName: "data",
BuildTags: "!dev",
VariableName: "Assets",
})
if err != nil {
log.Fatalln(err)
}
}

2
go.mod
View File

@ -7,6 +7,8 @@ require (
github.com/jmoiron/sqlx v1.2.0
github.com/mattn/go-sqlite3 v1.9.0
github.com/rs/zerolog v1.15.0
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd // indirect
github.com/speps/go-hashids v2.0.0+incompatible
github.com/stretchr/graceful v1.2.15
google.golang.org/appengine v1.6.5 // indirect

4
go.sum
View File

@ -14,6 +14,10 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.15.0 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/speps/go-hashids v2.0.0+incompatible h1:kSfxGfESueJKTx0mpER9Y/1XHl+FVQjtCqRyYcviFbw=
github.com/speps/go-hashids v2.0.0+incompatible/go.mod h1:P7hqPzMdnZOfyIk+xrlG1QaSMw+gCBdHKsBDnhpaZvc=
github.com/stretchr/graceful v1.2.15 h1:vmXbwPGfe8bI6KkgmHry/P1Pk63bM3TDcfi+5mh+VHg=

View File

@ -5,12 +5,15 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"path"
"time"
"github.com/chrissexton/happy/data"
"github.com/speps/go-hashids"
_ "github.com/mattn/go-sqlite3"
@ -23,7 +26,7 @@ import (
)
var (
distPath = flag.String("dist", "frontend/dist", "path to dist files")
distPath = flag.String("dist", "/", "path to dist files")
salt = flag.String("salt", "happy", "salt for IDs")
minHashLen = flag.Int("minHashLen", 4, "minimum ID hash size")
develop = flag.Bool("develop", false, "turn on develop mode")
@ -297,17 +300,31 @@ func (s *server) handleMood(w http.ResponseWriter, r *http.Request) {
func (s *server) indexHandler(entryPoint string) func(w http.ResponseWriter, r *http.Request) {
fn := func(w http.ResponseWriter, r *http.Request) {
p := path.Join(*distPath, path.Clean(r.URL.Path))
f, err := os.Stat(p)
if !os.IsNotExist(err) && !f.IsDir() {
http.ServeFile(w, r, p)
return
f, err := data.Assets.Open(p)
if err == nil {
if finfo, err := f.Stat(); err != nil && !finfo.IsDir() {
io.Copy(w, f)
return
}
}
log.Debug().Err(err).Str("path", p).Msg("file not found")
p = path.Join(p, "index.html")
if f, err := os.Stat(p); !os.IsNotExist(err) && !f.IsDir() {
http.ServeFile(w, r, p)
f, err = data.Assets.Open(p)
if err == nil {
if finfo, err := f.Stat(); err != nil && !finfo.IsDir() {
io.Copy(w, f)
return
}
}
log.Debug().Err(err).Str("path", p).Msg("file not found")
p = path.Join(*distPath, entryPoint)
f, err = data.Assets.Open(p)
if err != nil {
log.Debug().Err(err).Str("path", p).Msg("file not found")
w.WriteHeader(http.StatusNotFound)
return
}
http.ServeFile(w, r, path.Join(*distPath, entryPoint))
io.Copy(w, f)
}
return fn
}