Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ cert.pem
key.pem

images
bin
bin
gopher
bindata.go
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
###################################
#Build stage
FROM golang:1.12-alpine AS build-env

#Build deps
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk --no-cache add git
COPY ./ /gopher
WORKDIR /gopher
RUN go build -tags 'bindata'

FROM alpine:3.9

EXPOSE 8000

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk --no-cache add \
bash \
ca-certificates \
curl \
gettext \
s6 \
su-exec \
tzdata

VOLUME ["/data"]

ENTRYPOINT ["/app/gopher/gopher"]

COPY --from=build-env ./gopher/gopher /app/gopher/gopher
52 changes: 52 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
version: '2'

tasks:
update:
cmds:
- GO111MODULE=on go mod tidy

build:
cmds:
- GO111MODULE=on go build

test:
cmds:
- go vet ./...
- go test
- go generate ./modules/...
- go vet -tags="bindata" ./...
- go test -tags="bindata"

run:
deps: [build]
cmds:
- ./gopher

generate:
cmds:
- go generate ./modules/...

release:
deps: [generate]
cmds:
- go build -tags 'bindata'

release-linux:
deps: [generate]
cmds:
- GO111MODULE=on GOOS=linux GOARCH=amd64 go build -tags 'bindata' -o gopher-linux-amd64

release-windows:
deps: [generate]
cmds:
- GOOS=windows GOARCH=amd64 go build -tags 'bindata' -o gopher-windows-amd64.exe

release-docker:
deps: [generate]
cmds:
- docker build . -t extract

clean-docker:
cmds:
- docker ps -a -q -f status=exited | xargs docker rm
- docker images --no-trunc=true --filter dangling=true --quiet | xargs docker rmi
14 changes: 7 additions & 7 deletions cmd/gopher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"gitea.com/tango/renders"

"github.com/jimmykuu/gopher"
"github.com/jimmykuu/gopher/modules/static"
"github.com/jimmykuu/gopher/modules/templates"
)

