generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
g "github.com/maragudk/gomponents" | ||
c "github.com/maragudk/gomponents/components" | ||
. "github.com/maragudk/gomponents/html" | ||
ghttp "github.com/maragudk/gomponents/http" | ||
|
||
hx "github.com/maragudk/gomponents-htmx" | ||
hxhttp "github.com/maragudk/gomponents-htmx/http" | ||
) | ||
|
||
func main() { | ||
if err := start(); err != nil { | ||
log.Fatalln("Error:", err) | ||
} | ||
} | ||
|
||
func start() error { | ||
now := time.Now() | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) { | ||
if r.Method == http.MethodPost && hxhttp.IsBoosted(r.Header) { | ||
now = time.Now() | ||
|
||
hxhttp.SetPushURL(w.Header(), "/?time="+now.Format(timeFormat)) | ||
|
||
return partial(now), nil | ||
} | ||
return page(now), nil | ||
})) | ||
|
||
log.Println("Starting on http://localhost:8080") | ||
if err := http.ListenAndServe("localhost:8080", mux); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
const timeFormat = "15:04:05" | ||
|
||
func page(now time.Time) g.Node { | ||
return c.HTML5(c.HTML5Props{ | ||
Title: now.Format(timeFormat), | ||
Head: []g.Node{ | ||
Script(Src("https://cdn.tailwindcss.com?plugins=forms,typography")), | ||
Script(Src("https://unpkg.com/htmx.org")), | ||
}, | ||
Body: []g.Node{ | ||
Div(Class("max-w-7xl mx-auto p-4 prose lg:prose-lg xl:prose-xl"), | ||
H1(g.Text(`gomponents + HTMX`)), | ||
P(g.Textf(`Time at last full page refresh was %v.`, now.Format(timeFormat))), | ||
partial(now), | ||
FormEl(Method("post"), Action("/"), hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"), | ||
Button(Type("submit"), g.Text(`Update time`), | ||
Class("rounded-md border border-transparent bg-orange-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-orange-700 focus:outline-none focus:ring-2 focus:ring-orange-500 focus:ring-offset-2"), | ||
), | ||
), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func partial(now time.Time) g.Node { | ||
return P(ID("partial"), g.Textf(`Time was last updated at %v.`, now.Format(timeFormat))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters