assets: optimize static files archetucture

This commit is contained in:
fatedier
2016-08-10 20:18:36 +08:00
parent 3ab9850871
commit 4300169041
20 changed files with 151 additions and 104 deletions

View File

@@ -36,6 +36,7 @@ var (
VhostHttpPort int64 = 0 // if VhostHttpPort equals 0, don't listen a public port for http protocol
VhostHttpsPort int64 = 0 // if VhostHttpsPort equals 0, don't listen a public port for https protocol
DashboardPort int64 = 0 // if DashboardPort equals 0, dashboard is not available
AssetsDir string = ""
LogFile string = "console"
LogWay string = "console" // console or file
LogLevel string = "info"
@@ -118,6 +119,11 @@ func loadCommonConf(confFile string) error {
DashboardPort = 0
}
tmpStr, ok = conf.Get("common", "assets_dir")
if ok {
AssetsDir = tmpStr
}
tmpStr, ok = conf.Get("common", "log_file")
if ok {
LogFile = tmpStr
@@ -252,6 +258,8 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e
}
} else if proxyServer.Type == "http" {
// for http
proxyServer.ListenPort = VhostHttpPort
domainStr, ok := section["custom_domains"]
if ok {
proxyServer.CustomDomains = strings.Split(domainStr, ",")
@@ -266,6 +274,8 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e
}
} else if proxyServer.Type == "https" {
// for https
proxyServer.ListenPort = VhostHttpsPort
domainStr, ok := section["custom_domains"]
if ok {
proxyServer.CustomDomains = strings.Split(domainStr, ",")

View File

@@ -16,55 +16,42 @@ package server
import (
"fmt"
"frp/models/metric"
"html/template"
"net"
"net/http"
"github.com/gin-gonic/gin"
"time"
)
func index(w http.ResponseWriter, r *http.Request) {
serinfo := metric.GetAllProxyMetrics()
t := template.Must(template.New("index.html").Delims("<<<", ">>>").ParseFiles("index.html"))
err := t.Execute(w, serinfo)
if err != nil {
fmt.Println(err.Error())
}
}
var (
httpServerReadTimeout = 10 * time.Second
httpServerWriteTimeout = 10 * time.Second
)
func RunDashboardServer(addr string, port int64) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
gin.SetMode(gin.ReleaseMode)
router := gin.New()
//router.LoadHTMLGlob("assets/*")
router.GET("/api/reload", apiReload)
router.GET("/api/proxies", apiProxies)
go router.Run(fmt.Sprintf("%s:%d", addr, port))
return
}
// url router
mux := http.NewServeMux()
// api, see dashboard_api.go
mux.HandleFunc("/api/reload", apiReload)
mux.HandleFunc("/api/proxies", apiProxies)
func RunDashboardServer2(addr string, port int64) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
// view see dashboard_view.go
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets"))))
mux.HandleFunc("/", viewDashboard)
http.HandleFunc("/", index)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
newPort := fmt.Sprintf(":%d", port)
err = http.ListenAndServe(newPort, nil)
address := fmt.Sprintf("%s:%d", addr, port)
server := &http.Server{
Addr: address,
Handler: mux,
ReadTimeout: httpServerReadTimeout,
WriteTimeout: httpServerWriteTimeout,
}
if address == "" {
address = ":http"
}
ln, err := net.Listen("tcp", address)
if err != nil {
return err
}
return nil
go server.Serve(ln)
return
}

View File

@@ -17,8 +17,7 @@ package server
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"frp/models/metric"
"frp/utils/log"
@@ -29,10 +28,10 @@ type GeneralResponse struct {
Msg string `json:"msg"`
}
func apiReload(c *gin.Context) {
func apiReload(w http.ResponseWriter, r *http.Request) {
var buf []byte
res := &GeneralResponse{}
defer func() {
buf, _ := json.Marshal(res)
log.Info("Http response [/api/reload]: %s", string(buf))
}()
@@ -43,7 +42,9 @@ func apiReload(c *gin.Context) {
res.Msg = fmt.Sprintf("%v", err)
log.Error("frps reload error: %v", err)
}
c.JSON(200, res)
buf, _ = json.Marshal(res)
w.Write(buf)
}
type ProxiesResponse struct {
@@ -52,7 +53,8 @@ type ProxiesResponse struct {
Proxies []*metric.ServerMetric `json:"proxies"`
}
func apiProxies(c *gin.Context) {
func apiProxies(w http.ResponseWriter, r *http.Request) {
var buf []byte
res := &ProxiesResponse{}
defer func() {
log.Info("Http response [/api/proxies]: code [%d]", res.Code)
@@ -60,5 +62,6 @@ func apiProxies(c *gin.Context) {
log.Info("Http request: [/api/proxies]")
res.Proxies = metric.GetAllProxyMetrics()
c.JSON(200, res)
buf, _ = json.Marshal(res)
w.Write(buf)
}

View File

@@ -0,0 +1,35 @@
// Copyright 2016 fatedier, fatedier@gmail.com
//
// 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 server
import (
"html/template"
"net/http"
"path"
"frp/models/metric"
"frp/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")))
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)
}
}

View File

@@ -63,7 +63,13 @@ func NewProxyServerFromCtlMsg(req *msg.ControlReq) (p *ProxyServer) {
p.PrivilegeMode = req.PrivilegeMode
p.PrivilegeToken = PrivilegeToken
p.BindAddr = BindAddr
p.ListenPort = req.RemotePort
if p.Type == "tcp" {
p.ListenPort = req.RemotePort
} else if p.Type == "http" {
p.ListenPort = VhostHttpPort
} else if p.Type == "https" {
p.ListenPort = VhostHttpsPort
}
p.CustomDomains = req.CustomDomains
p.HostHeaderRewrite = req.HostHeaderRewrite
return

View File

@@ -131,7 +131,6 @@ func (l *Listener) Accept() (*conn.Conn, error) {
// if rewriteFunc is exist and rewriteHost is set
// rewrite http requests with a modified host header
if l.mux.rewriteFunc != nil && l.rewriteHost != "" {
fmt.Printf("host rewrite: %s\n", l.rewriteHost)
sConn, err := l.mux.rewriteFunc(conn, l.rewriteHost)
if err != nil {
return nil, fmt.Errorf("http host header rewrite failed")