func main() {
t := tango.Classic()
t.Use(
events.Events(),
tango.Static(tango.StaticOptions{
RootPath: "./static",
Prefix: "static",
}),
static.Static("./static"),
renders.New(renders.Options{
Reload: true,
Directory: "./templates",
Funcs: gopher.Funcs,
Reload: true,
Directory: "./templates",
FileSystem: templates.FileSystem("templates"),
Funcs: gopher.Funcs,
}))

gopher.SetRoutes(t)
Expand Down
9 changes: 4 additions & 5 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"html/template"
"io"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -15,6 +13,7 @@ import (

"github.com/jimmykuu/gopher/conf"
"github.com/jimmykuu/gopher/models"
"github.com/jimmykuu/gopher/modules/static"
"github.com/jimmykuu/webhelpers"
)

Expand Down Expand Up @@ -99,10 +98,8 @@ var Funcs = template.FuncMap{
},
"staticfile": func(path string) string {
// 增加静态文件的版本,防止文件变化后浏览器不更新
var filepath = filepath.Join("./static", path)
var md5Str string

file, err := os.Open(filepath)
file, err := static.PublicFileSystem("./static").Open(path)
if err != nil {
if conf.Config.Debug {
panic(err)
Expand All @@ -112,6 +109,8 @@ var Funcs = template.FuncMap{

md5Str = "nofile"
} else {
defer file.Close()

var ok bool
if md5Str, ok = staticFiles[path]; !ok {
// Debug 状态下,每次都会读取文件的 md5 值,非 Debug状态下,只有第一次读取
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/jimmykuu/webhelpers v0.0.0-20160107152426-014fab4e0ee0
github.com/kr/pretty v0.1.0 // indirect
github.com/onsi/ginkgo v1.10.1 // indirect
github.com/onsi/gomega v1.7.0 // indirect
github.com/pborman/uuid v1.2.0
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
gopkg.in/yaml.v2 v2.2.2 // indirect
Expand Down
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ gitea.com/lunny/log v0.0.0-20190322053110-01b5df579c4e h1:r1en/D7xJmcY24VkHkjkcJ
gitea.com/lunny/log v0.0.0-20190322053110-01b5df579c4e/go.mod h1:uJEsN4LQpeGYRCjuPXPZBClU7N5pWzGuyF4uqLpE/e0=
gitea.com/lunny/tango v0.5.5/go.mod h1:Y4NPJ0GIMLXS5hJPyj4qUzdvrMb6TexIYNVyBLF7AVc=
gitea.com/lunny/tango v0.6.0 h1:Z9wX9fq5e2y9Da+cmlFpE3aAo1+GfTuhYIPPYdCD6kM=
gitea.com/lunny/tango v0.6.0 h1:Z9wX9fq5e2y9Da+cmlFpE3aAo1+GfTuhYIPPYdCD6kM=
gitea.com/lunny/tango v0.6.0/go.mod h1:ebPnYxaYz9qzByTh3A7604q3tcj0QtATtdwvsAafswU=
gitea.com/lunny/tango v0.6.0/go.mod h1:ebPnYxaYz9qzByTh3A7604q3tcj0QtATtdwvsAafswU=
gitea.com/tango/binding v0.0.0-20190606022902-f7676e2641fd h1:iEaCQ6nNs0XaTXFX90qs1KKrWqG2DO+7odSKSkdjc/M=
gitea.com/tango/binding v0.0.0-20190606022902-f7676e2641fd/go.mod h1:38aj78Spy3l3omiX0ZyGy0i3sKfn8t4cKKCPns1QY+Q=
Expand All @@ -23,13 +25,20 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jimmykuu/webhelpers v0.0.0-20160107152426-014fab4e0ee0 h1:3lUkXG0ce/Rw8UsSkwmjIk+SREManVBJx7/K+UGoyMU=
github.com/jimmykuu/webhelpers v0.0.0-20160107152426-014fab4e0ee0/go.mod h1:v/Zs0C7g4i1TZgoKQIDeF+zlmIYXNRUooAmF264DVxY=
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
Expand All @@ -41,6 +50,11 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lunny/log v0.0.0-20160921050905-7887c61bf0de/go.mod h1:3q8WtuPQsoRbatJuy3nvq/hRSvuBJrHHr+ybPPiNvHQ=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
Expand All @@ -50,21 +64,40 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 h1:Jpy1PXuP99tXNrhbq2BaPz9B+jNAvH1JPQQpG/9GCXY=
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c h1:Ho+uVpkel/udgjbwB5Lktg9BtvJSh2DT0Hi6LPSyI2w=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384 h1:TFlARGu6Czu1z7q93HTxcP1P+/ZFC/IKythI5RzrnRg=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
21 changes: 21 additions & 0 deletions modules/static/dynamic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// +build !bindata

package static

import (
"net/http"

"gitea.com/lunny/tango"
)

func PublicFileSystem(publicDir string) http.FileSystem {
return http.Dir(publicDir)
}

// Static implements the tango static handler for serving assets.
func Static(publicDir string) tango.Handler {
return tango.Static(tango.StaticOptions{
RootPath: publicDir,
Prefix: "static",
})
}
23 changes: 23 additions & 0 deletions modules/static/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build ignore

package main

import (
"log"
"net/http"

"github.com/shurcooL/vfsgen"
)

func main() {
var fsPublic http.FileSystem = http.Dir("../../static")
err := vfsgen.Generate(fsPublic, vfsgen.Options{
PackageName: "static",
BuildTags: "bindata",
VariableName: "Assets",
Filename: "bindata.go",
})
if err != nil {
log.Fatalf("%v", err)
}
}
3 changes: 3 additions & 0 deletions modules/static/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package static

//go:generate go run main.go
21 changes: 21 additions & 0 deletions modules/static/static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// +build bindata

package static

import (
"net/http"

"gitea.com/lunny/tango"
)

func PublicFileSystem(publicDir string) http.FileSystem {
return Assets
}

// Static implements the tango static handler for serving assets.
func Static(publicDir string) tango.Handler {
return tango.Static(tango.StaticOptions{
Prefix: "static",
FileSystem: PublicFileSystem(publicDir),
})
}
13 changes: 13 additions & 0 deletions modules/templates/dynamic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !bindata

package templates

import (
"net/http"
"path"
)

// FileSystem implements the macaron handler for serving the templates.
func FileSystem(templatesDir string) http.FileSystem {
return http.Dir(path.Join("./", templatesDir))
}
23 changes: 23 additions & 0 deletions modules/templates/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build ignore

package main

import (
"log"
"net/http"

"github.com/shurcooL/vfsgen"
)

func main() {
var fsTemplates http.FileSystem = http.Dir("../../templates")
err := vfsgen.Generate(fsTemplates, vfsgen.Options{
PackageName: "templates",
BuildTags: "bindata",
VariableName: "Assets",
Filename: "bindata.go",
})
if err != nil {
log.Fatal("%v", err)
}
}
12 changes: 12 additions & 0 deletions modules/templates/static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build bindata

package templates

import (
"net/http"
)

// FileSystem implements the handler for serving the templates.
func FileSystem(templatesDir string) http.FileSystem {
return Assets
}
3 changes: 3 additions & 0 deletions modules/templates/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package templates

//go:generate go run main.go