File tree Expand file tree Collapse file tree 10 files changed +48
-95
lines changed Expand file tree Collapse file tree 10 files changed +48
-95
lines changed Original file line number Diff line number Diff line change
1
+ go-web-app /assets /css /vendor
2
+ go-web-app /assets /js /vendor
Original file line number Diff line number Diff line change @@ -20,4 +20,4 @@ How to use:
20
20
- Open http://localhost:8383
21
21
22
22
[ go ] : https://golang.org/
23
- [ installtask ] : https://github.com/go-task/task# installation
23
+ [ installtask ] : https://taskfile.dev/ installation/
Original file line number Diff line number Diff line change
1
+ # https://taskfile.dev/
2
+
1
3
version : ' 3'
2
4
3
- vars :
4
- EXE : app{{exeExt}}
5
+ interval : 1s
5
6
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
9
10
10
- CSS_FILES :
11
- assets/css/vendor/reboot.css
12
- assets/css/main.css
11
+ EXE : app{{exeExt}}
13
12
14
13
tasks :
15
14
default :
16
- cmds :
17
- - task : run
18
-
19
- run :
20
15
desc : Build and run the web app
16
+ aliases : [run]
21
17
cmds :
22
18
- task : build
23
19
- ./{{.EXE}}
24
20
25
21
build :
26
22
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
33
23
deps : [js, css]
34
24
cmds :
35
- - fileb0x assets_box.yml
25
+ - go build -v -o {{.EXE}} ./app.go
36
26
sources :
37
- - public/**/*
27
+ - ' **/*.go'
28
+ - ' {{.JS_BUNDLE}}'
29
+ - ' {{.CSS_BUNDLE}}'
38
30
generates :
39
- - boxes/assets/ab0x.go
40
- - assets_box.yml
31
+ - ' {{.EXE}}'
41
32
42
33
js :
43
- desc : Bundle JS
34
+ desc : Bundle JS using esbuild
44
35
cmds :
45
- - minify -o public/app .js {{.JS_FILES }}
36
+ - esbuild --bundle --minify assets/js/main .js > {{.JS_BUNDLE }}
46
37
sources :
47
38
- assets/js/**/*.js
48
39
generates :
49
- - public/app.js
40
+ - ' {{.JS_BUNDLE}} '
50
41
51
42
css :
52
- desc : Bundle CSS
43
+ desc : Bundle CSS using esbuild
53
44
cmds :
54
- - minify -o public/app .css {{.CSS_FILES }}
45
+ - esbuild --bundle --minify assets/css/main .css > {{.CSS_BUNDLE }}
55
46
sources :
56
47
- assets/css/**/*.css
57
48
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}}'
77
50
78
- go-get :
51
+ clean :
52
+ desc : Clean every generated file
79
53
cmds :
80
- - go get -u {{.REPO}}
54
+ - rm -f {{.CSS_BUNDLE}}
55
+ - rm -f {{.JS_BUNDLE}}
56
+ - rm -f {{.EXE}}
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
+ "embed"
4
5
"fmt"
5
6
"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"
9
7
)
10
8
9
+ //go:embed public/*
10
+ var publicDir embed.FS
11
+
12
+ //go:embed templates/*
13
+ var templatesDir embed.FS
14
+
11
15
func main () {
12
- http .Handle ("/public/" , withoutCache (assets . Handler ))
16
+ http .Handle ("/public/" , withoutCache (http . FileServer ( http . FS ( publicDir )) ))
13
17
http .HandleFunc ("/" , indexPage )
14
18
15
19
fmt .Printf ("Running web app on :8383" )
@@ -19,7 +23,7 @@ func main() {
19
23
}
20
24
21
25
func indexPage (w http.ResponseWriter , r * http.Request ) {
22
- data , err := templates .ReadFile ("templates/index.html" )
26
+ data , err := templatesDir .ReadFile ("templates/index.html" )
23
27
if err != nil {
24
28
panic (err )
25
29
}
Original file line number Diff line number Diff line change
1
+ @import "./vendor/reboot.css" ;
2
+
1
3
html , body {
2
4
height : 100% ;
3
5
}
Original file line number Diff line number Diff line change 1
- ; ( function ( ) {
2
- 'use strict' ;
1
+ import $ from "./vendor/jquery.js" ;
3
2
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
+ ) ;
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments