Add directory to go project

I launched a go app by following the instructions in the tutorial section. In the go app I am serving html using the following code.

`http.Handle("/", http.FileServer(http.Dir("./frontend")))`

Which takes the html files in the frontend directory and serves it to the client. How do i include the frontend folder in the fly deployment.

Note I have tried static files, but that does work

There’s two options you can use with Go. If you want to simply add your files to your deployment, you’ll need to use an ADD command to your Dockerfile so that they’re included in your deployed image:

ADD ./frontend frontend

The other option is to bundle files directly into your Go binary by using the go:embed directive. If you use that option, then your files will always be available any time you run your program since they are embedded into the program itself.

//go:embed frontend
var frontendFS embed.FS

You can then serve the directory like this:

fsys, err := fs.Sub(frontendFS, "frontend")
if err != nil {
    return err
http.Handle("/", http.FileServer(http.FS(fsys)))

Thanks I was able to get the second solution to work!!!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.