skip to Main Content

So. I have a stucture like this:

app
-api
-templates
-examples
-html

using echo like this

e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
        Root:       "examples/html",
        Browse:     true,
        IgnoreBase: true,
    }))

and it works perfect when I run it locally

but when i put this in docker-container
then i get error trying to get fonts and other params for page

2023-05-03T19:14:48Z ERR error="code=400, message=failed to parse page id: invalid UUID length: 16" environment=dev latency=0 method=GET path=/i/blocks/index.css query= version=v0.0.0

/i/ – is the group path in api
locally it gets handled by IgnoreBase: true in middleware.StaticConfig above

not so in docker

here’s part of the docker file after build:

RUN go build myApp

FROM debian:buster

WORKDIR /app

COPY templates/ templates/
COPY examples/html/ examples/html/
COPY --from=build_stage /app/app-server /app/app-server

EXPOSE 8080

ENTRYPOINT [ "/app/app-server"]

everything else works perfect, it sees templates, gets info from them, but fails to get statics from examples/html

P>S> would be perfect if solution uses go:embed, but just making it run properly would be great enough )))

P>P>S>
There is a template that contains <link rel="stylesheet" href="./blocks/index.css">
to get the page I call Get http://localhost:8080/i/:id
through middleware it should call examples/html/blocks/index.css
but instead it calls to /i/blocks/index.css

as mentioned above it works perfect when i run the app locally, but when it is in a container it fails with mistake above, because middleware doesn’t remove junk from the path like it does when run locally.

UPD: it stopped working locally too. Now I don’t understand anything.

2

Answers


  1. Chosen as BEST ANSWER

    Closing this issue. My guess there was a problem in cache or smth. Because through middleware it doesn't work locally either.

    It works like this

    e := echo.New()
    e.Static("/i/blocks", "examples/html/blocks")
    e.Static("/i/assets", "examples/html/assets")
    e.Static("/i/blocks/body/examples/html/assets", "examples/html/assets")
    e.Static("/i/blocks/logo/examples/html/assets", "examples/html/assets")
    

    both locally and through container will post separate question on how to make it look good


  2. Based on the information you provided, it seems like the issue might be related to the path resolution in the Docker container. Let’s try to fix it by using the absolute path for the Root configuration in the middleware.StaticConfig:

    1. Modify your main.go to include the embed package and use the embed.FS to serve your static files:
    package main
    
    import (
        _ "embed"
        "github.com/labstack/echo/v4"
        "github.com/labstack/echo/v4/middleware"
        "net/http"
        "os"
        "path/filepath"
    )
    
    //go:embed examples/html
    var html embed.FS
    
    func main() {
        e := echo.New()
    
        absPath, _ := filepath.Abs("examples/html")
        e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
            Root:       absPath,
            Filesystem: http.FS(html),
            Browse:     true,
            IgnoreBase: true,
        }))
    
        // Other routes and middlewares
    
        e.Start(":8080")
    }
    
    
    1. Modify your Dockerfile to include the necessary files:
    # Build stage
    FROM golang:1.17 as build_stage
    
    WORKDIR /app
    
    COPY go.mod .
    COPY go.sum .
    RUN go mod download
    
    COPY . .
    
    RUN go build -o app-server .
    
    # Final stage
    FROM debian:buster
    
    WORKDIR /app
    
    COPY templates/ templates/
    COPY examples/html/ examples/html/
    COPY --from=build_stage /app/app-server /app/app-server
    
    EXPOSE 8080
    
    ENTRYPOINT [ "/app/app-server"]
    
    

    With these changes, your application should be able to serve the static files correctly when running inside a Docker container.

    This solution uses the embed package and the http.FS for serving static files. It also makes sure that the absolute path is used for the Root configuration in the middleware.StaticConfig.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search