Skip to content

Commit

Permalink
wip: bundle components
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jul 23, 2024
1 parent b597449 commit 2a9b167
Show file tree
Hide file tree
Showing 11 changed files with 857 additions and 15 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,6 @@ web_modules/
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
Expand Down Expand Up @@ -353,4 +349,4 @@ dist
# Azure Toolkit for IntelliJ plugin
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij

# End of https://www.toptal.com/developers/gitignore/api/go,intellij,webstorm,node
# End of https://www.toptal.com/developers/gitignore/api/go,intellij,webstorm,node
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.DEFAULT_GOAL := release

GO ?= go
GO ?= go
GO_RUN_TOOLS ?= $(GO) run -modfile ./tools/go.mod
GO_TEST ?= $(GO_RUN_TOOLS) gotest.tools/gotestsum --format pkgname
GO_TEST ?= $(GO_RUN_TOOLS) gotest.tools/gotestsum --format pkgname
GO_RELEASER ?= $(GO_RUN_TOOLS) github.com/goreleaser/goreleaser
GO_BENCHSTAT ?= $(GO_RUN_TOOLS) golang.org/x/perf/cmd/benchstat
GO_MOD ?= $(shell ${GO} list -m)
GO_MOD ?= $(shell ${GO} list -m)

.PHONY: release
release: ## Release the project.
Expand All @@ -15,6 +15,10 @@ release: ## Release the project.
generate: ## Generate code.
$(GO) generate ./...

.PHONY: bundle
bundle: ## Bundle the project.
./node_modules/.bin/esbuild src/**/*.js --minify --sourcemap --bundle --outfile=dist/out.js

.PHONY: fmt
fmt: ## Run go fmt against code.
$(GO_RUN_TOOLS) mvdan.cc/gofumpt -w .
Expand Down
128 changes: 128 additions & 0 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package bundle

import (
"errors"
"fmt"
"io/fs"
"path"
"strings"

"github.com/gofiber/fiber/v2"
htmx "github.com/zeiss/fiber-htmx"
)

// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
Next func(c *fiber.Ctx) bool
// Bundle is the http.FileSystem to serve.
Bundle fs.FS `json:"-"`
// Root is the root directory of the Bundle.
Root string `json:"root"`
// PathPrefix is the optional prefix used to serve the filesystem content.
PathPrefix string `json:"path_prefix"`
// MaxAge defines the max-age of the Cache-Control header in seconds.
MaxAge int `json:"max_age"`
// ContentTypeCharset defines the charset of the Content-Type header.
ContentTypeCharset string `json:"content_type_charset"`
}

// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Bundle: htmx.Bundle,
Root: htmx.BundleFoler,
PathPrefix: "",
MaxAge: 0,
ContentTypeCharset: "",
}

// New ...
func New(config ...Config) fiber.Handler {

Check failure on line 41 in bundle/bundle.go

View workflow job for this annotation

GitHub Actions / lint

cyclomatic complexity 13 of func `New` is high (> 10) (gocyclo)
cfg := configDefault(config...)

return func(c *fiber.Ctx) error {
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}

method := c.Method()

if method != fiber.MethodGet && method != fiber.MethodHead {
return c.Next()
}

p := strings.TrimPrefix(c.Path(), cfg.PathPrefix)

f, err := cfg.Bundle.Open(path.Join(htmx.BundleFoler, p))
if err != nil && errors.Is(err, fs.ErrNotExist) {
return fiber.ErrNotFound
}

if err != nil {
return fiber.ErrInternalServerError
}

stat, err := f.Stat()
if err != nil {
return fmt.Errorf("failed to stat: %w", err)
}

if stat.IsDir() {
return fiber.ErrForbidden
}

c.Type(getFileExtension(stat.Name()))

c.Status(fiber.StatusOK)

contentLength := int(stat.Size())

if method == fiber.MethodGet {
c.Response().SetBodyStream(f, contentLength)
return nil
}

if method == fiber.MethodHead {
c.Request().ResetBody()
c.Response().SkipBody = true
c.Response().Header.SetContentLength(contentLength)

if err := f.Close(); err != nil {
return fmt.Errorf("failed to close: %w", err)
}

return nil
}

return c.Next()
}
}

func getFileExtension(p string) string {
n := strings.LastIndexByte(p, '.')
if n < 0 {
return ""
}
return p[n:]
}

// Helper function to set default values
func configDefault(config ...Config) Config {
if len(config) < 1 {
return ConfigDefault
}

// Override default config
cfg := config[0]

if cfg.Bundle == nil {
cfg.Bundle = ConfigDefault.Bundle
}

if cfg.Root == "" {
cfg.Root = ConfigDefault.Root
}

return cfg
}
71 changes: 71 additions & 0 deletions components/dropdowns/select.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dropdowns

import (
"github.com/zeiss/fiber-htmx/components/forms"

htmx "github.com/zeiss/fiber-htmx"
)

// SingleSelectProps ...
type SingleSelectProps struct {
URL string
ID string
}

// SingleSelect ...
func SingleSelect(props SingleSelectProps, children ...htmx.Node) htmx.Node {
return Dropdown(
DropdownProps{
ClassNames: htmx.ClassNames{},
},
DropdownButton(
DropdownButtonProps{
ClassNames: htmx.ClassNames{
"btn-outline": true,
},
},
htmx.ID(props.ID),
htmx.Text("Dropdown"),
),
DropdownMenuItems(
DropdownMenuItemsProps{},
forms.TextInputBordered(
forms.TextInputProps{
ClassNames: htmx.ClassNames{
"input-sm": true,
},
Placeholder: "Search...",
},
htmx.HxPost(props.URL),
htmx.HxTrigger("input changed delay:500ms, search"),
htmx.HxTarget("#search-results"),
),
htmx.Div(
htmx.ID("search-results"),
),
),
)
}

// SingleSelectOptionProps ...
type SingleSelectOptionProps struct {
ID string
Name string
Value string
}

// SingleSelectOption ...
func SingleSelectOption(props SingleSelectOptionProps, children ...htmx.Node) htmx.Node {
return DropdownMenuItem(
DropdownMenuItemProps{},
htmx.Input(
htmx.Attribute("type", "hidden"),
htmx.Attribute("name", props.Name),
htmx.Value(props.Value),
),
htmx.A(
htmx.Group(children...),
htmx.HyperScript(`on click put parentElement.innerHTML of me into `+props.ID+`.innerHTML`),
),
)
}
Loading

0 comments on commit 2a9b167

Please sign in to comment.