Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dadav committed Jul 12, 2024
1 parent aa60220 commit 0663b95
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 146 deletions.
8 changes: 5 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/dadav/gorge/internal/utils"
v3 "github.com/dadav/gorge/internal/v3/api"
backend "github.com/dadav/gorge/internal/v3/backend"
"github.com/dadav/gorge/internal/v3/ui/handlers"
"github.com/dadav/gorge/internal/v3/ui"
openapi "github.com/dadav/gorge/pkg/gen/v3/openapi"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
Expand Down Expand Up @@ -150,8 +150,10 @@ You can also enable the caching functionality to speed things up.`,

if config.UI {
r.Group(func(r chi.Router) {
g := handlers.NewGorgeService()
g.AddRoutesToRouter(r)
r.HandleFunc("/", ui.IndexHandler)
r.HandleFunc("/search", ui.SearchHandler)
r.HandleFunc("/modules/{module}", ui.ModuleHandler)
r.Handle("/assets/*", ui.HandleAssets())
})
}

Expand Down
7 changes: 7 additions & 0 deletions internal/v3/ui/components/module.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package components

import gen "github.com/dadav/gorge/pkg/gen/v3/openapi"

templ ModuleView(module *gen.Module) {
<h3>{ module.Name }</h3>
}
50 changes: 50 additions & 0 deletions internal/v3/ui/components/module_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/v3/ui/components/page.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package components

templ Page(title string, content templ.Component) {
<!DOCTYPE html>
<html>
@Header(title)
<body>
<div class="main">
@content
</div>
</body>
</html>
}
51 changes: 51 additions & 0 deletions internal/v3/ui/components/page_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions internal/v3/ui/components/search.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package components

import (
"fmt"
gen "github.com/dadav/gorge/pkg/gen/v3/openapi"
)

templ SearchView() {
<div class="search">
<img id="logo" src="/assets/logo.png" width="300"/>
<br/>
<input
class="form-control"
type="search"
name="query"
placeholder="Name, author, version..."
hx-get="/search"
hx-params="*"
hx-trigger="input changed delay:500ms, search, load"
hx-target="#search-results"
/>
<table class="table">
<thead>
<tr>
<th>Module</th>
<th>Author</th>
<th>Version</th>
</tr>
</thead>
<tbody id="search-results"></tbody>
</table>
</div>
}

templ ModuleToTableRow(module *gen.Module) {
<tr>
<td><a href={ templ.URL(fmt.Sprintf("/modules/%s", module.Name)) }>{ module.Name }</a></td>
<td>{ module.Owner.Username }</td>
<td>{ module.CurrentRelease.Version }</td>
</tr>
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions internal/v3/ui/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ui

import (
"net/http"
"strings"

"github.com/a-h/templ"
"github.com/dadav/gorge/internal/log"
"github.com/dadav/gorge/internal/v3/backend"
"github.com/dadav/gorge/internal/v3/ui/components"
"github.com/go-chi/chi/v5"
)

func IndexHandler(w http.ResponseWriter, r *http.Request) {
templ.Handler(components.Page("Gorge", components.SearchView())).ServeHTTP(w, r)
}

func SearchHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query().Get("query")
modules, err := backend.ConfiguredBackend.GetAllModules()
if err != nil {
w.WriteHeader(500)
log.Log.Error(err)
return
}

for _, module := range modules {
if strings.Contains(module.Name, query) || strings.Contains(module.Owner.Username, query) || strings.Contains(module.CurrentRelease.Version, query) {
templ.Handler(components.ModuleToTableRow(module)).ServeHTTP(w, r)
}
}
}

func ModuleHandler(w http.ResponseWriter, r *http.Request) {
moduleName := chi.URLParam(r, "module")
modules, err := backend.ConfiguredBackend.GetAllModules()
if err != nil {
w.WriteHeader(500)
log.Log.Error(err)
return
}

for _, module := range modules {
if module.Name == moduleName {
templ.Handler(components.Page(module.Name, components.ModuleView(module))).ServeHTTP(w, r)
return
}
}

http.NotFound(w, r)
}
Loading

0 comments on commit 0663b95

Please sign in to comment.