feat:add claasf (#31)
* feat:add claasf * refactor(tag.go) * refactor(cache.go) * refactor(attribute.go) * refactor(app.go) * refactor(attribute.go)
This commit is contained in:
parent
70228912be
commit
a0f5b5dfd5
4 changed files with 22 additions and 18 deletions
|
|
@ -3,9 +3,6 @@ package h
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
"github.com/maddalax/htmgo/framework/hx"
|
|
||||||
"github.com/maddalax/htmgo/framework/service"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -13,6 +10,10 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/maddalax/htmgo/framework/hx"
|
||||||
|
"github.com/maddalax/htmgo/framework/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RequestContext struct {
|
type RequestContext struct {
|
||||||
|
|
@ -214,9 +215,8 @@ func (app *App) start() {
|
||||||
|
|
||||||
port := ":3000"
|
port := ":3000"
|
||||||
slog.Info(fmt.Sprintf("Server started at localhost%s", port))
|
slog.Info(fmt.Sprintf("Server started at localhost%s", port))
|
||||||
err := http.ListenAndServe(port, app.Router)
|
|
||||||
|
|
||||||
if err != nil {
|
if err := http.ListenAndServe(port, app.Router); err != nil {
|
||||||
// If we are in watch mode, just try to kill any processes holding that port
|
// If we are in watch mode, just try to kill any processes holding that port
|
||||||
// and try again
|
// and try again
|
||||||
if IsDevelopment() && IsWatchMode() {
|
if IsDevelopment() && IsWatchMode() {
|
||||||
|
|
@ -228,14 +228,16 @@ func (app *App) start() {
|
||||||
cmd := exec.Command("bash", "-c", fmt.Sprintf("kill -9 $(lsof -ti%s)", port))
|
cmd := exec.Command("bash", "-c", fmt.Sprintf("kill -9 $(lsof -ti%s)", port))
|
||||||
cmd.Run()
|
cmd.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 50)
|
time.Sleep(time.Millisecond * 50)
|
||||||
err = http.ListenAndServe(":3000", app.Router)
|
|
||||||
if err != nil {
|
// Try to start server again
|
||||||
|
if err := http.ListenAndServe(port, app.Router); err != nil {
|
||||||
|
slog.Error("Failed to restart server", "error", err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,10 @@ package h
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/maddalax/htmgo/framework/hx"
|
"github.com/maddalax/htmgo/framework/hx"
|
||||||
"github.com/maddalax/htmgo/framework/internal/datastructure"
|
"github.com/maddalax/htmgo/framework/internal/datastructure"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AttributeMap = map[string]any
|
type AttributeMap = map[string]any
|
||||||
|
|
@ -89,9 +90,7 @@ func Checked() Ren {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Id(value string) Ren {
|
func Id(value string) Ren {
|
||||||
if strings.HasPrefix(value, "#") {
|
value = strings.TrimPrefix(value, "#")
|
||||||
value = value[1:]
|
|
||||||
}
|
|
||||||
return Attribute("id", value)
|
return Attribute("id", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -193,6 +192,11 @@ func Class(value ...string) *AttributeR {
|
||||||
return Attribute("class", MergeClasses(value...))
|
return Attribute("class", MergeClasses(value...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ClassF(format string, args ...interface{}) *AttributeR {
|
||||||
|
atr := fmt.Sprintf(format, args...)
|
||||||
|
return Attribute("class", atr)
|
||||||
|
}
|
||||||
|
|
||||||
func ClassX(value string, m ClassMap) Ren {
|
func ClassX(value string, m ClassMap) Ren {
|
||||||
builder := strings.Builder{}
|
builder := strings.Builder{}
|
||||||
builder.WriteString(value)
|
builder.WriteString(value)
|
||||||
|
|
|
||||||
|
|
@ -277,7 +277,7 @@ func (c *CachedNode) ClearExpired() {
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
deletedCount := 0
|
deletedCount := 0
|
||||||
if c.isByKey == true {
|
if c.isByKey {
|
||||||
if c.byKeyCache != nil && c.byKeyExpiration != nil {
|
if c.byKeyCache != nil && c.byKeyExpiration != nil {
|
||||||
for key := range c.byKeyCache {
|
for key := range c.byKeyCache {
|
||||||
expir, ok := c.byKeyExpiration[key]
|
expir, ok := c.byKeyExpiration[key]
|
||||||
|
|
@ -303,7 +303,7 @@ func (c *CachedNode) ClearExpired() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CachedNode) Render(ctx *RenderContext) {
|
func (c *CachedNode) Render(ctx *RenderContext) {
|
||||||
if c.isByKey == true {
|
if c.isByKey {
|
||||||
panic("CachedPerKey should not be rendered directly")
|
panic("CachedPerKey should not be rendered directly")
|
||||||
} else {
|
} else {
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
|
|
|
||||||
|
|
@ -268,9 +268,7 @@ func TagF(tag string, format string, args ...interface{}) *Element {
|
||||||
case *AttributeMapOrdered:
|
case *AttributeMapOrdered:
|
||||||
children = append(children, d)
|
children = append(children, d)
|
||||||
case *ChildList:
|
case *ChildList:
|
||||||
for _, child := range d.Children {
|
children = append(children, d.Children...)
|
||||||
children = append(children, child)
|
|
||||||
}
|
|
||||||
case *AttributeR:
|
case *AttributeR:
|
||||||
children = append(children, d)
|
children = append(children, d)
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue