Merge remote-tracking branch 'origin/master' into ws-extension-updates

This commit is contained in:
maddalax 2024-11-18 11:28:49 -06:00
commit 519be8771d
40 changed files with 216 additions and 96 deletions

View file

@ -5,8 +5,8 @@ go 1.23.0
require ( require (
github.com/fsnotify/fsnotify v1.7.0 github.com/fsnotify/fsnotify v1.7.0
github.com/google/uuid v1.6.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/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499 github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241116145200-825c4dd7ecca
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.21.0 golang.org/x/mod v0.21.0
golang.org/x/sys v0.26.0 golang.org/x/sys v0.26.0

View file

@ -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/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 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/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-20241116145200-825c4dd7ecca h1:yAs6jEsQjQ/Dg47DA0clJ3rgzxvjCS+lQk5NovRx0WQ=
github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499/go.mod h1:FraJsj3NRuLBQDk83ZVa+psbNRNLe+rajVtVhYMEme4= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

View file

@ -2,13 +2,9 @@ package astgen
import ( import (
"fmt" "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/ast"
"go/parser" "go/parser"
"go/token" "go/token"
"golang.org/x/mod/modfile"
"io/fs" "io/fs"
"log/slog" "log/slog"
"os" "os"
@ -16,6 +12,11 @@ import (
"slices" "slices"
"strings" "strings"
"unicode" "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 { 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 { func GetModuleName() string {
wd := process.GetWorkingDir() wd := process.GetWorkingDir()
modPath := filepath.Join(wd, "go.mod") 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) goModBytes, err := os.ReadFile(modPath)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err) fmt.Fprintf(os.Stderr, "error reading go.mod: %v\n", err)

View file

@ -2,12 +2,10 @@ module astgen-project-sample
go 1.23.0 go 1.23.0
require ( require github.com/maddalax/htmgo/framework v1.0.3-0.20241116145200-825c4dd7ecca
github.com/go-chi/chi/v5 v5.1.0
github.com/maddalax/htmgo/framework v1.0.3-0.20241109183230-b234ead96499
)
require ( require (
github.com/go-chi/chi/v5 v5.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

View file

@ -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/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 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

View file

@ -5,7 +5,7 @@ go 1.23.0
require ( require (
github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/chi/v5 v5.1.0
github.com/google/uuid v1.6.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/mattn/go-sqlite3 v1.14.23
github.com/puzpuzpuz/xsync/v3 v3.4.0 github.com/puzpuzpuz/xsync/v3 v3.4.0
) )

View file

@ -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/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 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= 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/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View file

@ -3,7 +3,7 @@ module hackernews
go 1.23.0 go 1.23.0
require ( 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 github.com/microcosm-cc/bluemonday v1.0.27
) )

View file

@ -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/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 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk= 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/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View file

@ -3,7 +3,7 @@ module simpleauth
go 1.23.0 go 1.23.0
require ( 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 github.com/mattn/go-sqlite3 v1.14.24
golang.org/x/crypto v0.28.0 golang.org/x/crypto v0.28.0
) )

View file

@ -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/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 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= 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/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View file

@ -5,7 +5,7 @@ go 1.23.0
require ( require (
entgo.io/ent v0.14.1 entgo.io/ent v0.14.1
github.com/google/uuid v1.6.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/mattn/go-sqlite3 v1.14.23
) )

View file

@ -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/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 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= 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/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= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=

View file

@ -4,7 +4,7 @@ go 1.23.0
require ( require (
github.com/maddalax/htmgo/extensions/websocket v0.0.0-20241109180553-34e816ff7c8a 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 ( require (

View file

@ -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/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 h1:BYVo9NCLHgXvf5pCGUnVg8UE7d9mWOyLgWXYTgVTkyA=
github.com/maddalax/htmgo/extensions/websocket v0.0.0-20241109180553-34e816ff7c8a/go.mod h1:r6/VqntLp7VlAUpIXy3MWZMHs2EkPKJP5rJdDL8lFP4= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4= github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4=

View file

@ -4,7 +4,7 @@ go 1.23.0
require ( require (
github.com/gobwas/ws v1.4.0 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/puzpuzpuz/xsync/v3 v3.4.0
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
) )

View file

@ -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/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 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4= github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4=

View file

@ -21,8 +21,8 @@ func EnableExtension(app *h.App, opts opts.ExtensionOpts) {
panic("websocket: SessionId func is required") panic("websocket: SessionId func is required")
} }
service.Set[ws.SocketManager](app.Opts.ServiceLocator, service.Singleton, func() *ws.SocketManager { service.Set[wsutil.SocketManager](app.Opts.ServiceLocator, service.Singleton, func() *wsutil.SocketManager {
manager := ws.NewSocketManager(&opts) manager := wsutil.NewSocketManager(&opts)
manager.StartMetrics() manager.StartMetrics()
return manager return manager
}) })

View file

@ -6,7 +6,6 @@ import (
"github.com/gobwas/ws" "github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil" "github.com/gobwas/ws/wsutil"
ws2 "github.com/maddalax/htmgo/extensions/websocket/opts" 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/h"
"github.com/maddalax/htmgo/framework/service" "github.com/maddalax/htmgo/framework/service"
"log/slog" "log/slog"
@ -26,7 +25,7 @@ func WsHttpHandler(opts *ws2.ExtensionOpts) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
cc := r.Context().Value(h.RequestContextKey).(*h.RequestContext) cc := r.Context().Value(h.RequestContextKey).(*h.RequestContext)
locator := cc.ServiceLocator() locator := cc.ServiceLocator()
manager := service.Get[ws3.SocketManager](locator) manager := service.Get[SocketManager](locator)
sessionId := opts.SessionId(cc) sessionId := opts.SessionId(cc)
@ -47,7 +46,7 @@ func WsHttpHandler(opts *ws2.ExtensionOpts) http.HandlerFunc {
we don't want to block the writer we don't want to block the writer
*/ */
done := make(chan bool, 1000) done := make(chan bool, 1000)
writer := make(ws3.WriterChan, 1000) writer := make(WriterChan, 1000)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}

View file

@ -1,4 +1,4 @@
package ws package wsutil
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package ws package wsutil
import ( import (
ws2 "github.com/maddalax/htmgo/extensions/websocket/opts" ws2 "github.com/maddalax/htmgo/extensions/websocket/opts"

View file

@ -1,9 +1,10 @@
package ws package ws
import ( import (
"github.com/maddalax/htmgo/extensions/websocket/internal/wsutil"
"github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/h"
) )
func ManagerFromCtx(ctx *h.RequestContext) *SocketManager { func ManagerFromCtx(ctx *h.RequestContext) *wsutil.SocketManager {
return SocketManagerFromCtx(ctx) return wsutil.SocketManagerFromCtx(ctx)
} }

View file

@ -1,6 +1,7 @@
package ws package ws
import ( import (
"github.com/maddalax/htmgo/extensions/websocket/internal/wsutil"
"github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/extensions/websocket/session"
"github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service" "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 // PushElementCtx sends an element to the current session and swaps it into the page
func PushElementCtx(ctx *h.RequestContext, el *h.Element) bool { func PushElementCtx(ctx *h.RequestContext, el *h.Element) bool {
locator := ctx.ServiceLocator() locator := ctx.ServiceLocator()
socketManager := service.Get[SocketManager](locator) socketManager := service.Get[wsutil.SocketManager](locator)
socketId := session.GetSessionId(ctx) socketId := session.GetSessionId(ctx)
socket := socketManager.Get(string(socketId)) socket := socketManager.Get(string(socketId))
if socket == nil { if socket == nil {

View file

@ -1,6 +1,7 @@
package ws package ws
import ( import (
"github.com/maddalax/htmgo/extensions/websocket/internal/wsutil"
"github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/extensions/websocket/session"
"github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service" "github.com/maddalax/htmgo/framework/service"
@ -11,7 +12,7 @@ import (
func Every(ctx *h.RequestContext, interval time.Duration, cb func() bool) { func Every(ctx *h.RequestContext, interval time.Duration, cb func() bool) {
socketId := session.GetSessionId(ctx) socketId := session.GetSessionId(ctx)
locator := ctx.ServiceLocator() locator := ctx.ServiceLocator()
manager := service.Get[SocketManager](locator) manager := service.Get[wsutil.SocketManager](locator)
manager.RunIntervalWithSocket(string(socketId), interval, cb) manager.RunIntervalWithSocket(string(socketId), interval, cb)
} }

View file

@ -2,15 +2,16 @@ package ws
import ( import (
"fmt" "fmt"
"github.com/maddalax/htmgo/extensions/websocket/internal/wsutil"
"github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/extensions/websocket/session"
"sync" "sync"
) )
type MessageHandler struct { type MessageHandler struct {
manager *SocketManager manager *wsutil.SocketManager
} }
func NewMessageHandler(manager *SocketManager) *MessageHandler { func NewMessageHandler(manager *wsutil.SocketManager) *MessageHandler {
return &MessageHandler{manager: manager} return &MessageHandler{manager: manager}
} }
@ -76,7 +77,7 @@ func (h *MessageHandler) OnDomElementRemoved(handlerId string) {
handlers.Delete(handlerId) handlers.Delete(handlerId)
} }
func (h *MessageHandler) OnSocketDisconnected(event SocketEvent) { func (h *MessageHandler) OnSocketDisconnected(event wsutil.SocketEvent) {
sessionId := session.Id(event.SessionId) sessionId := session.Id(event.SessionId)
hashes, ok := sessionIdToHashes.Load(sessionId) hashes, ok := sessionIdToHashes.Load(sessionId)
if ok { if ok {

View file

@ -1,12 +1,13 @@
package ws package ws
import ( import (
"github.com/maddalax/htmgo/extensions/websocket/internal/wsutil"
"github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/extensions/websocket/session"
"github.com/maddalax/htmgo/framework/service" "github.com/maddalax/htmgo/framework/service"
) )
func StartListener(locator *service.Locator) { func StartListener(locator *service.Locator) {
manager := service.Get[SocketManager](locator) manager := service.Get[wsutil.SocketManager](locator)
manager.Listen(socketMessageListener) manager.Listen(socketMessageListener)
handler := NewMessageHandler(manager) handler := NewMessageHandler(manager)
go func() { go func() {
@ -22,9 +23,9 @@ func handle(handler *MessageHandler) {
handler.OnServerSideEvent(event) handler.OnServerSideEvent(event)
case event := <-socketMessageListener: case event := <-socketMessageListener:
switch event.Type { switch event.Type {
case DisconnectedEvent: case wsutil.DisconnectedEvent:
handler.OnSocketDisconnected(event) handler.OnSocketDisconnected(event)
case MessageEvent: case wsutil.MessageEvent:
handlerId, ok := event.Payload["id"].(string) handlerId, ok := event.Payload["id"].(string)
eventName, ok2 := event.Payload["event"].(string) eventName, ok2 := event.Payload["event"].(string)

View file

@ -1,11 +1,12 @@
package ws package ws
import ( import (
"github.com/maddalax/htmgo/extensions/websocket/internal/wsutil"
"github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/h"
) )
type Metrics struct { type Metrics struct {
Manager ManagerMetrics Manager wsutil.ManagerMetrics
Handler HandlerMetrics Handler HandlerMetrics
} }

View file

@ -1,6 +1,7 @@
package ws package ws
import ( import (
"github.com/maddalax/htmgo/extensions/websocket/internal/wsutil"
"github.com/maddalax/htmgo/extensions/websocket/session" "github.com/maddalax/htmgo/extensions/websocket/session"
"github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/h"
"github.com/puzpuzpuz/xsync/v3" "github.com/puzpuzpuz/xsync/v3"
@ -10,8 +11,8 @@ import (
type HandlerData struct { type HandlerData struct {
SessionId session.Id SessionId session.Id
Socket *SocketConnection Socket *wsutil.SocketConnection
Manager *SocketManager Manager *wsutil.SocketManager
} }
type Handler func(data HandlerData) 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 hashesToSessionId = xsync.NewMapOf[KeyHash, session.Id]()
var serverEventNamesToHash = xsync.NewMapOf[string, map[KeyHash]bool]() 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 serverSideMessageListener = make(chan ServerSideEvent, 100)
var lock = sync.Mutex{} var lock = sync.Mutex{}
var callingHandler = atomic.Bool{} var callingHandler = atomic.Bool{}

View file

@ -2,7 +2,7 @@ module github.com/maddalax/htmgo/framework-ui
go 1.23.0 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 ( require (
github.com/go-chi/chi/v5 v5.1.0 // indirect github.com/go-chi/chi/v5 v5.1.0 // indirect

View file

@ -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/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 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

View file

@ -7,6 +7,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"testing" "testing"
"time"
) )
func findScriptById(n *html.Node, id string) *html.Node { func findScriptById(n *html.Node, id string) *html.Node {
@ -87,7 +88,7 @@ func TestJsEval(t *testing.T) {
} }
func TestSetText(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) { func TestSetTextOnChildren(t *testing.T) {
@ -100,42 +101,42 @@ func TestSetTextOnChildren(t *testing.T) {
} }
func TestIncrement(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) { func TestSetInnerHtml(t *testing.T) {
htmlContent := Div(Span(UnsafeRaw("inner content"))) htmlContent := Div(Span(UnsafeRaw("inner content")))
compareIgnoreSpaces(t, renderJs(t, SetInnerHtml(htmlContent)), "this.innerHTML = `<div><span>inner content</span></div>`;") compareIgnoreSpaces(t, renderJs(t, SetInnerHtml(htmlContent)), "(self||this).innerHTML = `<div><span>inner content</span></div>`;")
} }
func TestSetOuterHtml(t *testing.T) { func TestSetOuterHtml(t *testing.T) {
htmlContent := Div(Span(UnsafeRaw("outer content"))) htmlContent := Div(Span(UnsafeRaw("outer content")))
compareIgnoreSpaces(t, renderJs(t, SetOuterHtml(htmlContent)), "this.outerHTML = `<div><span>outer content</span></div>`;") compareIgnoreSpaces(t, renderJs(t, SetOuterHtml(htmlContent)), "(self||this).outerHTML = `<div><span>outer content</span></div>`;")
} }
func TestAddAttribute(t *testing.T) { 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) { func TestSetDisabled(t *testing.T) {
compareIgnoreSpaces(t, renderJs(t, SetDisabled(true)), "this.setAttribute('disabled', 'true');") compareIgnoreSpaces(t, renderJs(t, SetDisabled(true)), "(self||this).setAttribute('disabled', 'true');")
compareIgnoreSpaces(t, renderJs(t, SetDisabled(false)), "this.removeAttribute('disabled');") compareIgnoreSpaces(t, renderJs(t, SetDisabled(false)), "(self||this).removeAttribute('disabled');")
} }
func TestRemoveAttribute(t *testing.T) { 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) { 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) { 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) { 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) { func TestToggleClassOnElement(t *testing.T) {
@ -206,7 +207,7 @@ func TestAlert(t *testing.T) {
} }
func TestRemove(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) { func TestSubmitFormOnEnter(t *testing.T) {
@ -394,5 +395,23 @@ func TestConsoleLog(t *testing.T) {
func TestSetValue(t *testing.T) { func TestSetValue(t *testing.T) {
t.Parallel() 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)
`)
} }

View file

@ -6,6 +6,7 @@ import (
"github.com/maddalax/htmgo/framework/hx" "github.com/maddalax/htmgo/framework/hx"
"github.com/maddalax/htmgo/framework/internal/util" "github.com/maddalax/htmgo/framework/internal/util"
"strings" "strings"
"time"
) )
type LifeCycle struct { type LifeCycle struct {
@ -163,7 +164,7 @@ func NewComplexJsCommand(command string) ComplexJsCommand {
// SetText sets the inner text of the element. // SetText sets the inner text of the element.
func SetText(text string) SimpleJsCommand { func SetText(text string) SimpleJsCommand {
// language=JavaScript // 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. // 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. // Increment increments the inner text of the element by the given amount.
func Increment(amount int) SimpleJsCommand { func Increment(amount int) SimpleJsCommand {
// language=JavaScript // 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. // SetInnerHtml sets the inner HTML of the element.
func SetInnerHtml(r Ren) SimpleJsCommand { func SetInnerHtml(r Ren) SimpleJsCommand {
// language=JavaScript // 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. // SetOuterHtml sets the outer HTML of the element.
func SetOuterHtml(r Ren) SimpleJsCommand { func SetOuterHtml(r Ren) SimpleJsCommand {
// language=JavaScript // 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. // AddAttribute adds the given attribute to the element.
func AddAttribute(name, value string) SimpleJsCommand { func AddAttribute(name, value string) SimpleJsCommand {
// language=JavaScript // 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. // 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. // RemoveAttribute removes the given attribute from the element.
func RemoveAttribute(name string) SimpleJsCommand { func RemoveAttribute(name string) SimpleJsCommand {
// language=JavaScript // 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. // AddClass adds the given class to the element.
func AddClass(class string) SimpleJsCommand { func AddClass(class string) SimpleJsCommand {
// language=JavaScript // 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. // RemoveClass removes the given class from the element.
func RemoveClass(class string) SimpleJsCommand { func RemoveClass(class string) SimpleJsCommand {
// language=JavaScript // 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. // ToggleClass toggles the given class on the element.
func ToggleClass(class string) SimpleJsCommand { func ToggleClass(class string) SimpleJsCommand {
// language=JavaScript // 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. // ToggleText toggles the given text on the element.
@ -391,7 +392,7 @@ func Alert(text string) SimpleJsCommand {
// Remove removes the element from the DOM. // Remove removes the element from the DOM.
func Remove() SimpleJsCommand { func Remove() SimpleJsCommand {
// language=JavaScript // language=JavaScript
return SimpleJsCommand{Command: "this.remove()"} return SimpleJsCommand{Command: "(self || this).remove()"}
} }
// EvalJs evaluates the given JavaScript code. // EvalJs evaluates the given JavaScript code.
@ -399,15 +400,21 @@ func EvalJs(js string) ComplexJsCommand {
return NewComplexJsCommand(js) return NewComplexJsCommand(js)
} }
func EvalCommandsOnSelector(selector string, cmds ...Command) ComplexJsCommand { func CombineCommands(cmds ...Command) string {
lines := make([]string, len(cmds)) lines := make([]string, len(cmds))
for i, cmd := range cmds { for i, cmd := range cmds {
lines[i] = Render(cmd) lines[i] = Render(cmd)
lines[i] = strings.ReplaceAll(lines[i], "(self || this).", "self.")
lines[i] = strings.ReplaceAll(lines[i], "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 // some commands set the element we need to fix it so we arent redeclaring it
lines[i] = strings.ReplaceAll(lines[i], "let element =", "element =") lines[i] = strings.ReplaceAll(lines[i], "let element =", "element =")
} }
code := strings.Join(lines, "\n") code := strings.Join(lines, "\n")
return code
}
func EvalCommandsOnSelector(selector string, cmds ...Command) ComplexJsCommand {
code := CombineCommands(cmds...)
return EvalJs(fmt.Sprintf(` return EvalJs(fmt.Sprintf(`
let element = document.querySelector("%s"); let element = document.querySelector("%s");
@ -444,7 +451,7 @@ func ConsoleLog(text string) SimpleJsCommand {
// SetValue sets the value of the element. // SetValue sets the value of the element.
func SetValue(value string) SimpleJsCommand { func SetValue(value string) SimpleJsCommand {
// language=JavaScript // 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. // SubmitFormOnEnter submits the form when the user presses the enter key.
@ -478,3 +485,31 @@ func InjectScriptIfNotExist(src string) ComplexJsCommand {
} }
`, src, src)) `, 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())
}

View file

@ -48,7 +48,7 @@ func TestRender(t *testing.T) {
div.attributes.Set("data-attr-1", "value") div.attributes.Set("data-attr-1", "value")
expected := `<div data-attr-1="value" id="my-div" data-attr-2="value" data-attr-3="value" hx-on::before-request="this.innerText = &#39;before request&#39;;" hx-on::after-request="this.innerText = &#39;after request&#39;;"><div>hello, world</div>hello, child</div>` expected := `<div data-attr-1="value" id="my-div" data-attr-2="value" data-attr-3="value" hx-on::before-request="(self || this).innerText = &#39;before request&#39;;" hx-on::after-request="(self || this).innerText = &#39;after request&#39;;"><div>hello, world</div>hello, child</div>`
result := Render(div) result := Render(div)
assert.Equal(t, assert.Equal(t,

View file

@ -47,3 +47,5 @@ var ToggleText = h.ToggleText
var ToggleTextOnSibling = h.ToggleTextOnSibling var ToggleTextOnSibling = h.ToggleTextOnSibling
var ToggleTextOnChildren = h.ToggleTextOnChildren var ToggleTextOnChildren = h.ToggleTextOnChildren
var ToggleTextOnParent = h.ToggleTextOnParent var ToggleTextOnParent = h.ToggleTextOnParent
var RunAfterTimeout = h.RunAfterTimeout
var RunOnInterval = h.RunOnInterval

View file

@ -6,8 +6,8 @@ require (
github.com/alecthomas/chroma/v2 v2.14.0 github.com/alecthomas/chroma/v2 v2.14.0
github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/chi/v5 v5.1.0
github.com/google/uuid v1.6.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/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499 github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241116145200-825c4dd7ecca
github.com/yuin/goldmark v1.7.4 github.com/yuin/goldmark v1.7.4
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
) )

View file

@ -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/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 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/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-20241116145200-825c4dd7ecca h1:yAs6jEsQjQ/Dg47DA0clJ3rgzxvjCS+lQk5NovRx0WQ=
github.com/maddalax/htmgo/tools/html-to-htmgo v0.0.0-20241109183230-b234ead96499/go.mod h1:FraJsj3NRuLBQDk83ZVa+psbNRNLe+rajVtVhYMEme4= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View file

@ -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
}

View file

@ -1,14 +1,15 @@
package main package main
import ( import (
"fmt"
"github.com/maddalax/htmgo/framework/h" "github.com/maddalax/htmgo/framework/h"
"github.com/maddalax/htmgo/framework/service" "github.com/maddalax/htmgo/framework/service"
"htmgo-site/__htmgo" "htmgo-site/__htmgo"
"htmgo-site/internal/cache" "htmgo-site/internal/cache"
"htmgo-site/internal/markdown" "htmgo-site/internal/markdown"
"htmgo-site/internal/sitemap" "htmgo-site/internal/sitemap"
"htmgo-site/internal/urlhelper"
"io/fs" "io/fs"
"log"
"net/http" "net/http"
) )
@ -20,13 +21,17 @@ func main() {
service.Set(locator, service.Singleton, markdown.NewRenderer) service.Set(locator, service.Singleton, markdown.NewRenderer)
service.Set(locator, service.Singleton, cache.NewSimpleCache) service.Set(locator, service.Singleton, cache.NewSimpleCache)
fmt.Printf("starting up server2\n")
h.Start(h.AppOpts{ h.Start(h.AppOpts{
ServiceLocator: locator, ServiceLocator: locator,
LiveReload: true, LiveReload: true,
Register: func(app *h.App) { 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) { app.UseWithContext(func(w http.ResponseWriter, r *http.Request, context map[string]any) {
context["embeddedMarkdown"] = markdownAssets context["embeddedMarkdown"] = markdownAssets
}) })

View file

@ -2,7 +2,7 @@ module starter-template
go 1.23.0 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 ( require (
github.com/go-chi/chi/v5 v5.1.0 // indirect github.com/go-chi/chi/v5 v5.1.0 // indirect

View file

@ -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/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 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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.20241116145200-825c4dd7ecca h1:XiXldwjy1R6dqh7nr6X0coCu5OyA4WZsXVkayM4f4Tw=
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/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=