diff --git a/loopback/loopback.go b/loopback/loopback.go index cde595a..dab760a 100644 --- a/loopback/loopback.go +++ b/loopback/loopback.go @@ -30,13 +30,13 @@ func (s *Server) Serve(ctx context.Context) { WriteTimeout: 1 * time.Second, ReadTimeout: 1 * time.Second, IdleTimeout: 5 * time.Second, - }) + }) app.Use( recover.New(), serverutils.ProvideUserCtxMiddleware(ctx), ) - + conf := config.Get(ctx) servingAddr := fmt.Sprintf("127.0.0.1:%d", conf.LoopbackPort) @@ -75,7 +75,7 @@ func GetBundleFileHandler(c *fiber.Ctx) error { ctx := c.UserContext() - bundleIdFromRoute := c.Params(bundleIdKey) + bundleIdFromRoute := c.Params(bundleIdKey) urlPathPrefix := BundlePath + "/" + bundleIdFromRoute + "/" @@ -108,7 +108,6 @@ func GetBundleFileHandler(c *fiber.Ctx) error { Error(). Msg("cant find bundle") - return c.SendStatus(http.StatusNotFound) } @@ -124,11 +123,10 @@ func GetBundleFileHandler(c *fiber.Ctx) error { Err(err). Msg("cant get file") - return c.SendStatus(http.StatusInternalServerError) } defer fileReader.Close() - c.Set(fiber.HeaderContentType, mimeType) + c.Set(fiber.HeaderContentType, mimeType) return c.SendStream(fileReader) } diff --git a/server/handlers/utils.go b/server/handlers/utils.go index 2f904ac..473f126 100644 --- a/server/handlers/utils.go +++ b/server/handlers/utils.go @@ -11,8 +11,8 @@ import ( ) func writePdf(c *fiber.Ctx, data io.Reader) error { - ctx:=c.UserContext() - + ctx := c.UserContext() + if data == nil { log.Ctx(ctx).Info().Msg("nothing to writeout: pdf data empty") return c.SendStatus(http.StatusNoContent) diff --git a/server/server.go b/server/server.go index a720185..3bef27f 100644 --- a/server/server.go +++ b/server/server.go @@ -45,14 +45,14 @@ func (s *Server) Serve(ctx context.Context) { WriteTimeout: 45 * time.Second, ReadTimeout: 45 * time.Second, IdleTimeout: 60 * time.Second, - BodyLimit: conf.MaxBodySizeInMb * 1024 * 1024, - }) + BodyLimit: conf.MaxBodySizeInMb * 1024 * 1024, + }) app.Use( cors.New(cors.Config{ - AllowOrigins: "*", - AllowHeaders: "*", - AllowMethods: strings.Join([]string{http.MethodGet, http.MethodPost}, ","), + AllowOrigins: "*", + AllowHeaders: "*", + AllowMethods: strings.Join([]string{http.MethodGet, http.MethodPost}, ","), AllowCredentials: false, }), recover.New(), @@ -136,9 +136,9 @@ func servePlaygroundFronted(app *fiber.App) { return ctx.SendFile(config.PathStaticExternPlayground + ctx.Path()) }) app.Get("/favicon.ico", func(ctx *fiber.Ctx) error { - return ctx.SendFile(config.PathStaticExternPlayground+"favicon.ico") + return ctx.SendFile(config.PathStaticExternPlayground + "favicon.ico") }) app.Get("/*", func(ctx *fiber.Ctx) error { - return ctx.SendFile(config.PathStaticExternPlayground+"index.html") + return ctx.SendFile(config.PathStaticExternPlayground + "index.html") }) } diff --git a/serverutils/middleware.go b/serverutils/middleware.go index bc9cb6d..7ab9880 100644 --- a/serverutils/middleware.go +++ b/serverutils/middleware.go @@ -25,11 +25,11 @@ func ProvideUserCtxMiddleware(ctx context.Context) func(c *fiber.Ctx) error { if requestCtx != nil { go func() { select { - case <- requestCtx.Done(): - cancel() - return - case <-combinedCtx.Done(): - return + case <-requestCtx.Done(): + cancel() + return + case <-combinedCtx.Done(): + return } }() } @@ -88,7 +88,7 @@ func RecoverMiddleware() func(c *fiber.Ctx) error { logRequestErr(c, rec) } }() - + err := c.Next() if err != nil { diff --git a/services/renderer/headlesschromium/headless-chromium.go b/services/renderer/headlesschromium/headless-chromium.go index 0411b1b..5ac2595 100644 --- a/services/renderer/headlesschromium/headless-chromium.go +++ b/services/renderer/headlesschromium/headless-chromium.go @@ -29,13 +29,13 @@ func NewChromiumBrowser(ctx context.Context) (context.Context, context.CancelFun chromedp.Flag("mute-audio", true), ) - if (config.Get(ctx).NoSandbox) { + if config.Get(ctx).NoSandbox { opts = append( opts, chromedp.Flag("no-sandbox", true), ) } - + allocCtx, cancelAllocCtx := chromedp.NewExecAllocator(ctx, opts...) cctx, cancelCctx := chromedp.NewContext(allocCtx) diff --git a/services/renderer/renderer-service_test.go b/services/renderer/renderer-service_test.go index eea4321..fd7e569 100644 --- a/services/renderer/renderer-service_test.go +++ b/services/renderer/renderer-service_test.go @@ -119,7 +119,7 @@ func TestWorkerBeyondTheLimit(t *testing.T) { for i := 0; i < jobCount-workerInstances; i++ { <-rendererMock.HitRenderChan } - + <-time.After(50 * time.Millisecond) if currLen := len(service.workerSlots); currLen != jobCount-workerInstances { diff --git a/utils/html-css.go b/utils/html-css.go index 555d291..e716039 100644 --- a/utils/html-css.go +++ b/utils/html-css.go @@ -45,7 +45,7 @@ func MergeCss(css ...*string) *string { var urlReferenceRegex = regexp.MustCompile(`(\ssrc="|\shref="|src:\s*(local\(.*\),)?\s*url\(["'])([^"']+)(["']\)|")`) type HttpClientExecuter interface { - Do(req *http.Request) (*http.Response, error) + Do(req *http.Request) (*http.Response, error) } func RequestAndInlineAllHtmlResources(ctx context.Context, htmlPtr *string, baseUrl string) *string { @@ -103,10 +103,10 @@ func requestAndReturnBase64IfPossible(ctx context.Context, htmlAttribute string, base64 := base64.StdEncoding.EncodeToString(bytes) isCssSrcUrl := strings.HasSuffix(prefix, `url("`) - - if (isCssSrcUrl) { - prefix = strings.TrimRight(prefix,`"`) - suffix = strings.TrimLeft(suffix,`"`) + + if isCssSrcUrl { + prefix = strings.TrimRight(prefix, `"`) + suffix = strings.TrimLeft(suffix, `"`) } return prefix + "data:" + mimeType + ";base64," + base64 + suffix diff --git a/utils/html-css_test.go b/utils/html-css_test.go index 57b4e68..10f220c 100644 --- a/utils/html-css_test.go +++ b/utils/html-css_test.go @@ -20,7 +20,7 @@ func TestAppendStyleToHtml(t *testing.T) { output := *outputPtr - if !strings.HasSuffix(output, "") { + if !strings.HasSuffix(output, "") { t.Fatal("Result should have appended css") } if !strings.HasPrefix(output, inputHtml) { @@ -38,7 +38,7 @@ func TestAppendStyleToHtmlReturnsEmptyWithNilInput(t *testing.T) { t.Fatal("Result should not be nil") } - if (*outputPtr != emptyString) { + if *outputPtr != emptyString { t.Fatal("Result should be empty") } } @@ -48,8 +48,7 @@ func TestMergeCss(t *testing.T) { css2 := ".test2 { color: blue; }" css3 := ".test3 { color: green; }" - expectedMergedCss := css1+css2+css3 - + expectedMergedCss := css1 + css2 + css3 outputPtr := MergeCss(&css1, &css2, &css3) @@ -65,17 +64,18 @@ func TestMergeCss(t *testing.T) { type HttpClientMock struct { RequestedResources []string } + func (c *HttpClientMock) Do(req *http.Request) (*http.Response, error) { c.RequestedResources = append(c.RequestedResources, req.URL.String()) - return &http.Response{ + return &http.Response{ Body: io.NopCloser(bytes.NewReader([]byte("foo"))), }, nil } func TestRequestAndInlineAllHtmlResources(t *testing.T) { inputHtml := "
Hello World " - + httpClientMock := &HttpClientMock{} ctx := context.WithValue(context.Background(), "httpClient", httpClientMock) @@ -102,4 +102,4 @@ func TestRequestAndInlineAllHtmlResources(t *testing.T) { if *outputPtr != expectedHtml { t.Fatalf("Result was not expected: %s != %s", *outputPtr, expectedHtml) } -} \ No newline at end of file +}