Can't serve static files using golang

Hi fly io newbie right here.

I want to serve a static json files in https://{host}/.well-known/assetlinks.json

This is my current folder files directory structure

.well-known
|  assetlinks.json
server
|  main.go

This is the code for server/main.go

package main

import (
	"net/http"
	"os"

	"github.com/charmbracelet/log"
)

func main() {

	port := os.Getenv("PORT")

	if port == "" {
		port = "8080"
	}

	http.Handle("/.well-known/",
		http.StripPrefix("/.well-known/",
			http.FileServer(http.Dir(".well-known"))),
	)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello, World"))
	})

	log.Info("App running in port :" + port)
	err := http.ListenAndServe(":"+port, nil)
	if err != nil {
		log.Fatal(err)
	}

}

This is my fly.toml file

# fly.toml app configuration file generated for receiptory-server on 2024-06-13T22:57:55+08:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = {app name}
primary_region = 'sin'

[build]
  [build.args]
    GO_VERSION = '1.22.2'
    BP_KEEP_FILES = '.well-known/*'

[env]
  PORT = '8080'

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[[vm]]
  memory = '1gb'
  cpu_kind = 'shared'
  cpus = 1

This is my Dockerfile

ARG GO_VERSION=1
FROM golang:${GO_VERSION}-bookworm as builder

WORKDIR /usr/src/app
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o /run-app ./server/main.go


FROM debian:bookworm

COPY --from=builder /run-app /usr/local/bin/
CMD ["run-app"]

From what I read here, I already specify in BP_KEEP_FILES to keep the .well-known dir. But for some reason it’s not served there.

Feeling abit loss, is there any other documentation site out there? There is a lot of unknown that need to dig into the community to find out. For instance there is no mention of paketo in the golang docs, is it stil being used right now?

If you have a Dockerfile, flyctl will use that to build your app instead of a buildpack. It doesn’t look like you’re copying assetlinks.json from the builder stage to your final stage. You’ll need that file in your final docker image if you want to serve it.

1 Like

Thank you so much, guess skip learning docker before using fly.io is a bad idea.

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