You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From what I understand, raw responses and static files are already an option, but opening up the ability to return HTML via handlers could widen the gofr ecosystem.
I for example use HTMX and Alpine JS. I have no desire to use what others call modern frameworks. HTMX keeps my frontend incredibly simple and allows me to render what I want server side, which for me is important.
HTMX+gofr could be a great use case here. gofr already supports static files, so the index.html can still be static so to speak and be served via the AddStaticFiles method, but HTMX attributes can be added to the index page to perform AJAX requests on load e.g.
<!doctype html>
<html>
<head>
<title>This is the title of the webpage!</title>
<script src="https://unpkg.com/[email protected]" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
</head>
<body>
<p>This is an example paragraph. Anything in the <strong>body</strong> tag will appear on the page, just like this <strong>p</strong> tag and its contents.</p>
<div hx-get="/greet" hx-trigger="load"></div>
</body>
</html>
By supporting HTML responses, it means we can keep the entire stack within the gofr ecosystem, thus benefiting from observability, which I think is the most critical use case.
Originally I've been using gin to serve the frontend and then routing to gofr, but it's disjointed.
At the moment I'm actually being naughty and abusing the deprecated UseMiddlewareWithContainer method in order to render HTML templates :) (NOTE: The example below doesn't include code that makes use of the container, but I am fetching data from the DB and/or another registered service)
package main
import (
"html/template"
"net/http"
"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/container"
)
// Define middleware with container access
func middleware(c *container.Container, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Your custom logic here
// For example, logging, authentication, etc.
c.Debug(r)
c.Debug(r.Method)
c.Debug(r.URL)
if r.Method == "GET" {
switch r.URL.Path {
case "/greet":
tmpl := template.Must(template.ParseFiles("layout.html"))
tmpl.Execute(w, nil)
return
}
}
// Continue with the request processing
handler.ServeHTTP(w, r)
})
}
func main() {
// initialise gofr object
a := gofr.New()
a.UseMiddlewareWithContainer(middleware)
a.Run()
}
The text was updated successfully, but these errors were encountered:
Would you concider supporting HTML responses?
From what I understand, raw responses and static files are already an option, but opening up the ability to return HTML via handlers could widen the gofr ecosystem.
I for example use HTMX and Alpine JS. I have no desire to use what others call modern frameworks. HTMX keeps my frontend incredibly simple and allows me to render what I want server side, which for me is important.
HTMX+gofr could be a great use case here. gofr already supports static files, so the index.html can still be static so to speak and be served via the AddStaticFiles method, but HTMX attributes can be added to the index page to perform AJAX requests on load e.g.
By supporting HTML responses, it means we can keep the entire stack within the gofr ecosystem, thus benefiting from observability, which I think is the most critical use case.
Originally I've been using gin to serve the frontend and then routing to gofr, but it's disjointed.
At the moment I'm actually being naughty and abusing the deprecated UseMiddlewareWithContainer method in order to render HTML templates :) (NOTE: The example below doesn't include code that makes use of the container, but I am fetching data from the DB and/or another registered service)
The text was updated successfully, but these errors were encountered: