diff --git a/cli/htmgo/go.mod b/cli/htmgo/go.mod index 7d67f4e..ab25b62 100644 --- a/cli/htmgo/go.mod +++ b/cli/htmgo/go.mod @@ -5,8 +5,8 @@ go 1.23.0 require ( github.com/fsnotify/fsnotify v1.7.0 github.com/google/uuid v1.6.0 - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 - github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499 + github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca + github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241116145200-825c4dd7ecca github.com/stretchr/testify v1.9.0 golang.org/x/mod v0.21.0 golang.org/x/sys v0.26.0 diff --git a/cli/htmgo/go.sum b/cli/htmgo/go.sum index 5942c25..954a850 100644 --- a/cli/htmgo/go.sum +++ b/cli/htmgo/go.sum @@ -8,10 +8,10 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= -github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499 h1:uny6xIIU2zNdP03qhQsdfJDOAQR+EBDtlY0DxzIFeCw= -github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499/go.mod h1:FraJsj3NRuLBQDk83ZVa+psbNRNLe+rajVtVhYMEme4= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241116145200-825c4dd7ecca h1:yAs6jEsQjQ/Dg47DA0clJ3rgzxvjCS+lQk5NovRx0WQ= +github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241116145200-825c4dd7ecca/go.mod h1:FraJsj3NRuLBQDk83ZVa+psbNRNLe+rajVtVhYMEme4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= diff --git a/cli/htmgo/tasks/astgen/entry.go b/cli/htmgo/tasks/astgen/entry.go index 359f39e..8e7e458 100644 --- a/cli/htmgo/tasks/astgen/entry.go +++ b/cli/htmgo/tasks/astgen/entry.go @@ -2,13 +2,9 @@ package astgen import ( "fmt" - "github.com/maddalax/htmgo/cli/htmgo/internal/dirutil" - "github.com/maddalax/htmgo/cli/htmgo/tasks/process" - "github.com/maddalax/htmgo/framework/h" "go/ast" "go/parser" "go/token" - "golang.org/x/mod/modfile" "io/fs" "log/slog" "os" @@ -16,6 +12,11 @@ import ( "slices" "strings" "unicode" + + "github.com/maddalax/htmgo/cli/htmgo/internal/dirutil" + "github.com/maddalax/htmgo/cli/htmgo/tasks/process" + "github.com/maddalax/htmgo/framework/h" + "golang.org/x/mod/modfile" ) type Page struct { @@ -483,9 +484,36 @@ func writeAssetsFile() { } +func HasModuleFile(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +} + +func CheckPagesDirectory(path string) error { + pagesPath := filepath.Join(path, "pages") + _, err := os.Stat(pagesPath) + if err != nil { + return fmt.Errorf("The directory pages does not exist.") + } + + return nil +} + func GetModuleName() string { wd := process.GetWorkingDir() modPath := filepath.Join(wd, "go.mod") + + if HasModuleFile(modPath) == false { + fmt.Fprintf(os.Stderr, "Module not found: go.mod file does not exist.") + return "" + } + + checkDir := CheckPagesDirectory(wd) + if checkDir != nil { + fmt.Fprintf(os.Stderr, checkDir.Error()) + return "" + } + goModBytes, err := os.ReadFile(modPath) if err != nil { fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err) diff --git a/cli/htmgo/tasks/astgen/project-sample/go.mod b/cli/htmgo/tasks/astgen/project-sample/go.mod index 981c812..e0f3622 100644 --- a/cli/htmgo/tasks/astgen/project-sample/go.mod +++ b/cli/htmgo/tasks/astgen/project-sample/go.mod @@ -2,12 +2,10 @@ module astgen-project-sample go 1.23.0 -require ( - github.com/go-chi/chi/v5 v5.1.0 - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 -) +require github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca require ( + github.com/go-chi/chi/v5 v5.1.0 // indirect github.com/google/uuid v1.6.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/cli/htmgo/tasks/astgen/project-sample/go.sum b/cli/htmgo/tasks/astgen/project-sample/go.sum index bbed914..2d58c3e 100644 --- a/cli/htmgo/tasks/astgen/project-sample/go.sum +++ b/cli/htmgo/tasks/astgen/project-sample/go.sum @@ -4,8 +4,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= diff --git a/examples/chat/go.mod b/examples/chat/go.mod index 71fc64d..be4236b 100644 --- a/examples/chat/go.mod +++ b/examples/chat/go.mod @@ -5,7 +5,7 @@ go 1.23.0 require ( github.com/go-chi/chi/v5 v5.1.0 github.com/google/uuid v1.6.0 - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 + github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca github.com/mattn/go-sqlite3 v1.14.23 github.com/puzpuzpuz/xsync/v3 v3.4.0 ) diff --git a/examples/chat/go.sum b/examples/chat/go.sum index 4670ba1..32efb60 100644 --- a/examples/chat/go.sum +++ b/examples/chat/go.sum @@ -4,8 +4,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/examples/hackernews/go.mod b/examples/hackernews/go.mod index f2a754a..1aca14c 100644 --- a/examples/hackernews/go.mod +++ b/examples/hackernews/go.mod @@ -3,7 +3,7 @@ module hackernews go 1.23.0 require ( - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 + github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca github.com/microcosm-cc/bluemonday v1.0.27 ) diff --git a/examples/hackernews/go.sum b/examples/hackernews/go.sum index a3db1b9..6c4c88d 100644 --- a/examples/hackernews/go.sum +++ b/examples/hackernews/go.sum @@ -8,8 +8,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8= github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk= github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/examples/simple-auth/go.mod b/examples/simple-auth/go.mod index b48a17f..d5fb4d9 100644 --- a/examples/simple-auth/go.mod +++ b/examples/simple-auth/go.mod @@ -3,7 +3,7 @@ module simpleauth go 1.23.0 require ( - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 + github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca github.com/mattn/go-sqlite3 v1.14.24 golang.org/x/crypto v0.28.0 ) diff --git a/examples/simple-auth/go.sum b/examples/simple-auth/go.sum index 37608bd..63c79e5 100644 --- a/examples/simple-auth/go.sum +++ b/examples/simple-auth/go.sum @@ -4,8 +4,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/examples/todo-list/go.mod b/examples/todo-list/go.mod index 55a6def..38b2080 100644 --- a/examples/todo-list/go.mod +++ b/examples/todo-list/go.mod @@ -5,7 +5,7 @@ go 1.23.0 require ( entgo.io/ent v0.14.1 github.com/google/uuid v1.6.0 - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 + github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca github.com/mattn/go-sqlite3 v1.14.23 ) diff --git a/examples/todo-list/go.sum b/examples/todo-list/go.sum index beaf601..09f077a 100644 --- a/examples/todo-list/go.sum +++ b/examples/todo-list/go.sum @@ -33,8 +33,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM= diff --git a/examples/ws-example/go.mod b/examples/ws-example/go.mod index d9323ee..91af23a 100644 --- a/examples/ws-example/go.mod +++ b/examples/ws-example/go.mod @@ -4,7 +4,7 @@ go 1.23.0 require ( github.com/maddalax/htmgo/extensions/websocket v0.0.0-20241109180553-34e816ff7c8a - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 + github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca ) require ( diff --git a/examples/ws-example/go.sum b/examples/ws-example/go.sum index 0eff937..ac51c0e 100644 --- a/examples/ws-example/go.sum +++ b/examples/ws-example/go.sum @@ -12,8 +12,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/maddalax/htmgo/extensions/websocket v0.0.0-20241109180553-34e816ff7c8a h1:BYVo9NCLHgXvf5pCGUnVg8UE7d9mWOyLgWXYTgVTkyA= github.com/maddalax/htmgo/extensions/websocket v0.0.0-20241109180553-34e816ff7c8a/go.mod h1:r6/VqntLp7VlAUpIXy3MWZMHs2EkPKJP5rJdDL8lFP4= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4= diff --git a/extensions/websocket/go.mod b/extensions/websocket/go.mod index 079fc63..ac40d1f 100644 --- a/extensions/websocket/go.mod +++ b/extensions/websocket/go.mod @@ -4,7 +4,7 @@ go 1.23.0 require ( github.com/gobwas/ws v1.4.0 - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 + github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca github.com/puzpuzpuz/xsync/v3 v3.4.0 github.com/stretchr/testify v1.9.0 ) diff --git a/extensions/websocket/go.sum b/extensions/websocket/go.sum index 33991dc..3b99e07 100644 --- a/extensions/websocket/go.sum +++ b/extensions/websocket/go.sum @@ -10,8 +10,8 @@ github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs= github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4= diff --git a/extensions/websocket/init.go b/extensions/websocket/init.go index 980691e..9261063 100644 --- a/extensions/websocket/init.go +++ b/extensions/websocket/init.go @@ -21,8 +21,8 @@ func EnableExtension(app *h.App, opts opts.ExtensionOpts) { panic("websocket: SessionId func is required") } - service.Set[ws.SocketManager](app.Opts.ServiceLocator, service.Singleton, func() *ws.SocketManager { - manager := ws.NewSocketManager(&opts) + service.Set[wsutil.SocketManager](app.Opts.ServiceLocator, service.Singleton, func() *wsutil.SocketManager { + manager := wsutil.NewSocketManager(&opts) manager.StartMetrics() return manager }) diff --git a/extensions/websocket/internal/wsutil/handler.go b/extensions/websocket/internal/wsutil/handler.go index 0b320b9..1096dd5 100644 --- a/extensions/websocket/internal/wsutil/handler.go +++ b/extensions/websocket/internal/wsutil/handler.go @@ -6,7 +6,6 @@ import ( "github.com/gobwas/ws" "github.com/gobwas/ws/wsutil" ws2 "github.com/maddalax/htmgo/extensions/websocket/opts" - ws3 "github.com/maddalax/htmgo/extensions/websocket/ws" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" "log/slog" @@ -26,7 +25,7 @@ func WsHttpHandler(opts *ws2.ExtensionOpts) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { cc := r.Context().Value(h.RequestContextKey).(*h.RequestContext) locator := cc.ServiceLocator() - manager := service.Get[ws3.SocketManager](locator) + manager := service.Get[SocketManager](locator) sessionId := opts.SessionId(cc) @@ -47,7 +46,7 @@ func WsHttpHandler(opts *ws2.ExtensionOpts) http.HandlerFunc { we don't want to block the writer */ done := make(chan bool, 1000) - writer := make(ws3.WriterChan, 1000) + writer := make(WriterChan, 1000) wg := sync.WaitGroup{} diff --git a/extensions/websocket/ws/manager.go b/extensions/websocket/internal/wsutil/manager.go similarity index 99% rename from extensions/websocket/ws/manager.go rename to extensions/websocket/internal/wsutil/manager.go index b250f7b..34988cd 100644 --- a/extensions/websocket/ws/manager.go +++ b/extensions/websocket/internal/wsutil/manager.go @@ -1,4 +1,4 @@ -package ws +package wsutil import ( "fmt" diff --git a/extensions/websocket/ws/manager_test.go b/extensions/websocket/internal/wsutil/manager_test.go similarity index 99% rename from extensions/websocket/ws/manager_test.go rename to extensions/websocket/internal/wsutil/manager_test.go index 3c05a6f..0ba95d4 100644 --- a/extensions/websocket/ws/manager_test.go +++ b/extensions/websocket/internal/wsutil/manager_test.go @@ -1,4 +1,4 @@ -package ws +package wsutil import ( ws2 "github.com/maddalax/htmgo/extensions/websocket/opts" diff --git a/extensions/websocket/ws/access.go b/extensions/websocket/ws/access.go index 012d859..efa63c5 100644 --- a/extensions/websocket/ws/access.go +++ b/extensions/websocket/ws/access.go @@ -1,9 +1,10 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/framework/h" ) -func ManagerFromCtx(ctx *h.RequestContext) *SocketManager { - return SocketManagerFromCtx(ctx) +func ManagerFromCtx(ctx *h.RequestContext) *wsutil.SocketManager { + return wsutil.SocketManagerFromCtx(ctx) } diff --git a/extensions/websocket/ws/dispatch.go b/extensions/websocket/ws/dispatch.go index 53ed0f3..248b649 100644 --- a/extensions/websocket/ws/dispatch.go +++ b/extensions/websocket/ws/dispatch.go @@ -1,6 +1,7 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" @@ -32,7 +33,7 @@ func PushElement(data HandlerData, el *h.Element) bool { // PushElementCtx sends an element to the current session and swaps it into the page func PushElementCtx(ctx *h.RequestContext, el *h.Element) bool { locator := ctx.ServiceLocator() - socketManager := service.Get[SocketManager](locator) + socketManager := service.Get[wsutil.SocketManager](locator) socketId := session.GetSessionId(ctx) socket := socketManager.Get(string(socketId)) if socket == nil { diff --git a/extensions/websocket/ws/every.go b/extensions/websocket/ws/every.go index d549c53..679d3d5 100644 --- a/extensions/websocket/ws/every.go +++ b/extensions/websocket/ws/every.go @@ -1,6 +1,7 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" @@ -11,7 +12,7 @@ import ( func Every(ctx *h.RequestContext, interval time.Duration, cb func() bool) { socketId := session.GetSessionId(ctx) locator := ctx.ServiceLocator() - manager := service.Get[SocketManager](locator) + manager := service.Get[wsutil.SocketManager](locator) manager.RunIntervalWithSocket(string(socketId), interval, cb) } diff --git a/extensions/websocket/ws/handler.go b/extensions/websocket/ws/handler.go index 07c3080..2d0bbb7 100644 --- a/extensions/websocket/ws/handler.go +++ b/extensions/websocket/ws/handler.go @@ -2,15 +2,16 @@ package ws import ( "fmt" + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "sync" ) type MessageHandler struct { - manager *SocketManager + manager *wsutil.SocketManager } -func NewMessageHandler(manager *SocketManager) *MessageHandler { +func NewMessageHandler(manager *wsutil.SocketManager) *MessageHandler { return &MessageHandler{manager: manager} } @@ -76,7 +77,7 @@ func (h *MessageHandler) OnDomElementRemoved(handlerId string) { handlers.Delete(handlerId) } -func (h *MessageHandler) OnSocketDisconnected(event SocketEvent) { +func (h *MessageHandler) OnSocketDisconnected(event wsutil.SocketEvent) { sessionId := session.Id(event.SessionId) hashes, ok := sessionIdToHashes.Load(sessionId) if ok { diff --git a/extensions/websocket/ws/listener.go b/extensions/websocket/ws/listener.go index 9b4965b..5556331 100644 --- a/extensions/websocket/ws/listener.go +++ b/extensions/websocket/ws/listener.go @@ -1,12 +1,13 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/framework/service" ) func StartListener(locator *service.Locator) { - manager := service.Get[SocketManager](locator) + manager := service.Get[wsutil.SocketManager](locator) manager.Listen(socketMessageListener) handler := NewMessageHandler(manager) go func() { @@ -22,9 +23,9 @@ func handle(handler *MessageHandler) { handler.OnServerSideEvent(event) case event := <-socketMessageListener: switch event.Type { - case DisconnectedEvent: + case wsutil.DisconnectedEvent: handler.OnSocketDisconnected(event) - case MessageEvent: + case wsutil.MessageEvent: handlerId, ok := event.Payload["id"].(string) eventName, ok2 := event.Payload["event"].(string) diff --git a/extensions/websocket/ws/metrics.go b/extensions/websocket/ws/metrics.go index 29bbe79..258493b 100644 --- a/extensions/websocket/ws/metrics.go +++ b/extensions/websocket/ws/metrics.go @@ -1,11 +1,12 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/framework/h" ) type Metrics struct { - Manager ManagerMetrics + Manager wsutil.ManagerMetrics Handler HandlerMetrics } diff --git a/extensions/websocket/ws/register.go b/extensions/websocket/ws/register.go index d24d0d1..b7350fd 100644 --- a/extensions/websocket/ws/register.go +++ b/extensions/websocket/ws/register.go @@ -1,6 +1,7 @@ package ws import ( + "github.com/maddalax/htmgo/extensions/websocket/internal/wsutil" "github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/framework/h" "github.com/puzpuzpuz/xsync/v3" @@ -10,8 +11,8 @@ import ( type HandlerData struct { SessionId session.Id - Socket *SocketConnection - Manager *SocketManager + Socket *wsutil.SocketConnection + Manager *wsutil.SocketManager } type Handler func(data HandlerData) @@ -28,7 +29,7 @@ var sessionIdToHashes = xsync.NewMapOf[session.Id, map[KeyHash]bool]() var hashesToSessionId = xsync.NewMapOf[KeyHash, session.Id]() var serverEventNamesToHash = xsync.NewMapOf[string, map[KeyHash]bool]() -var socketMessageListener = make(chan SocketEvent, 100) +var socketMessageListener = make(chan wsutil.SocketEvent, 100) var serverSideMessageListener = make(chan ServerSideEvent, 100) var lock = sync.Mutex{} var callingHandler = atomic.Bool{} diff --git a/framework-ui/go.mod b/framework-ui/go.mod index 48776cf..a25192c 100644 --- a/framework-ui/go.mod +++ b/framework-ui/go.mod @@ -2,7 +2,7 @@ module github.com/maddalax/htmgo/framework-ui go 1.23.0 -require github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 +require github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca require ( github.com/go-chi/chi/v5 v5.1.0 // indirect diff --git a/framework-ui/go.sum b/framework-ui/go.sum index b93d222..1468ba2 100644 --- a/framework-ui/go.sum +++ b/framework-ui/go.sum @@ -4,8 +4,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= diff --git a/framework/h/command_test.go b/framework/h/command_test.go index 2442506..ff331eb 100644 --- a/framework/h/command_test.go +++ b/framework/h/command_test.go @@ -7,6 +7,7 @@ import ( "regexp" "strings" "testing" + "time" ) func findScriptById(n *html.Node, id string) *html.Node { @@ -87,7 +88,7 @@ func TestJsEval(t *testing.T) { } func TestSetText(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, SetText("Hello World")), "this.innerText = 'Hello World';") + compareIgnoreSpaces(t, renderJs(t, SetText("Hello World")), "(self||this).innerText = 'Hello World';") } func TestSetTextOnChildren(t *testing.T) { @@ -100,42 +101,42 @@ func TestSetTextOnChildren(t *testing.T) { } func TestIncrement(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, Increment(5)), "this.innerText = parseInt(this.innerText) + 5;") + compareIgnoreSpaces(t, renderJs(t, Increment(5)), "(self||this).innerText = parseInt((self||this).innerText) + 5;") } func TestSetInnerHtml(t *testing.T) { htmlContent := Div(Span(UnsafeRaw("inner content"))) - compareIgnoreSpaces(t, renderJs(t, SetInnerHtml(htmlContent)), "this.innerHTML = `
inner content
`;") + compareIgnoreSpaces(t, renderJs(t, SetInnerHtml(htmlContent)), "(self||this).innerHTML = `
inner content
`;") } func TestSetOuterHtml(t *testing.T) { htmlContent := Div(Span(UnsafeRaw("outer content"))) - compareIgnoreSpaces(t, renderJs(t, SetOuterHtml(htmlContent)), "this.outerHTML = `
outer content
`;") + compareIgnoreSpaces(t, renderJs(t, SetOuterHtml(htmlContent)), "(self||this).outerHTML = `
outer content
`;") } func TestAddAttribute(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, AddAttribute("data-id", "123")), "this.setAttribute('data-id', '123');") + compareIgnoreSpaces(t, renderJs(t, AddAttribute("data-id", "123")), "(self||this).setAttribute('data-id', '123');") } func TestSetDisabled(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, SetDisabled(true)), "this.setAttribute('disabled', 'true');") - compareIgnoreSpaces(t, renderJs(t, SetDisabled(false)), "this.removeAttribute('disabled');") + compareIgnoreSpaces(t, renderJs(t, SetDisabled(true)), "(self||this).setAttribute('disabled', 'true');") + compareIgnoreSpaces(t, renderJs(t, SetDisabled(false)), "(self||this).removeAttribute('disabled');") } func TestRemoveAttribute(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, RemoveAttribute("data-id")), "this.removeAttribute('data-id');") + compareIgnoreSpaces(t, renderJs(t, RemoveAttribute("data-id")), "(self||this).removeAttribute('data-id');") } func TestAddClass(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, AddClass("active")), "this.classList.add('active');") + compareIgnoreSpaces(t, renderJs(t, AddClass("active")), "(self||this).classList.add('active');") } func TestRemoveClass(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, RemoveClass("active")), "this.classList.remove('active');") + compareIgnoreSpaces(t, renderJs(t, RemoveClass("active")), "(self||this).classList.remove('active');") } func TestToggleClass(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, ToggleClass("hidden")), "this.classList.toggle('hidden');") + compareIgnoreSpaces(t, renderJs(t, ToggleClass("hidden")), "(self||this).classList.toggle('hidden');") } func TestToggleClassOnElement(t *testing.T) { @@ -206,7 +207,7 @@ func TestAlert(t *testing.T) { } func TestRemove(t *testing.T) { - compareIgnoreSpaces(t, renderJs(t, Remove()), "this.remove();") + compareIgnoreSpaces(t, renderJs(t, Remove()), "(self||this).remove();") } func TestSubmitFormOnEnter(t *testing.T) { @@ -394,5 +395,23 @@ func TestConsoleLog(t *testing.T) { func TestSetValue(t *testing.T) { t.Parallel() - compareIgnoreSpaces(t, renderJs(t, SetValue("New Value")), "this.value = 'New Value';") + compareIgnoreSpaces(t, renderJs(t, SetValue("New Value")), "(self||this).value = 'New Value';") +} + +func TestRunAfterTimeout(t *testing.T) { + t.Parallel() + compareIgnoreSpaces(t, renderJs(t, RunAfterTimeout(time.Second*5, SetText("Hello"))), ` + setTimeout(function() { + (self||this).innerText = 'Hello' + }, 5000) + `) +} + +func TestRunOnInterval(t *testing.T) { + t.Parallel() + compareIgnoreSpaces(t, renderJs(t, RunOnInterval(time.Second, SetText("Hello"))), ` + setInterval(function() { + (self||this).innerText = 'Hello' + }, 1000) + `) } diff --git a/framework/h/lifecycle.go b/framework/h/lifecycle.go index 1aed70b..393c228 100644 --- a/framework/h/lifecycle.go +++ b/framework/h/lifecycle.go @@ -6,6 +6,7 @@ import ( "github.com/maddalax/htmgo/framework/hx" "github.com/maddalax/htmgo/framework/internal/util" "strings" + "time" ) type LifeCycle struct { @@ -163,7 +164,7 @@ func NewComplexJsCommand(command string) ComplexJsCommand { // SetText sets the inner text of the element. func SetText(text string) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.innerText = '%s'", text)} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).innerText = '%s'", text)} } // SetTextOnChildren sets the inner text of all the children of the element that match the selector. @@ -180,25 +181,25 @@ func SetTextOnChildren(selector, text string) ComplexJsCommand { // Increment increments the inner text of the element by the given amount. func Increment(amount int) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.innerText = parseInt(this.innerText) + %d", amount)} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).innerText = parseInt((self || this).innerText) + %d", amount)} } // SetInnerHtml sets the inner HTML of the element. func SetInnerHtml(r Ren) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.innerHTML = `%s`", Render(r))} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).innerHTML = `%s`", Render(r))} } // SetOuterHtml sets the outer HTML of the element. func SetOuterHtml(r Ren) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.outerHTML = `%s`", Render(r))} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).outerHTML = `%s`", Render(r))} } // AddAttribute adds the given attribute to the element. func AddAttribute(name, value string) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.setAttribute('%s', '%s')", name, value)} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).setAttribute('%s', '%s')", name, value)} } // SetDisabled sets the disabled attribute on the element. @@ -213,25 +214,25 @@ func SetDisabled(disabled bool) SimpleJsCommand { // RemoveAttribute removes the given attribute from the element. func RemoveAttribute(name string) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.removeAttribute('%s')", name)} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).removeAttribute('%s')", name)} } // AddClass adds the given class to the element. func AddClass(class string) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.classList.add('%s')", class)} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).classList.add('%s')", class)} } // RemoveClass removes the given class from the element. func RemoveClass(class string) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.classList.remove('%s')", class)} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).classList.remove('%s')", class)} } // ToggleClass toggles the given class on the element. func ToggleClass(class string) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.classList.toggle('%s')", class)} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).classList.toggle('%s')", class)} } // ToggleText toggles the given text on the element. @@ -391,7 +392,7 @@ func Alert(text string) SimpleJsCommand { // Remove removes the element from the DOM. func Remove() SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: "this.remove()"} + return SimpleJsCommand{Command: "(self || this).remove()"} } // EvalJs evaluates the given JavaScript code. @@ -399,15 +400,21 @@ func EvalJs(js string) ComplexJsCommand { return NewComplexJsCommand(js) } -func EvalCommandsOnSelector(selector string, cmds ...Command) ComplexJsCommand { +func CombineCommands(cmds ...Command) string { lines := make([]string, len(cmds)) for i, cmd := range cmds { lines[i] = Render(cmd) + lines[i] = strings.ReplaceAll(lines[i], "(self || this).", "self.") lines[i] = strings.ReplaceAll(lines[i], "this.", "self.") // some commands set the element we need to fix it so we arent redeclaring it lines[i] = strings.ReplaceAll(lines[i], "let element =", "element =") } code := strings.Join(lines, "\n") + return code +} + +func EvalCommandsOnSelector(selector string, cmds ...Command) ComplexJsCommand { + code := CombineCommands(cmds...) return EvalJs(fmt.Sprintf(` let element = document.querySelector("%s"); @@ -444,7 +451,7 @@ func ConsoleLog(text string) SimpleJsCommand { // SetValue sets the value of the element. func SetValue(value string) SimpleJsCommand { // language=JavaScript - return SimpleJsCommand{Command: fmt.Sprintf("this.value = '%s'", value)} + return SimpleJsCommand{Command: fmt.Sprintf("(self || this).value = '%s'", value)} } // SubmitFormOnEnter submits the form when the user presses the enter key. @@ -478,3 +485,31 @@ func InjectScriptIfNotExist(src string) ComplexJsCommand { } `, src, src)) } + +func RunOnInterval(time time.Duration, cmds ...Command) ComplexJsCommand { + code := strings.Builder{} + + for _, cmd := range cmds { + code.WriteString(fmt.Sprintf(` + setInterval(function() { + %s + }, %d) + `, Render(cmd), time.Milliseconds())) + } + + return EvalJs(code.String()) +} + +func RunAfterTimeout(time time.Duration, cmds ...Command) ComplexJsCommand { + code := strings.Builder{} + + for _, cmd := range cmds { + code.WriteString(fmt.Sprintf(` + setTimeout(function() { + %s + }, %d) + `, Render(cmd), time.Milliseconds())) + } + + return EvalJs(code.String()) +} diff --git a/framework/h/render_test.go b/framework/h/render_test.go index 83a4cf4..4ddfa03 100644 --- a/framework/h/render_test.go +++ b/framework/h/render_test.go @@ -48,7 +48,7 @@ func TestRender(t *testing.T) { div.attributes.Set("data-attr-1", "value") - expected := `
hello, world
hello, child
` + expected := `
hello, world
hello, child
` result := Render(div) assert.Equal(t, diff --git a/framework/js/commands.go b/framework/js/commands.go index b1703e3..18a6ce2 100644 --- a/framework/js/commands.go +++ b/framework/js/commands.go @@ -47,3 +47,5 @@ var ToggleText = h.ToggleText var ToggleTextOnSibling = h.ToggleTextOnSibling var ToggleTextOnChildren = h.ToggleTextOnChildren var ToggleTextOnParent = h.ToggleTextOnParent +var RunAfterTimeout = h.RunAfterTimeout +var RunOnInterval = h.RunOnInterval diff --git a/htmgo-site/go.mod b/htmgo-site/go.mod index c0afcdc..fd13b2b 100644 --- a/htmgo-site/go.mod +++ b/htmgo-site/go.mod @@ -6,8 +6,8 @@ require ( github.com/alecthomas/chroma/v2 v2.14.0 github.com/go-chi/chi/v5 v5.1.0 github.com/google/uuid v1.6.0 - github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 - github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499 + github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca + github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241116145200-825c4dd7ecca github.com/yuin/goldmark v1.7.4 github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc ) diff --git a/htmgo-site/go.sum b/htmgo-site/go.sum index 20652ab..1cd7775 100644 --- a/htmgo-site/go.sum +++ b/htmgo-site/go.sum @@ -19,10 +19,10 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= -github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499 h1:uny6xIIU2zNdP03qhQsdfJDOAQR+EBDtlY0DxzIFeCw= -github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499/go.mod h1:FraJsj3NRuLBQDk83ZVa+psbNRNLe+rajVtVhYMEme4= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241116145200-825c4dd7ecca h1:yAs6jEsQjQ/Dg47DA0clJ3rgzxvjCS+lQk5NovRx0WQ= +github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241116145200-825c4dd7ecca/go.mod h1:FraJsj3NRuLBQDk83ZVa+psbNRNLe+rajVtVhYMEme4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/htmgo-site/internal/urlhelper/ip.go b/htmgo-site/internal/urlhelper/ip.go new file mode 100644 index 0000000..2d33209 --- /dev/null +++ b/htmgo-site/internal/urlhelper/ip.go @@ -0,0 +1,27 @@ +package urlhelper + +import ( + "net/http" + "strings" +) + +func GetClientIp(r *http.Request) string { + // Try to get the real client IP from the 'CF-Connecting-IP' header + if ip := r.Header.Get("CF-Connecting-IP"); ip != "" { + return ip + } + + // If not available, fall back to 'X-Forwarded-For' + if ip := r.Header.Get("X-Forwarded-For"); ip != "" { + return ip + } + + // Otherwise, use the default remote address (this will be Cloudflare's IP) + remote := r.RemoteAddr + + if strings.HasPrefix(remote, "[::1]") { + return "localhost" + } + + return remote +} diff --git a/htmgo-site/main.go b/htmgo-site/main.go index 5c6a484..35914f3 100644 --- a/htmgo-site/main.go +++ b/htmgo-site/main.go @@ -1,14 +1,15 @@ package main import ( - "fmt" "github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/service" "htmgo-site/__htmgo" "htmgo-site/internal/cache" "htmgo-site/internal/markdown" "htmgo-site/internal/sitemap" + "htmgo-site/internal/urlhelper" "io/fs" + "log" "net/http" ) @@ -20,13 +21,17 @@ func main() { service.Set(locator, service.Singleton, markdown.NewRenderer) service.Set(locator, service.Singleton, cache.NewSimpleCache) - fmt.Printf("starting up server2\n") - h.Start(h.AppOpts{ ServiceLocator: locator, LiveReload: true, Register: func(app *h.App) { + app.Use(func(ctx *h.RequestContext) { + r := ctx.Request + // Log the details of the incoming request + log.Printf("Method: %s, URL: %s, RemoteAddr: %s", r.Method, r.URL.String(), urlhelper.GetClientIp(r)) + }) + app.UseWithContext(func(w http.ResponseWriter, r *http.Request, context map[string]any) { context["embeddedMarkdown"] = markdownAssets }) diff --git a/templates/starter/go.mod b/templates/starter/go.mod index 5659eb2..26a7bbe 100644 --- a/templates/starter/go.mod +++ b/templates/starter/go.mod @@ -2,7 +2,7 @@ module starter-template go 1.23.0 -require github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 +require github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca require ( github.com/go-chi/chi/v5 v5.1.0 // indirect diff --git a/templates/starter/go.sum b/templates/starter/go.sum index bbed914..2d58c3e 100644 --- a/templates/starter/go.sum +++ b/templates/starter/go.sum @@ -4,8 +4,8 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499 h1:hQF++Rt9lJHUFk8PG097MTbpl2NnEcbot0iy8ZVPSOI= -github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw= +github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=