diff --git a/Dockerfile b/Dockerfile index 5cc79bf..da4b3c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/core/resource_test.go b/core/resource_test.go index 7e053c2..b3764b3 100644 --- a/core/resource_test.go +++ b/core/resource_test.go @@ -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)) diff --git a/go.mod b/go.mod index acd0b7a..80623dc 100644 --- a/go.mod +++ b/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 diff --git a/grpcox.go b/grpcox.go index 47199bd..e659e2b 100644 --- a/grpcox.go +++ b/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()) } diff --git a/handler/routes.go b/handler/routes.go index 4e5c27c..87a10a2 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -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 { diff --git a/handler/types.go b/handler/types.go index 00b023a..0721fca 100644 --- a/handler/types.go +++ b/handler/types.go @@ -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 diff --git a/index/embed.go b/index/embed.go new file mode 100644 index 0000000..7f091db --- /dev/null +++ b/index/embed.go @@ -0,0 +1,6 @@ +package index + +import "embed" + +//go:embed css/* font/* img/* js/* index.html +var FS embed.FS