Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed file using golang embed filesystem #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.13-alpine AS builder
FROM golang:1.16-alpine AS builder

ENV GO111MODULE=on

Expand All @@ -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"]
2 changes: 1 addition & 1 deletion core/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gusaul/grpcox

go 1.12
go 1.16

require (
github.com/fullstorydev/grpcurl v1.3.2
Expand Down
31 changes: 20 additions & 11 deletions grpcox.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"flag"
"fmt"
"log"
"net/http"
Expand All @@ -14,21 +15,29 @@ import (
"github.com/gusaul/grpcox/handler"
)

var (
logfile string
port int
)

func main() {
flag.StringVar(&logfile, "log", "", "Specify log file")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should have default value to log/grpcox.log

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 != "" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont need to check if have default value

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)
Copy link
Owner

@gusaul gusaul May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is breaking changes, we use env variable as config now, its should not completely replace, just make it replace if only being set by flag args

muxRouter := mux.NewRouter()
handler.Init(muxRouter)
var wait time.Duration = time.Second * 15
Expand Down Expand Up @@ -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())
}
Expand Down
9 changes: 4 additions & 5 deletions handler/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion handler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions index/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package index

import "embed"

//go:embed css/* font/* img/* js/* index.html
var FS embed.FS