1
0
mirror of https://github.com/gusaul/grpcox.git synced 2025-04-17 19:25:37 +00:00

Change assets to assetfs

This commit is contained in:
annlumia 2023-01-20 18:41:58 +07:00
parent 40bca6e2af
commit 2dafcd5024
8 changed files with 1167 additions and 23 deletions

17
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"output": "debug.exe"
}
]
}

View File

@ -43,5 +43,50 @@ configure app preferences by editing `config.env` file
set value `0 (zero)` to disable auto close idle connection.
## Demo
![gRPCox Demo](https://raw.githubusercontent.com/gusaul/grpcox/master/index/img/demogrpcox.gif)
## Using AssetFs
By using assetfs this tool can be compiled into 1 binary file, so that it will be easier to use.
Please update assetfs when any files inside index folder are modified:
```shell
# Install required package
$ go get github.com/go-bindata/go-bindata/...
$ go get github.com/elazarl/go-bindata-assetfs/...
# Update assetfs
$ go-bindata-assetfs -pkg handler -o handler/binddata.go index/...
```
### Compile
if you have golang installed on your local machine, just run command
```shell
# Windows
go build -o grpcox.exe
# Unix
go build -o grpcox
```
from grpcox directory
configure app preferences by editing `config.env` file
| var | usage | type | unit |
|-----------------|---------------------------------------------|--------|--------|
| MAX_LIFE_CONN | maximum idle time connection before closed | number | minute |
| TICK_CLOSE_CONN | ticker interval to sweep expired connection | number | second |
| BIND_ADDR | ip:port to bind service | string | |
set value `0 (zero)` to disable auto close idle connection.
### How to use?
Copy your binary file (ex: grpcox.exe) to your favorite folder, and just run it.

2
go.mod
View File

@ -3,7 +3,9 @@ module github.com/gusaul/grpcox
go 1.12
require (
github.com/elazarl/go-bindata-assetfs v1.0.1 // indirect
github.com/fullstorydev/grpcurl v1.3.2
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/gorilla/mux v1.7.0
github.com/jhump/protoreflect v1.5.0
google.golang.org/grpc v1.21.0

4
go.sum
View File

@ -1,8 +1,12 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/elazarl/go-bindata-assetfs v1.0.1 h1:m0kkaHRKEu7tUIUFVwhGGGYClXvyl4RE03qmvRTNfbw=
github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/fullstorydev/grpcurl v1.3.2 h1:cJKWsBYMocdxXQvgbnhtLG810SL5MhKT4K7BagxRih8=
github.com/fullstorydev/grpcurl v1.3.2/go.mod h1:kvk8xPCXOrwVd9zYdjy+xSOT4YWm6kyth4Y9NMfBns4=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=

1095
handler/binddata.go Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,6 @@
package handler
import (
"bytes"
"context"
"fmt"
"io/ioutil"
@ -25,18 +24,6 @@ func InitHandler() *Handler {
}
}
func (h *Handler) index(w http.ResponseWriter, r *http.Request) {
body := new(bytes.Buffer)
err := indexHTML.Execute(body, make(map[string]string))
if err != nil {
writeError(w, err)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(body.Bytes())
}
func (h *Handler) getActiveConns(w http.ResponseWriter, r *http.Request) {
response(w, h.g.GetActiveConns(context.TODO()))
}

View File

@ -3,6 +3,7 @@ package handler
import (
"net/http"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/mux"
)
@ -10,8 +11,6 @@ import (
func Init(router *mux.Router) {
h := InitHandler()
router.HandleFunc("/", h.index)
ajaxRoute := router.PathPrefix("/server/{host}").Subrouter()
ajaxRoute.HandleFunc("/services", corsHandler(h.getLists)).Methods(http.MethodGet, http.MethodOptions)
ajaxRoute.HandleFunc("/services", corsHandler(h.getListsWithProto)).Methods(http.MethodPost)
@ -24,11 +23,9 @@ func Init(router *mux.Router) {
// close active connection
router.HandleFunc("/active/close/{host}", corsHandler(h.closeActiveConns)).Methods(http.MethodDelete, http.MethodOptions)
assetsPath := "index"
router.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir(assetsPath+"/css/"))))
router.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(http.Dir(assetsPath+"/js/"))))
router.PathPrefix("/font/").Handler(http.StripPrefix("/font/", http.FileServer(http.Dir(assetsPath+"/font/"))))
router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir(assetsPath+"/img/"))))
// Serve assets with assetfs
fs := &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "index"}
router.PathPrefix("/").Handler(http.FileServer(fs))
}
func corsHandler(h http.HandlerFunc) http.HandlerFunc {

View File

@ -2,19 +2,16 @@ package handler
import (
"encoding/json"
"html/template"
"net/http"
"regexp"
)
var (
reGetFuncArg *regexp.Regexp
indexHTML *template.Template
)
func init() {
reGetFuncArg = regexp.MustCompile("\\( (.*) \\) returns")
indexHTML = template.Must(template.New("index.html").Delims("{[", "]}").ParseFiles("index/index.html"))
}
// Response - Standar ajax Response