wip
This commit is contained in:
parent
066ca8b19e
commit
359d4efe3c
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,7 @@
|
||||||
|
// +build dev
|
||||||
|
|
||||||
|
package data
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
var Assets = http.Dir("../frontend/dist")
|
|
@ -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
2
go.mod
|
@ -7,6 +7,8 @@ require (
|
||||||
github.com/jmoiron/sqlx v1.2.0
|
github.com/jmoiron/sqlx v1.2.0
|
||||||
github.com/mattn/go-sqlite3 v1.9.0
|
github.com/mattn/go-sqlite3 v1.9.0
|
||||||
github.com/rs/zerolog v1.15.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/speps/go-hashids v2.0.0+incompatible
|
||||||
github.com/stretchr/graceful v1.2.15
|
github.com/stretchr/graceful v1.2.15
|
||||||
google.golang.org/appengine v1.6.5 // indirect
|
google.golang.org/appengine v1.6.5 // indirect
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -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/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 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=
|
||||||
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
|
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 h1:kSfxGfESueJKTx0mpER9Y/1XHl+FVQjtCqRyYcviFbw=
|
||||||
github.com/speps/go-hashids v2.0.0+incompatible/go.mod h1:P7hqPzMdnZOfyIk+xrlG1QaSMw+gCBdHKsBDnhpaZvc=
|
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=
|
github.com/stretchr/graceful v1.2.15 h1:vmXbwPGfe8bI6KkgmHry/P1Pk63bM3TDcfi+5mh+VHg=
|
||||||
|
|
31
serve.go
31
serve.go
|
@ -5,12 +5,15 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/chrissexton/happy/data"
|
||||||
|
|
||||||
"github.com/speps/go-hashids"
|
"github.com/speps/go-hashids"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
@ -23,7 +26,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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")
|
salt = flag.String("salt", "happy", "salt for IDs")
|
||||||
minHashLen = flag.Int("minHashLen", 4, "minimum ID hash size")
|
minHashLen = flag.Int("minHashLen", 4, "minimum ID hash size")
|
||||||
develop = flag.Bool("develop", false, "turn on develop mode")
|
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) {
|
func (s *server) indexHandler(entryPoint string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
p := path.Join(*distPath, path.Clean(r.URL.Path))
|
p := path.Join(*distPath, path.Clean(r.URL.Path))
|
||||||
f, err := os.Stat(p)
|
f, err := data.Assets.Open(p)
|
||||||
if !os.IsNotExist(err) && !f.IsDir() {
|
if err == nil {
|
||||||
http.ServeFile(w, r, p)
|
if finfo, err := f.Stat(); err != nil && !finfo.IsDir() {
|
||||||
|
io.Copy(w, f)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
log.Debug().Err(err).Str("path", p).Msg("file not found")
|
||||||
p = path.Join(p, "index.html")
|
p = path.Join(p, "index.html")
|
||||||
if f, err := os.Stat(p); !os.IsNotExist(err) && !f.IsDir() {
|
f, err = data.Assets.Open(p)
|
||||||
http.ServeFile(w, r, p)
|
if err == nil {
|
||||||
|
if finfo, err := f.Stat(); err != nil && !finfo.IsDir() {
|
||||||
|
io.Copy(w, f)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.ServeFile(w, r, path.Join(*distPath, entryPoint))
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
io.Copy(w, f)
|
||||||
}
|
}
|
||||||
return fn
|
return fn
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue