Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: wip: minimal UI for clair #1832

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ui/assets/v1/footer
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<footer>Clair {{ Version}}</footer>
</body>
</html>
7 changes: 7 additions & 0 deletions ui/assets/v1/header
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{.Title}}</title>
</head>
<body>
7 changes: 7 additions & 0 deletions ui/assets/v1/index.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{- template "header" . -}}
<form>
<label></label>
<input />
<button></button>
</form>
{{ template "footer" . -}}
99 changes: 99 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Package ui is a barebones HTML UI for clair.
package ui

import (
"embed"
"fmt"
"html/template"
"io"
"io/fs"
"net/http"
"path"
"strings"

"github.com/quay/clair/v4/cmd"
)

type V1 struct {
mux *http.ServeMux
tmpl *template.Template
sys fs.FS
ctx map[string]*PageContext
}

type PageContext struct {
Title string
Aux map[string]interface{}
}

var (
_ http.Handler = (*V1)(nil)

//go:embed assets/v1
v1assets embed.FS
)

func New() (*V1, error) {
var err error
h := V1{
mux: http.NewServeMux(),
}
h.sys, err = fs.Sub(v1assets, "assets/v1")
if err != nil {
return nil, err
}
t := template.New("")
t.Funcs(template.FuncMap(map[string]interface{}{
"Version": func() string {
return cmd.Version
},
}))
h.tmpl, err = t.ParseFS(h.sys, "*.tmpl", "header", "footer")
if err != nil {
return nil, err
}

h.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
p := strings.TrimLeft(r.URL.Path, "/")
try := []string{
p + ".tmpl",
path.Join(p, "index.tmpl"),
}
for _, n := range try {
t := h.tmpl.Lookup(n)
if t == nil {
continue
}
w.Header().Set("content-type", "text/html")
if err := t.Execute(w, h.ctx[n]); err != nil {
srv := r.Context().Value(http.ServerContextKey).(*http.Server)
srv.ErrorLog.Printf("error executing template %q: %v", n, err)
}
return
}
if f, err := h.sys.Open(p); err == nil {
defer f.Close()
io.Copy(w, f)
return
}
w.WriteHeader(http.StatusNotFound)
})

h.ctx = map[string]*PageContext{
"index.tmpl": {
Title: "Clair",
},
}

return &h, nil
}

func (h *V1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
case http.MethodPost:
default:
http.Error(w, fmt.Sprintf("disallowed method: %q", r.Method), http.StatusMethodNotAllowed)
}
h.mux.ServeHTTP(w, r)
}
30 changes: 30 additions & 0 deletions ui/ui_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ui_test

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"

"github.com/quay/clair/v4/ui"
)

func TestIndex(t *testing.T) {
h, err := ui.New()
if err != nil {
t.Fatal(err)
}

req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
rec.Body = new(bytes.Buffer)

h.ServeHTTP(rec, req)
res := rec.Result()

t.Logf("%+#q", rec.Body.String())

if res.StatusCode != http.StatusOK {
t.Fail()
}
}