Golang embeded files with apache

Hello everybody.

This is a simple web application witch uses embedded fs for storing css and js files

package main

import (
	"embed"
	"io/fs"
	"net/http"
)

//go:embed static/*
var StaticFiles embed.FS

type StaticPath struct {
	StaticP embed.FS
}

func (s StaticPath) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	publicFS, _ := fs.Sub(s.StaticP, "static")
	http.FileServer(http.FS(publicFS)).ServeHTTP(w, req)
}

func NewRouter() *http.ServeMux {
	r := http.NewServeMux()
	r.Handle("/", StaticPath{
		StaticP: StaticFiles,
	})
	return r
}

func main() {
	srv := &http.Server{
		Addr:    "localhost:3000",
		Handler: NewRouter(),
	}

	_ = srv.ListenAndServe()
}

directory struccture:

  β”œβ”€β”€ go.mod
  β”œβ”€β”€ go.sum
  β”œβ”€β”€ main.go
  └── static
      β”œβ”€β”€ index.html
      └── style.css

2 directories, 5 files
apache 2 configuration is: (cat /etc/apache2/conf-enabled/app1.conf)

<VirtualHost *:80>
    ProxyPass /app1 http://localhost:3000/
    ProxyPassReverse /app1 http://localhost:3000/
</VirtualHost>

Html page served fine but css and js files in static folder not.
I do not have a clue where the problem is. Could somebody help me?

I don’t know anything about embedded files or apache, but I do know that checking errors instead of ignoring them usually gives a clue.

1 Like