static files in golang app

Hi I am new to Fly, want to ask why my app can’t serve the static files after I deploy it, even though it is working fine in my local

I have already looked for the same problem → How to upload static files when deploying a go app?

but adding the [[statics]] section to my .toml didn’t work

here is my folder structure

fly.toml
main.go
static
| css
| | style.css
| js 
| | script.js
| index.html

and here is my .toml file

..
..

[[statics]]
  guest_path = "/app/static"
  url_prefix = "/static"

..
..

Can you try to add 2 statics sections, one with guest_path = “/app/static/css” and one with guest_path = “app/static/js” with the same url_prefix?

If that doesn’t work, a Fly.io employee will get to this soon.

I also found Run a Static Website,
but I would need to know more about what you are building here.

hhmm it still doesn’t work, btw these are my files

main.go

package main

import (
	"embed"
	"html/template"
	"log"
	"net/http"
	"os"
	"strings"
)

//go:embed static/*.html
var resources embed.FS

var t = template.Must(template.ParseFS(resources, "static/*.html"))

func main() {
	port := "8080"

	http.HandleFunc("/", index)

	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))

	log.Println("listening on", port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}

func index(w http.ResponseWriter, req *http.Request) {
	host := getHost(req)
	data := make(map[string]interface{})
	data["Host"] = host
	t.ExecuteTemplate(w, "index.html", data)
}

func getHost(req *http.Request) string {
	host := "https://" + req.Host
	if strings.Contains(req.Host, "localhost") {
		host = "http://" + req.Host
	}
	return host
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Go Web App</title>
    <link href="{{.Host}}/static/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="container mx-auto mt-10">
        <h1 class="text-2xl text-gray-800 font-bold">
            Hello World!
        </h1>
    </div>
    <script src="{{.Host}}/static/js/script.js"></script>
</body>
</html>

on my local all works fine, when I access localhost:8080 the page will render and it will load the CSS file

on the server (after I deploy), I can access the page but the static files (CSS, js) wouldn’t load

Since it is a Go app, did you go through Run a Go App · Fly Docs?

yes, I tried to follow the tutorial and it works

and now I want to add CSS file to my HTML (not in the tutorial), so I tweak the code but still can’t find the solution :thinking:

I was just going through it too, but apparently the cloned GitHub repo isn’t deploying as is.
The CLI just freezes in the image pulling phase in my Windows laptop.
App status is Pending in the Dashboard and no logs in Monitoring.

Did you read this?

ahhh this solves my issue, thanks a lot

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