Skip to content

Commit 25c58be

Browse files
committed
Improve Go Web App example
Modernize it according to more recent good practiced. - Replaced Minify with esbuild - Replaced fileb0x with embed
1 parent 94ec824 commit 25c58be

File tree

10 files changed

+48
-95
lines changed

10 files changed

+48
-95
lines changed

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go-web-app/assets/css/vendor
2+
go-web-app/assets/js/vendor

go-web-app/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ How to use:
2020
- Open http://localhost:8383
2121

2222
[go]: https://golang.org/
23-
[installtask]: https://github.com/go-task/task#installation
23+
[installtask]: https://taskfile.dev/installation/

go-web-app/Taskfile.yml

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,56 @@
1+
# https://taskfile.dev/
2+
13
version: '3'
24

3-
vars:
4-
EXE: app{{exeExt}}
5+
interval: 1s
56

6-
JS_FILES:
7-
assets/js/vendor/jquery.js
8-
assets/js/main.js
7+
vars:
8+
JS_BUNDLE: public/app.js
9+
CSS_BUNDLE: public/app.css
910

10-
CSS_FILES:
11-
assets/css/vendor/reboot.css
12-
assets/css/main.css
11+
EXE: app{{exeExt}}
1312

1413
tasks:
1514
default:
16-
cmds:
17-
- task: run
18-
19-
run:
2015
desc: Build and run the web app
16+
aliases: [run]
2117
cmds:
2218
- task: build
2319
- ./{{.EXE}}
2420

2521
build:
2622
desc: Build the web app
27-
deps: [assets, templates]
28-
cmds:
29-
- go build -v -o {{.EXE}} ./app.go
30-
31-
assets:
32-
desc: Bundle assets and generate box for it
3323
deps: [js, css]
3424
cmds:
35-
- fileb0x assets_box.yml
25+
- go build -v -o {{.EXE}} ./app.go
3626
sources:
37-
- public/**/*
27+
- '**/*.go'
28+
- '{{.JS_BUNDLE}}'
29+
- '{{.CSS_BUNDLE}}'
3830
generates:
39-
- boxes/assets/ab0x.go
40-
- assets_box.yml
31+
- '{{.EXE}}'
4132

4233
js:
43-
desc: Bundle JS
34+
desc: Bundle JS using esbuild
4435
cmds:
45-
- minify -o public/app.js {{.JS_FILES}}
36+
- esbuild --bundle --minify assets/js/main.js > {{.JS_BUNDLE}}
4637
sources:
4738
- assets/js/**/*.js
4839
generates:
49-
- public/app.js
40+
- '{{.JS_BUNDLE}}'
5041

5142
css:
52-
desc: Bundle CSS
43+
desc: Bundle CSS using esbuild
5344
cmds:
54-
- minify -o public/app.css {{.CSS_FILES}}
45+
- esbuild --bundle --minify assets/css/main.css > {{.CSS_BUNDLE}}
5546
sources:
5647
- assets/css/**/*.css
5748
generates:
58-
- public/app.css
59-
60-
templates:
61-
desc: Generate box for HTML templates
62-
cmds:
63-
- fileb0x templates_box.yml
64-
sources:
65-
- templates/**/*
66-
- templates_box.yml
67-
generates:
68-
- boxes/templates/ab0x.go
69-
70-
dl-deps:
71-
desc: Install tools required to build this app
72-
cmds:
73-
- task: go-get
74-
vars: {REPO: github.com/tdewolff/minify/cmd/minify}
75-
- task: go-get
76-
vars: {REPO: github.com/UnnoTed/fileb0x}
49+
- '{{.CSS_BUNDLE}}'
7750

78-
go-get:
51+
clean:
52+
desc: Clean every generated file
7953
cmds:
80-
- go get -u {{.REPO}}
54+
- rm -f {{.CSS_BUNDLE}}
55+
- rm -f {{.JS_BUNDLE}}
56+
- rm -f {{.EXE}}

go-web-app/app.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package main
22

33
import (
4+
"embed"
45
"fmt"
56
"net/http"
6-
7-
"github.com/go-task/examples/go-web-app/boxes/assets"
8-
"github.com/go-task/examples/go-web-app/boxes/templates"
97
)
108

9+
//go:embed public/*
10+
var publicDir embed.FS
11+
12+
//go:embed templates/*
13+
var templatesDir embed.FS
14+
1115
func main() {
12-
http.Handle("/public/", withoutCache(assets.Handler))
16+
http.Handle("/public/", withoutCache(http.FileServer(http.FS(publicDir))))
1317
http.HandleFunc("/", indexPage)
1418

1519
fmt.Printf("Running web app on :8383")
@@ -19,7 +23,7 @@ func main() {
1923
}
2024

2125
func indexPage(w http.ResponseWriter, r *http.Request) {
22-
data, err := templates.ReadFile("templates/index.html")
26+
data, err := templatesDir.ReadFile("templates/index.html")
2327
if err != nil {
2428
panic(err)
2529
}

go-web-app/assets/css/main.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import "./vendor/reboot.css";
2+
13
html, body {
24
height: 100%;
35
}

go-web-app/assets/js/main.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
;(function() {
2-
'use strict';
1+
import $ from "./vendor/jquery.js";
32

4-
$('#app').html(
5-
'<h1>Go Web App Example</h1>' +
6-
'<p>This app was compiled using Task.</p>' +
7-
'<p>' +
8-
'It uses Minify to bundle JS and CSS assets, and Fileb0x to keep ' +
9-
'everything (assets and templates) inside a single binary.' +
10-
'</p>'
11-
);
12-
})();
3+
$("#app").html(
4+
"<h1>Go Web App Example</h1>" +
5+
"<p>This app was compiled using Task.</p>" +
6+
"<p>" +
7+
`It uses <a target="_blank" href="https://esbuild.github.io/">esbuild</a> to bundle JS and CSS assets, ` +
8+
`and <a target="_blank" href="https://pkg.go.dev/embed">Go embeb</a> to keep ` +
9+
"everything (assets and templates) inside a single binary." +
10+
"</p>"
11+
);

go-web-app/assets_box.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

go-web-app/boxes/assets/.keep

Whitespace-only changes.

go-web-app/boxes/templates/.keep

Whitespace-only changes.

go-web-app/templates_box.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)