Skip to content

Commit

Permalink
assets: use statik to compile static files into binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed Aug 11, 2016
1 parent 2d30a6e commit 32d0ce9
Show file tree
Hide file tree
Showing 24 changed files with 474 additions and 27 deletions.
5 changes: 5 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
export PATH := $(GOPATH)/bin:$(PATH)
export GO15VENDOREXPERIMENT := 1

all: build
all: fmt dep build

build: fmt frps frpc build_test
build: frps frpc build_test

build_test: echo_server http_server

dep: statik

statik:
go get -d github.com/rakyll/statik
@go install github.com/rakyll/statik
@rm -rf ./src/assets/statik
go generate ./src/...

fmt:
go fmt ./src/...
@go fmt ./test/echo_server.go
Expand All @@ -15,7 +23,7 @@ fmt:

frps:
go build -o bin/frps ./src/cmd/frps
cp -rf ./assets ./bin
cp -rf ./src/assets/static ./bin

frpc:
go build -o bin/frpc ./src/cmd/frpc
Expand Down Expand Up @@ -43,3 +51,6 @@ clean:
rm -f ./test/bin/echo_server
rm -f ./test/bin/http_server
cd ./test && ./clean_test.sh && cd -

save:
godep save ./src/...
18 changes: 0 additions & 18 deletions assets/static/iconfont.css

This file was deleted.

2 changes: 1 addition & 1 deletion conf/frps.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ vhost_https_port = 443
# if you want to configure or reload frps by dashboard, dashboard_port must be set
dashboard_port = 7500
# dashboard assets directory(only for debug mode)
assets_dir = ./assets
# assets_dir = ./static
# console or real logFile path like ./frps.log
log_file = ./frps.log
# debug, info, warn, error
Expand Down
75 changes: 75 additions & 0 deletions src/assets/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2016 fatedier, [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package assets

//go:generate statik -src=./static
//go:generate go fmt statik/statik.go

import (
"io/ioutil"
"net/http"
"os"
"path"

"github.com/rakyll/statik/fs"

_ "github.com/fatedier/frp/src/assets/statik"
)

var (
// store static files in memory by statik
FileSystem http.FileSystem

// if prefix is not empty, we get file content from disk
prefixPath string
)

// if path is empty, load assets in memory
// or set FileSystem using disk files
func Load(path string) (err error) {
prefixPath = path
if prefixPath != "" {
FileSystem = http.Dir(prefixPath)
return nil
} else {
FileSystem, err = fs.New()
}
return err
}

func ReadFile(file string) (content string, err error) {
if prefixPath == "" {
file, err := FileSystem.Open(path.Join("/", file))
if err != nil {
return content, err
}
buf, err := ioutil.ReadAll(file)
if err != nil {
return content, err
}
content = string(buf)
} else {
file, err := os.Open(path.Join(prefixPath, file))
if err != nil {
return content, err
}
buf, err := ioutil.ReadAll(file)
if err != nil {
return content, err
}
content = string(buf)
}
return content, err
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added src/assets/static/favicon.ico
Binary file not shown.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions src/assets/statik/statik.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/cmd/frps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

docopt "github.com/docopt/docopt-go"

"github.com/fatedier/frp/src/assets"
"github.com/fatedier/frp/src/models/server"
"github.com/fatedier/frp/src/utils/conn"
"github.com/fatedier/frp/src/utils/log"
Expand Down Expand Up @@ -130,6 +131,13 @@ func main() {

log.InitLog(server.LogWay, server.LogFile, server.LogLevel, server.LogMaxDays)

// init assets
err = assets.Load(server.AssetsDir)
if err != nil {
log.Error("Load assets error: %v", err)
os.Exit(1)
}

l, err := conn.Listen(server.BindAddr, server.BindPort)
if err != nil {
log.Error("Create server listener error, %v", err)
Expand Down
7 changes: 5 additions & 2 deletions src/models/server/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"net"
"net/http"
"time"

"github.com/fatedier/frp/src/assets"
)

var (
Expand All @@ -33,8 +35,9 @@ func RunDashboardServer(addr string, port int64) (err error) {
mux.HandleFunc("/api/reload", apiReload)
mux.HandleFunc("/api/proxies", apiProxies)

// view see dashboard_view.go
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets"))))
// view, see dashboard_view.go
mux.Handle("/favicon.ico", http.FileServer(assets.FileSystem))
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))
mux.HandleFunc("/", viewDashboard)

address := fmt.Sprintf("%s:%d", addr, port)
Expand Down
11 changes: 8 additions & 3 deletions src/models/server/dashboard_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ package server
import (
"html/template"
"net/http"
"path"

"github.com/fatedier/frp/src/assets"
"github.com/fatedier/frp/src/models/metric"
"github.com/fatedier/frp/src/utils/log"
)

func viewDashboard(w http.ResponseWriter, r *http.Request) {
metrics := metric.GetAllProxyMetrics()
t := template.Must(template.New("index.html").Delims("<<<", ">>>").ParseFiles(path.Join(AssetsDir, "index.html")))
dashboardTpl, err := assets.ReadFile("index.html")
if err != nil {
http.Error(w, "get dashboard template file error", http.StatusInternalServerError)
return
}
t := template.Must(template.New("index.html").Delims("<<<", ">>>").Parse(dashboardTpl))

err := t.Execute(w, metrics)
err = t.Execute(w, metrics)
if err != nil {
log.Warn("parse template file [index.html] error: %v", err)
http.Error(w, "parse template file error", http.StatusInternalServerError)
Expand Down
Loading

0 comments on commit 32d0ce9

Please sign in to comment.