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
|
||||
|
||||
|
@ -11,8 +11,7 @@ RUN go mod tidy && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o grpcox grpc
|
|||
|
||||
FROM alpine
|
||||
|
||||
COPY ./index /index
|
||||
COPY --from=builder /src/grpcox ./
|
||||
RUN mkdir /log
|
||||
EXPOSE 6969
|
||||
ENTRYPOINT ["./grpcox"]
|
||||
ENTRYPOINT ["./grpcox", "-log", "log/grpcox.log"]
|
||||
|
|
|
@ -60,7 +60,7 @@ func Test_prepareImport(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
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",
|
||||
string(got),
|
||||
string(tt.want))
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
|||
module github.com/gusaul/grpcox
|
||||
|
||||
go 1.12
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/fullstorydev/grpcurl v1.3.2
|
||||
|
|
31
grpcox.go
31
grpcox.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -14,21 +15,29 @@ import (
|
|||
"github.com/gusaul/grpcox/handler"
|
||||
)
|
||||
|
||||
var (
|
||||
logfile string
|
||||
port int
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&logfile, "log", "", "Specify log file")
|
||||
flag.IntVar(&port, "port", 6969, "Specify port server")
|
||||
flag.Parse()
|
||||
|
||||
// logging conf
|
||||
f, err := os.OpenFile("log/grpcox.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("error opening file: %v", err)
|
||||
if logfile != "" {
|
||||
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
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
|
||||
addr := "0.0.0.0:6969"
|
||||
if value, ok := os.LookupEnv("BIND_ADDR"); ok {
|
||||
addr = value
|
||||
}
|
||||
addr := fmt.Sprintf(":%d", port)
|
||||
muxRouter := mux.NewRouter()
|
||||
handler.Init(muxRouter)
|
||||
var wait time.Duration = time.Second * 15
|
||||
|
@ -59,7 +68,7 @@ func main() {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), wait)
|
||||
defer cancel()
|
||||
|
||||
err = removeProtos()
|
||||
err := removeProtos()
|
||||
if err != nil {
|
||||
log.Printf("error while removing protos: %s", err.Error())
|
||||
}
|
||||
|
|
|
@ -24,11 +24,10 @@ 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/"))))
|
||||
router.PathPrefix("/css/").Handler(http.FileServer(http.FS(fs)))
|
||||
router.PathPrefix("/js/").Handler(http.FileServer(http.FS(fs)))
|
||||
router.PathPrefix("/font/").Handler(http.FileServer(http.FS(fs)))
|
||||
router.PathPrefix("/img/").Handler(http.FileServer(http.FS(fs)))
|
||||
}
|
||||
|
||||
func corsHandler(h http.HandlerFunc) http.HandlerFunc {
|
||||
|
|
|
@ -5,16 +5,20 @@ import (
|
|||
"html/template"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/gusaul/grpcox/index"
|
||||
)
|
||||
|
||||
var (
|
||||
reGetFuncArg *regexp.Regexp
|
||||
indexHTML *template.Template
|
||||
|
||||
fs = index.FS
|
||||
)
|
||||
|
||||
func init() {
|
||||
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
|
||||
|
|
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