htmgo/examples/hackernews/main.go

37 lines
787 B
Go
Raw Normal View History

2024-10-10 22:00:20 +00:00
package main
import (
2024-10-11 02:01:39 +00:00
"fmt"
2024-10-10 22:00:20 +00:00
"github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service"
"hackernews/__htmgo"
"io/fs"
"net/http"
)
func main() {
locator := service.NewLocator()
h.Start(h.AppOpts{
ServiceLocator: locator,
LiveReload: true,
Register: func(app *h.App) {
sub, err := fs.Sub(GetStaticAssets(), "assets/dist")
if err != nil {
panic(err)
}
http.FileServerFS(sub)
2024-10-11 02:01:39 +00:00
app.Router.Handle("/item", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
w.Header().Set("Location", fmt.Sprintf("/?item=%s", id))
w.WriteHeader(302)
}))
2024-10-10 22:00:20 +00:00
app.Router.Handle("/public/*", http.StripPrefix("/public", http.FileServerFS(sub)))
__htmgo.Register(app.Router)
},
})
}