Minimal Chi-based static app template — drop your frontend in
./publicand serve it under a configurable path. Automatically redirects/to your app root.
.
├── README.md
├── go.mod
├── go.sum
├── main.go # entrypoint, config-aware server
├── public
│ └── index.html # your frontend app
└── src
└── config
├── conf.go # defines Config struct
└── load.go # env loader
main.goloads configuration from environment variables (likeAPP_PATH,APP_PORT).- Serves
./publicfolder at the mount point (APP_PATH). - Automatically redirects
/→APP_PATH(if it’s not/).
- Set environment variables:
export APP_PATH="/myprefix/"
export APP_PORT="3000"- Run the server:
go run main.go- Visit:
http://localhost:3000/→ redirects to/myprefix/http://localhost:3000/myprefix/→ serves./public/index.htmlhttp://localhost:3000/myprefix/js/main.js→ serves other static assets
package config
type Config struct {
APP_PATH string // required, must end with "/"
APP_PORT string // optional, default "3000"
}- Reads
APP_PATHandAPP_PORTfrom environment variables. - Fallback to defaults if optional values are missing.
package main
import (
"const-aka/src/config"
"fmt"
"log"
"net/http"
"github.com/go-chi/chi/v5"
)
func main() {
var cfg config.Config
if err := config.Load(&cfg); err != nil {
log.Fatal(err)
}
r := chi.NewRouter()
// redirect / → APP_PATH
if cfg.APP_PATH != "/" {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, cfg.APP_PATH, http.StatusFound)
})
}
// serve static files under APP_PATH
fs := http.FileServer(http.Dir("./public"))
r.Handle(cfg.APP_PATH+"*", http.StripPrefix(cfg.APP_PATH, fs))
fmt.Printf("Server running at http://localhost:%s%s\n", cfg.APP_PORT, cfg.APP_PATH)
log.Fatal(http.ListenAndServe(":"+cfg.APP_PORT, r))
}APP_PATHmust end with/for proper routing.- The template is ready for multiple frontends if you copy
r.Handle(...)blocks for different folders. - Perfect for rapid frontend prototyping without touching boilerplate.