Serving files in golang server 404 page not found

Hello. I started using golang recently, and decided to try to deploy a simple app.

I followed the example at Run a Go App · Fly Docs and everything worked. So I thought next I would have it load an index.html page

I changed out the lines http.HandleFunc and put my index.html in with the example template

package main

import (
“embed”
“html/template”
“log”
“net/http”
“os”
)

//go:embed templates/*
var resources embed.FS

//var t = template.Must(template.ParseFS(resources, “templates/*”))

func main() {
port := os.Getenv(“PORT”)
if port == “” {
port = “8080”

}

http.HandleFunc(“/”, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, “./templates/index.html”)
})

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

I deploy again, but all I get is “404 page not found”

I can’t seem to figure out where I am going wrong. Everything works fine locally

After this step I wanted to try adding websockets. But I can’t even load a simple html page.

Looking for help or advice. Many thanks.

Shouldn’t you be using htmx? jk
If I had to guess, maybe the relative path of your file is different on your local vs your deployed instance.

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