From 280dfe53bb37a26c73b879e600ce3c43c288da44 Mon Sep 17 00:00:00 2001 From: kevkevinpal Date: Sun, 22 Dec 2024 18:00:28 -0500 Subject: [PATCH] moved xgo to be used on all routes and internalServerMiddleware --- Dockerfile | 8 ++++++-- routes/index.go | 16 +++++++++++++++ routes/people.go | 9 ++++---- routes/person.go | 3 +-- utils/logger.go | 53 ------------------------------------------------ utils/trace.go | 28 ------------------------- 6 files changed, 27 insertions(+), 90 deletions(-) delete mode 100644 utils/logger.go delete mode 100644 utils/trace.go diff --git a/Dockerfile b/Dockerfile index d840376cd..3b80b05b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,11 +8,15 @@ WORKDIR /app COPY go.mod . COPY go.sum . +# Installing xgo +RUN go install github.com/xhd2015/xgo/cmd/xgo@latest +ENV PATH="/usr/local/xgo/bin:${PATH}" + RUN go mod download COPY . . -RUN CGO_ENABLED=0 go build +RUN CGO_ENABLED=0 xgo build # final stage FROM alpine:latest @@ -24,4 +28,4 @@ COPY --from=builder /app/sphinx-tribes /app/ RUN ls app EXPOSE 5002 -ENTRYPOINT ["/app/sphinx-tribes"] \ No newline at end of file +ENTRYPOINT ["/app/sphinx-tribes"] diff --git a/routes/index.go b/routes/index.go index 2347dcca9..c6ac07f2e 100644 --- a/routes/index.go +++ b/routes/index.go @@ -9,11 +9,15 @@ import ( "os" "runtime" "time" + "strings" + "context" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/rs/cors" "github.com/urfave/negroni" + "github.com/xhd2015/xgo/runtime/core" + "github.com/xhd2015/xgo/runtime/trap" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/config" @@ -195,6 +199,18 @@ func internalServerErrorHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rr := negroni.NewResponseWriter(w) + trap.AddInterceptor(&trap.Interceptor{ + Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) { + index := strings.Index(f.File, "sphinx-tribes") + trimmed := f.File + if index != -1 { + trimmed = f.File[index:] + } + logger.Log.Machine("%s:%d %s\n", trimmed, f.Line, f.Name) + + return nil, nil + }, + }) defer func() { if err := recover(); err != nil { // Get stack trace diff --git a/routes/people.go b/routes/people.go index 9a9d61f8f..59d6b0480 100644 --- a/routes/people.go +++ b/routes/people.go @@ -6,7 +6,6 @@ import ( "github.com/go-chi/chi" "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" - "github.com/stakwork/sphinx-tribes/utils" ) func PeopleRoutes() chi.Router { @@ -15,10 +14,10 @@ func PeopleRoutes() chi.Router { peopleHandler := handlers.NewPeopleHandler(db.DB) r.Group(func(r chi.Router) { - r.Get("/", utils.AutoLog(peopleHandler.GetListedPeople)) - r.Get("/search", utils.AutoLog(peopleHandler.GetPeopleBySearch)) - r.Get("/posts", utils.AutoLog(handlers.GetListedPosts)) - r.Get("/wanteds/assigned/{uuid}", utils.AutoLog(bountyHandler.GetPersonAssignedBounties)) + r.Get("/", peopleHandler.GetListedPeople) + r.Get("/search", peopleHandler.GetPeopleBySearch) + r.Get("/posts", handlers.GetListedPosts) + r.Get("/wanteds/assigned/{uuid}", bountyHandler.GetPersonAssignedBounties) r.Get("/wanteds/created/{uuid}", bountyHandler.GetPersonCreatedBounties) r.Get("/wanteds/header", handlers.GetWantedsHeader) r.Get("/short", handlers.GetPeopleShortList) diff --git a/routes/person.go b/routes/person.go index 5cb45c9bd..1b4246563 100644 --- a/routes/person.go +++ b/routes/person.go @@ -5,7 +5,6 @@ import ( "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" - "github.com/stakwork/sphinx-tribes/utils" ) func PersonRoutes() chi.Router { @@ -14,7 +13,7 @@ func PersonRoutes() chi.Router { r.Group(func(r chi.Router) { r.Get("/{pubkey}", peopleHandler.GetPersonByPubkey) r.Get("/id/{id}", peopleHandler.GetPersonById) - r.Get("/uuid/{uuid}", utils.AutoLog(peopleHandler.GetPersonByUuid)) + r.Get("/uuid/{uuid}", peopleHandler.GetPersonByUuid) r.Get("/uuid/{uuid}/assets", handlers.GetPersonAssetsByUuid) r.Get("/githubname/{github}", handlers.GetPersonByGithubName) }) diff --git a/utils/logger.go b/utils/logger.go deleted file mode 100644 index e4685b0d4..000000000 --- a/utils/logger.go +++ /dev/null @@ -1,53 +0,0 @@ -package utils - -import ( - "log" - "os" - "github.com/stakwork/sphinx-tribes/config" -) - -type Logger struct { - infoLogger *log.Logger - warningLogger *log.Logger - errorLogger *log.Logger - debugLogger *log.Logger - machineLogger *log.Logger -} - -var Log = Logger{ - infoLogger: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile), - warningLogger: log.New(os.Stdout, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile), - errorLogger: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile), - debugLogger: log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile), - machineLogger: log.New(os.Stdout, "MACHINE: ", log.Ldate|log.Ltime|log.Lshortfile), -} - -func (l *Logger) Machine(format string, v ...interface{}) { - if config.LogLevel == "MACHINE" { - l.machineLogger.Printf(format, v...) - } -} - -func (l *Logger) Debug(format string, v ...interface{}) { - if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" { - l.debugLogger.Printf(format, v...) - } -} - -func (l *Logger) Info(format string, v ...interface{}) { - if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" { - l.infoLogger.Printf(format, v...) - } -} - -func (l *Logger) Warning(format string, v ...interface{}) { - if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" { - l.warningLogger.Printf(format, v...) - } -} - -func (l *Logger) Error(format string, v ...interface{}) { - if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" || config.LogLevel == "ERROR" { - l.errorLogger.Printf(format, v...) - } -} diff --git a/utils/trace.go b/utils/trace.go deleted file mode 100644 index 650603613..000000000 --- a/utils/trace.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "context" - "net/http" - "fmt" - "strings" - "github.com/xhd2015/xgo/runtime/core" - "github.com/xhd2015/xgo/runtime/trap" -) - -func AutoLog(fn http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - trap.AddInterceptor(&trap.Interceptor{ - Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) { - index := strings.Index(f.File, "sphinx-tribes") - trimmed := f.File - if index != -1 { - trimmed = f.File[index:] - } - fmt.Printf("%s:%d %s\n", trimmed, f.Line, f.Name) - - return nil, nil - }, - }) - fn(w, r) - } -}