about summary refs log tree commit diff stats
path: root/internal/examples/app/http
diff options
context:
space:
mode:
Diffstat (limited to 'internal/examples/app/http')
-rw-r--r--internal/examples/app/http/pages.go24
-rw-r--r--internal/examples/app/http/server.go18
2 files changed, 42 insertions, 0 deletions
diff --git a/internal/examples/app/http/pages.go b/internal/examples/app/http/pages.go
new file mode 100644
index 0000000..dd686c0
--- /dev/null
+++ b/internal/examples/app/http/pages.go
@@ -0,0 +1,24 @@
+package http
+
+import (
+	"net/http"
+
+	g "github.com/maragudk/gomponents"
+	ghttp "github.com/maragudk/gomponents/http"
+
+	"app/html"
+)
+
+func Home(mux *http.ServeMux) {
+	mux.Handle("GET /", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
+		// Let's pretend this comes from a db or something.
+		items := []string{"Super", "Duper", "Nice"}
+		return html.HomePage(items), nil
+	}))
+}
+
+func About(mux *http.ServeMux) {
+	mux.Handle("GET /about", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
+		return html.AboutPage(), nil
+	}))
+}
diff --git a/internal/examples/app/http/server.go b/internal/examples/app/http/server.go
new file mode 100644
index 0000000..8264fcf
--- /dev/null
+++ b/internal/examples/app/http/server.go
@@ -0,0 +1,18 @@
+package http
+
+import (
+	"net/http"
+)
+
+func Start() error {
+	return http.ListenAndServe(":8080", setupRoutes())
+}
+
+func setupRoutes() http.Handler {
+	mux := http.NewServeMux()
+
+	Home(mux)
+	About(mux)
+
+	return mux
+}