mirror of
https://github.com/gusaul/grpcox.git
synced 2024-12-26 02:40:10 +00:00
Embed file using golang embed filesystem
This commit is contained in:
parent
c83858ebfc
commit
58019cca30
|
@ -1,4 +1,4 @@
|
||||||
FROM golang:1.13-alpine AS builder
|
FROM golang:1.16-alpine AS builder
|
||||||
|
|
||||||
ENV GO111MODULE=on
|
ENV GO111MODULE=on
|
||||||
|
|
||||||
|
@ -11,8 +11,7 @@ RUN go mod tidy && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o grpcox grpc
|
||||||
|
|
||||||
FROM alpine
|
FROM alpine
|
||||||
|
|
||||||
COPY ./index /index
|
|
||||||
COPY --from=builder /src/grpcox ./
|
COPY --from=builder /src/grpcox ./
|
||||||
RUN mkdir /log
|
RUN mkdir /log
|
||||||
EXPOSE 6969
|
EXPOSE 6969
|
||||||
ENTRYPOINT ["./grpcox"]
|
ENTRYPOINT ["./grpcox", "-log", "log/grpcox.log"]
|
||||||
|
|
|
@ -60,7 +60,7 @@ func Test_prepareImport(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := prepareImport(tt.args.proto, ""); !reflect.DeepEqual(got, tt.want) {
|
if got := prepareImport(tt.args.proto); !reflect.DeepEqual(got, tt.want) {
|
||||||
t.Errorf("prepareImport() = %v, want %v",
|
t.Errorf("prepareImport() = %v, want %v",
|
||||||
string(got),
|
string(got),
|
||||||
string(tt.want))
|
string(tt.want))
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
||||||
module github.com/gusaul/grpcox
|
module github.com/gusaul/grpcox
|
||||||
|
|
||||||
go 1.12
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/fullstorydev/grpcurl v1.3.2
|
github.com/fullstorydev/grpcurl v1.3.2
|
||||||
|
|
31
grpcox.go
31
grpcox.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -14,21 +15,29 @@ import (
|
||||||
"github.com/gusaul/grpcox/handler"
|
"github.com/gusaul/grpcox/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
logfile string
|
||||||
|
port int
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.StringVar(&logfile, "log", "", "Specify log file")
|
||||||
|
flag.IntVar(&port, "port", 6969, "Specify port server")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
// logging conf
|
// logging conf
|
||||||
f, err := os.OpenFile("log/grpcox.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
if logfile != "" {
|
||||||
if err != nil {
|
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
log.Fatalf("error opening file: %v", err)
|
if err != nil {
|
||||||
|
log.Fatalf("error opening file: %v", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
log.SetOutput(f)
|
||||||
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
|
||||||
log.SetOutput(f)
|
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
||||||
|
|
||||||
// start app
|
// start app
|
||||||
addr := "0.0.0.0:6969"
|
addr := fmt.Sprintf(":%d", port)
|
||||||
if value, ok := os.LookupEnv("BIND_ADDR"); ok {
|
|
||||||
addr = value
|
|
||||||
}
|
|
||||||
muxRouter := mux.NewRouter()
|
muxRouter := mux.NewRouter()
|
||||||
handler.Init(muxRouter)
|
handler.Init(muxRouter)
|
||||||
var wait time.Duration = time.Second * 15
|
var wait time.Duration = time.Second * 15
|
||||||
|
@ -59,7 +68,7 @@ func main() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), wait)
|
ctx, cancel := context.WithTimeout(context.Background(), wait)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err = removeProtos()
|
err := removeProtos()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while removing protos: %s", err.Error())
|
log.Printf("error while removing protos: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,11 +24,10 @@ func Init(router *mux.Router) {
|
||||||
// close active connection
|
// close active connection
|
||||||
router.HandleFunc("/active/close/{host}", corsHandler(h.closeActiveConns)).Methods(http.MethodDelete, http.MethodOptions)
|
router.HandleFunc("/active/close/{host}", corsHandler(h.closeActiveConns)).Methods(http.MethodDelete, http.MethodOptions)
|
||||||
|
|
||||||
assetsPath := "index"
|
router.PathPrefix("/css/").Handler(http.FileServer(http.FS(fs)))
|
||||||
router.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir(assetsPath+"/css/"))))
|
router.PathPrefix("/js/").Handler(http.FileServer(http.FS(fs)))
|
||||||
router.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(http.Dir(assetsPath+"/js/"))))
|
router.PathPrefix("/font/").Handler(http.FileServer(http.FS(fs)))
|
||||||
router.PathPrefix("/font/").Handler(http.StripPrefix("/font/", http.FileServer(http.Dir(assetsPath+"/font/"))))
|
router.PathPrefix("/img/").Handler(http.FileServer(http.FS(fs)))
|
||||||
router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir(assetsPath+"/img/"))))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func corsHandler(h http.HandlerFunc) http.HandlerFunc {
|
func corsHandler(h http.HandlerFunc) http.HandlerFunc {
|
||||||
|
|
|
@ -5,16 +5,20 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/gusaul/grpcox/index"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
reGetFuncArg *regexp.Regexp
|
reGetFuncArg *regexp.Regexp
|
||||||
indexHTML *template.Template
|
indexHTML *template.Template
|
||||||
|
|
||||||
|
fs = index.FS
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
reGetFuncArg = regexp.MustCompile("\\( (.*) \\) returns")
|
reGetFuncArg = regexp.MustCompile("\\( (.*) \\) returns")
|
||||||
indexHTML = template.Must(template.New("index.html").Delims("{[", "]}").ParseFiles("index/index.html"))
|
indexHTML = template.Must(template.New("index.html").Delims("{[", "]}").ParseFS(fs, "index.html"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response - Standar ajax Response
|
// Response - Standar ajax Response
|
||||||
|
|
6
index/embed.go
Normal file
6
index/embed.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package index
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed css/* font/* img/* js/* index.html
|
||||||
|
var FS embed.FS
|
Loading…
Reference in New Issue
Block a user