Skip to content

Commit

Permalink
feat: Web UI のノードやボタンの遷移先を調整できるようにする (#38)
Browse files Browse the repository at this point in the history
* feat: ノードやボタンの遷移先の base path を設定できるようにする

* docs: update usage
  • Loading branch information
harsssh authored Dec 10, 2024
1 parent 1c630e3 commit 14ce37f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ go install github.com/mazrean/isucrud@latest
Usage of isucrud:
-web
run as web server
-addr
-addr string
web server address (default ":7070")
-base string
base for serving the web server (default "/")
-dst string
destination file (default "./dbdoc.md")
destination file (default "./dbdoc.md")
-ignore value
ignore function
ignore function
-ignoreInitialize
ignore functions with 'initialize' in the name (default true)
ignore functions with 'initialize' in the name (default true)
-ignoreMain
ignore main function (default true)
ignore main function (default true)
-ignorePrefix value
ignore function
ignore function
-version
show version
show version
```
## 注意事項
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/asset/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</head>

<body>
{{if .IsFiltered}}<button onclick="location.href = '/'">全ノードを表示する</button>{{end}}
{{if .IsFiltered}}<button onclick="location.href = {{.BasePath}}">全ノードを表示する</button>{{end}}
<div>
nodes:
{{range .NodeTypes}}<span>
Expand Down
11 changes: 8 additions & 3 deletions internal/ui/mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type EdgeType struct {
valid bool
}

type RenderMermaidOption struct {
IsHttp bool
BasePath string
}

var (
nodeTypes = []NodeType{
dbdoc.NodeTypeTable: {"table", "テーブル", tableNodeColor, true},
Expand All @@ -50,7 +55,7 @@ var (
func RenderMermaid(
w io.StringWriter,
nodes []*dbdoc.Node,
isHttp bool,
option RenderMermaidOption,
) error {
_, err := w.WriteString("graph LR\n")
if err != nil {
Expand Down Expand Up @@ -108,9 +113,9 @@ func RenderMermaid(
}
}

if isHttp {
if option.IsHttp {
for _, node := range nodes {
_, err = w.WriteString(fmt.Sprintf(" click %s \"/?node=%s\"\n", node.ID, node.ID))
_, err = w.WriteString(fmt.Sprintf(" click %s \"%s?node=%s\"\n", node.ID, option.BasePath, node.ID))
if err != nil {
return fmt.Errorf("failed to write click event: %w", err)
}
Expand Down
13 changes: 10 additions & 3 deletions internal/ui/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (

type TemplateParam struct {
IsFiltered bool
BasePath string
NodeTypes []NodeType
EdgeTypes []EdgeType
Nodes []*dbdoc.Node
Expand All @@ -39,7 +40,9 @@ func RenderMarkdown(dest string, nodes []*dbdoc.Node) error {
err = RenderMermaid(
sb,
nodes,
false,
RenderMermaidOption{
IsHttp: false,
},
)
if err != nil {
return fmt.Errorf("failed to write mermaid: %w", err)
Expand All @@ -64,7 +67,7 @@ func RenderMarkdown(dest string, nodes []*dbdoc.Node) error {
return nil
}

func RenderHTML(w io.Writer, nodes []*dbdoc.Node, targetNodeID string) error {
func RenderHTML(w io.Writer, nodes []*dbdoc.Node, targetNodeID string, basePath string) error {
filtered := false
filteredNodes := nodes
if targetNodeID != "" {
Expand All @@ -76,7 +79,10 @@ func RenderHTML(w io.Writer, nodes []*dbdoc.Node, targetNodeID string) error {
err := RenderMermaid(
sb,
filteredNodes,
true,
RenderMermaidOption{
IsHttp: true,
BasePath: basePath,
},
)
if err != nil {
return fmt.Errorf("failed to write mermaid: %w", err)
Expand All @@ -89,6 +95,7 @@ func RenderHTML(w io.Writer, nodes []*dbdoc.Node, targetNodeID string) error {

err = tmpl.Execute(w, TemplateParam{
IsFiltered: filtered,
BasePath: basePath,
NodeTypes: nodeTypes[1:],
EdgeTypes: edgeTypes[1:],
Nodes: nodes,
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
ignoreMain, ignoreInitialize bool
web bool
addr string
base string
)

func init() {
Expand All @@ -34,6 +35,7 @@ func init() {
flag.BoolVar(&ignoreInitialize, "ignoreInitialize", true, "ignore functions with 'initialize' in the name")
flag.BoolVar(&web, "web", false, "run as web server")
flag.StringVar(&addr, "addr", "localhost:7070", "address to listen on")
flag.StringVar(&base, "base", "/", "base for serving the web server")
}

func main() {
Expand Down Expand Up @@ -69,7 +71,7 @@ func main() {

targetNodeID := r.URL.Query().Get("node")

err := ui.RenderHTML(w, nodes, targetNodeID)
err := ui.RenderHTML(w, nodes, targetNodeID, base)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down

0 comments on commit 14ce37f

Please sign in to comment.