From 2a7dc481b1130efde3b14ac492939b4f0d5f9fd0 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 4 May 2022 12:23:53 +0200 Subject: [PATCH] chore: tailwindcss theme + goreleaser basic recipe --- .chglog/CHANGELOG.tpl.md | 22 + .chglog/config.yml | 28 + .githooks/commit-msg | 18 + .gitignore | 5 +- .goreleaser.yaml | 44 + CHANGELOG.md | 8 + Makefile | 28 +- commitlint.config.js | 1 + go.mod | 22 +- go.sum | 40 +- internal/server/assets/src/main.css | 3 + internal/server/fs.go | 20 + internal/server/route/form.go | 35 + internal/server/route/handler.go | 6 +- internal/server/server.go | 9 +- .../server/template/blocks/form.html.tmpl | 28 +- .../blocks/form_input_array.html.tmpl | 15 +- .../blocks/form_input_boolean.html.tmpl | 22 +- .../blocks/form_input_number.html.tmpl | 5 +- .../blocks/form_input_string.html.tmpl | 6 +- .../server/template/blocks/form_row.html.tmpl | 8 +- .../server/template/blocks/head.html.tmpl | 5 - .../server/template/layouts/index.html.tmpl | 5 +- internal/server/template/template.go | 26 +- modd.conf | 12 +- package-lock.json | 1990 +++++++++++++++++ package.json | 25 + script/release | 104 - tailwind.config.js | 9 + 29 files changed, 2337 insertions(+), 212 deletions(-) create mode 100755 .chglog/CHANGELOG.tpl.md create mode 100644 .chglog/config.yml create mode 100755 .githooks/commit-msg create mode 100644 .goreleaser.yaml create mode 100644 CHANGELOG.md create mode 100644 commitlint.config.js create mode 100644 internal/server/assets/src/main.css create mode 100644 internal/server/fs.go create mode 100644 package-lock.json create mode 100644 package.json delete mode 100755 script/release create mode 100644 tailwind.config.js diff --git a/.chglog/CHANGELOG.tpl.md b/.chglog/CHANGELOG.tpl.md new file mode 100755 index 0000000..e19ffdd --- /dev/null +++ b/.chglog/CHANGELOG.tpl.md @@ -0,0 +1,22 @@ +{{ range .Versions }} + +## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }}) + +{{ range .CommitGroups -}} +### {{ .Title }} + +{{ range .Commits -}} +* {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} + +{{- if .NoteGroups -}} +{{ range .NoteGroups -}} +### {{ .Title }} + +{{ range .Notes }} +{{ .Body }} +{{ end }} +{{ end -}} +{{ end -}} +{{ end -}} \ No newline at end of file diff --git a/.chglog/config.yml b/.chglog/config.yml new file mode 100644 index 0000000..53f57de --- /dev/null +++ b/.chglog/config.yml @@ -0,0 +1,28 @@ +style: github +template: CHANGELOG.tpl.md +info: + title: CHANGELOG + repository_url: https://github.com/Bornholm/formidable +options: + commits: + # filters: + # Type: + # - feat + # - fix + # - perf + # - refactor + commit_groups: + # title_maps: + # feat: Features + # fix: Bug Fixes + # perf: Performance Improvements + # refactor: Code Refactoring + header: + pattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$" + pattern_maps: + - Type + - Scope + - Subject + notes: + keywords: + - BREAKING CHANGE \ No newline at end of file diff --git a/.githooks/commit-msg b/.githooks/commit-msg new file mode 100755 index 0000000..9a6a128 --- /dev/null +++ b/.githooks/commit-msg @@ -0,0 +1,18 @@ +#!/bin/bash + +set -eo pipefail + +DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)" +export PATH="${PATH}:${DIR}/../node_modules/.bin" + +function lint_commit_message { + local commit_message_file="$1" + cat "$commit_message_file" | commitlint +} + +function main { + local commit_message_file="$1" + lint_commit_message "$commit_message_file" +} + +main $@ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 43b7f71..2ba58a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ /.env -/bin \ No newline at end of file +/bin +/node_modules +/internal/server/assets/dist/* +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..c31fd0e --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,44 @@ +project_name: frmd +before: + hooks: + # You may remove this if you don't use go modules. + - go mod tidy + # you may remove this if you don't need go generate + - go generate ./... + - make GITCHLOG_ARGS="--next-tag {{ incpatch .Version }}-next --output CHANGELOG.md" changelog +builds: + - id: frmd + env: + - CGO_ENABLED=0 + ldflags: + - -s + - -w + - -X 'main.GitRef={{ .Commit }}' + - -X 'main.ProjectVersion={{ .Version }}' + - -X 'main.BuildDate={{ .Date }}' + gcflags: + - -trimpath="${PWD}" + asmflags: + - -trimpath="${PWD}" + goos: + - linux + - windows + - darwin + main: ./cmd/frmd +archives: + - replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ incpatch .Version }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d3ab185 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ + + +## 0.0.1-next (2022-05-04) + +### Feat + +* **edit:** tailwindcss theme + diff --git a/Makefile b/Makefile index c4a5556..3853fa3 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,10 @@ LINT_ARGS ?= --timeout 5m FRMD_CMD ?= SHELL = /bin/bash +TAILWINDCSS_ARGS ?= +GORELEASER_VERSION ?= v1.8.3 +GORELEASER_ARGS ?= --auto-snapshot --rm-dist +GITCHLOG_ARGS ?= .PHONY: help help: ## Display this help @@ -21,11 +25,29 @@ build: build-frmd ## Build artefacts build-frmd: ## Build executable CGO_ENABLED=0 go build -v -o ./bin/frmd ./cmd/frmd +.PHONY: tailwind +tailwind: + npx tailwindcss -i ./internal/server/assets/src/main.css -o ./internal/server/assets/dist/main.css $(TAILWINDCSS_ARGS) + +internal/server/assets/dist/main.css: tailwind + .env: cp .env.dist .env -deps: +.PHONY: deps +deps: node_modules + +node_modules: + npm ci .PHONY: release -release: - ./misc/script/release \ No newline at end of file +release: deps + VERSION=$(GORELEASER_VERSION) curl -sfL https://goreleaser.com/static/run | bash /dev/stdin $(GORELEASER_ARGS) + +.PHONY: changelog +changelog: + go run -mod=readonly github.com/git-chglog/git-chglog/cmd/git-chglog@v0.15.1 $(GITCHLOG_ARGS) + + +install-git-hooks: + git config core.hooksPath .githooks \ No newline at end of file diff --git a/commitlint.config.js b/commitlint.config.js new file mode 100644 index 0000000..6eaf62b --- /dev/null +++ b/commitlint.config.js @@ -0,0 +1 @@ +module.exports = {extends: ['@commitlint/config-conventional']} \ No newline at end of file diff --git a/go.mod b/go.mod index affae50..51d8d31 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ module forge.cadoles.com/wpetit/formidable -go 1.17 +go 1.18 require ( + github.com/Masterminds/sprig/v3 v3.2.2 github.com/pkg/errors v0.9.1 github.com/urfave/cli/v2 v2.4.0 ) @@ -10,7 +11,6 @@ require ( require ( github.com/Masterminds/goutils v1.1.1 // indirect github.com/Masterminds/semver/v3 v3.1.1 // indirect - github.com/Masterminds/sprig/v3 v3.2.2 // indirect github.com/google/uuid v1.1.1 // indirect github.com/huandu/xstrings v1.3.1 // indirect github.com/imdario/mergo v0.3.11 // indirect @@ -22,23 +22,9 @@ require ( ) require ( - github.com/awesome-gocui/gocui v1.1.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/gdamore/encoding v1.0.0 // indirect - github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 // indirect + github.com/davecgh/go-spew v1.1.1 github.com/go-chi/chi v1.5.4 - github.com/jroimartin/gocui v0.5.0 // indirect - github.com/lucasb-eyer/go-colorful v1.2.0 // indirect - github.com/mattn/go-runewidth v0.0.13 // indirect - github.com/nsf/termbox-go v1.1.1 // indirect - github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8 // indirect - github.com/rivo/uniseg v0.2.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 // indirect - github.com/skanehira/gocui-component v0.0.0-20190406233618-9b1c71353c96 // indirect - github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect - golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect - golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect - golang.org/x/text v0.3.6 // indirect + github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 ) diff --git a/go.sum b/go.sum index d0155e9..8d8eaa1 100644 --- a/go.sum +++ b/go.sum @@ -5,18 +5,11 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8= github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk= -github.com/awesome-gocui/gocui v1.1.0 h1:db2j7yFEoHZjpQFeE2xqiatS8bm1lO3THeLwE6MzOII= -github.com/awesome-gocui/gocui v1.1.0/go.mod h1:M2BXkrp7PR97CKnPRT7Rk0+rtswChPtksw/vRAESGpg= github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= -github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= -github.com/gdamore/tcell/v2 v2.4.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU= -github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1 h1:QqwPZCwh/k1uYqq6uXSb9TRDhTkfQbO80v8zhnIe5zM= -github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04= github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs= github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= @@ -25,64 +18,37 @@ github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= -github.com/jroimartin/gocui v0.5.0 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4= -github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE= -github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= -github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= -github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= -github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= -github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY= -github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +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/rivo/tview v0.0.0-20220307222120-9994674d60a8 h1:xe+mmCnDN82KhC010l3NfYlA8ZbOuzbXAzSYBa6wbMc= -github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8/go.mod h1:WIfMkQNY+oq/mWwtsjOYHIZBuwthioY2srOmljJkTnk= -github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 h1:TToq11gyfNlrMFZiYujSekIsPd9AmsA2Bj/iv+s4JHE= github.com/santhosh-tekuri/jsonschema/v5 v5.0.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -github.com/skanehira/gocui-component v0.0.0-20190406233618-9b1c71353c96 h1:iNHvqWqQWIQ0wdWkcHW2sQhvRaVlCrnFmUftk7LcbrE= -github.com/skanehira/gocui-component v0.0.0-20190406233618-9b1c71353c96/go.mod h1:uhDvc/srGKwvK9bGt4zlfTiywMLL7ngz44Yp2nQwTjE= github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/urfave/cli/v2 v2.4.0 h1:m2pxjjDFgDxSPtO8WSdbndj17Wu2y8vOT86wE/tjr+I= github.com/urfave/cli/v2 v2.4.0/go.mod h1:NX9W0zmTvedE5oDoOMs2RTC8RvdK98NTYZE5LbaEYPg= -github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo= -github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 h1:bXoxMPcSLOq08zI3/c5dEBT6lE4eh+jOh886GHrn6V8= golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY= -golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE= -golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/server/assets/src/main.css b/internal/server/assets/src/main.css new file mode 100644 index 0000000..bd6213e --- /dev/null +++ b/internal/server/assets/src/main.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; \ No newline at end of file diff --git a/internal/server/fs.go b/internal/server/fs.go new file mode 100644 index 0000000..feff4c8 --- /dev/null +++ b/internal/server/fs.go @@ -0,0 +1,20 @@ +package server + +import ( + "embed" +) + +var ( + //go:embed template/layouts/* template/blocks/* + templates embed.FS + //go:embed assets/dist/* + assets embed.FS +) + +func getEmbeddedTemplates() embed.FS { + return templates +} + +func getEmbeddedAssets() embed.FS { + return assets +} diff --git a/internal/server/route/form.go b/internal/server/route/form.go index f1882b8..90473b0 100644 --- a/internal/server/route/form.go +++ b/internal/server/route/form.go @@ -3,6 +3,7 @@ package route import ( "net/http" "net/url" + "strconv" "strings" "forge.cadoles.com/wpetit/formidable/internal/jsonpointer" @@ -66,6 +67,10 @@ func createHandleFormHandlerFunc(schema *jsonschema.Schema, defaults, values int data.Error = validationErr } + if data.Error == nil { + data.SuccessMessage = "Data updated." + } + if err := template.Exec("index.html.tmpl", w, data); err != nil { panic(errors.WithStack(err)) } @@ -89,6 +94,10 @@ func handleForm(form url.Values, schema *jsonschema.Schema, values interface{}) switch prefix { case "bool": + if fieldValues[0] == "" { + continue + } + booVal, err := parseBoolean(fieldValues[0]) if err != nil { return nil, errors.Wrapf(err, "could not parse boolean field '%s'", property) @@ -101,6 +110,23 @@ func handleForm(form url.Values, schema *jsonschema.Schema, values interface{}) return nil, errors.Wrapf(err, "could not set property '%s' with value '%v'", property, fieldValues[0]) } + case "num": + if fieldValues[0] == "" { + continue + } + + numVal, err := parseNumeric(fieldValues[0]) + if err != nil { + return nil, errors.Wrapf(err, "could not parse numeric field '%s'", property) + } + + pointer := jsonpointer.New(property) + + values, err = pointer.Force(values, numVal) + if err != nil { + return nil, errors.Wrapf(err, "could not set property '%s' with value '%v'", property, fieldValues[0]) + } + case "add": pointer := jsonpointer.New(property) @@ -146,6 +172,15 @@ func parseBoolean(value string) (bool, error) { } } +func parseNumeric(value string) (float64, error) { + numVal, err := strconv.ParseFloat(value, 64) + if err != nil { + return 0, errors.WithStack(err) + } + + return numVal, nil +} + func parseFieldName(name string) (string, string, error) { tokens := strings.SplitN(name, ":", 2) diff --git a/internal/server/route/handler.go b/internal/server/route/handler.go index c90db87..b30d08d 100644 --- a/internal/server/route/handler.go +++ b/internal/server/route/handler.go @@ -1,12 +1,14 @@ package route import ( + "net/http" + "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/santhosh-tekuri/jsonschema/v5" ) -func NewHandler(schema *jsonschema.Schema, defaults, values interface{}) (*chi.Mux, error) { +func NewHandler(schema *jsonschema.Schema, defaults, values interface{}, assetsHandler http.Handler) (*chi.Mux, error) { router := chi.NewRouter() router.Use(middleware.RequestID) @@ -15,5 +17,7 @@ func NewHandler(schema *jsonschema.Schema, defaults, values interface{}) (*chi.M router.Get("/", createRenderFormHandlerFunc(schema, defaults, values)) router.Post("/", createHandleFormHandlerFunc(schema, defaults, values)) + router.Handle("/assets/*", assetsHandler) + return router, nil } diff --git a/internal/server/server.go b/internal/server/server.go index b398614..a874570 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -60,13 +60,18 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e } }() - if err := template.Load(); err != nil { + templates := getEmbeddedTemplates() + + if err := template.Load(templates, "template"); err != nil { errs <- errors.WithStack(err) return } - handler, err := route.NewHandler(s.schema, s.defaults, s.values) + assets := getEmbeddedAssets() + assetsHandler := http.FileServer(http.FS(assets)) + + handler, err := route.NewHandler(s.schema, s.defaults, s.values, assetsHandler) if err != nil { errs <- errors.WithStack(err) diff --git a/internal/server/template/blocks/form.html.tmpl b/internal/server/template/blocks/form.html.tmpl index 1334b5d..9cefe73 100644 --- a/internal/server/template/blocks/form.html.tmpl +++ b/internal/server/template/blocks/form.html.tmpl @@ -3,17 +3,33 @@ - +
+ {{ .Schema.Title }} + {{ .Schema.Description }} + - +
-
- {{ .Schema.Title }} - {{ .Schema.Description }} -
+ {{ if .Error }} +
+ There is some(s) error(s) ! +
+ {{ .Error.Message }} +
+ {{ end }} + {{ if .SuccessMessage }} +
+ Success ! +
+ {{ .SuccessMessage }} +
+ {{ end }} {{template "form_item" .}} {{end}} \ No newline at end of file diff --git a/internal/server/template/blocks/form_input_array.html.tmpl b/internal/server/template/blocks/form_input_array.html.tmpl index 309ebd2..1be62d9 100644 --- a/internal/server/template/blocks/form_input_array.html.tmpl +++ b/internal/server/template/blocks/form_input_array.html.tmpl @@ -9,13 +9,16 @@ {{ $itemProperty := printf "%d" $index }} {{ $itemSchema := getItemSchema $root.Schema }} {{ $formItemData := formItemData $root $itemProperty $itemSchema }} - {{ template "form_row" $formItemData }} - - + + +
@@ -23,7 +26,11 @@ - + diff --git a/internal/server/template/blocks/form_input_boolean.html.tmpl b/internal/server/template/blocks/form_input_boolean.html.tmpl index 04e2540..b3924ef 100644 --- a/internal/server/template/blocks/form_input_boolean.html.tmpl +++ b/internal/server/template/blocks/form_input_boolean.html.tmpl @@ -1,12 +1,22 @@ {{define "form_input_boolean"}} {{ $fullProperty := getFullProperty .Parent .Property }} {{ $checked := getValue .Defaults .Values $fullProperty }} -