Skip to content

Commit

Permalink
Add caddy to ci
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Sep 28, 2024
1 parent 7d6851a commit a775d5f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 40 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Config struct {
// The path to the templates directory. Default `templates`.
TemplatesDir string `json:"templates_dir,omitempty" arg:"-t,--template-dir" default:"templates"`

// The FS to load templates from. Overrides Path if not nil.
// The FS to load templates from. Overrides TemplatesDir if not nil.
TemplatesFS fs.FS `json:"-" arg:"-"`

// File extension to search for to find template files. Default `.html`.
Expand Down
108 changes: 69 additions & 39 deletions make_tool.cue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"tool/exec"
"tool/file"
"tool/os"

// "encoding/json"
// "tool/cli"
)

#vars: {
Expand Down Expand Up @@ -69,32 +66,26 @@ task: build: {
task: run: {
vars: #vars

gobuild: task.build & {"vars": vars, outfile: "\(vars.testdir)/xtemplate"}
rmdataw: file.RemoveAll & {path: "\(vars.testdir)/dataw"}
mkdataw: file.Mkdir & {path: "\(vars.testdir)/dataw", $after: rmdataw.$done}
mklog: file.Create & {filename: "\(vars.testdir)/xtemplate.log", contents: ""}

start: exec.Run & {
$after: mkdataw.$done && mklog.$done && gobuild.gobuild.$done
cmd: ["bash", "-c", "./xtemplate --loglevel -4 -d DB:sql:sqlite3:file:./dataw/test.sqlite -d FS:fs:./data --config-file config.json >\(mklog.filename) 2>&1"]
dir: vars.testdir
}

ready: exec.Run & {
$after: mklog.$done
cmd: ["bash", "-c", "grep -q 'starting server' <(tail -f \(mklog.filename))"]
cmd: "./xtemplate --loglevel -4 -d DB:sql:sqlite3:file:./dataw/test.sqlite -d FS:fs:./data --config-file config.json"
dir: vars.testdir
$after: mkdataw.$done
}
}

task: test: {
vars: #vars

port: int | *8080

list: file.Glob & {glob: "\(vars.testdir)/tests/*.hurl"}
ready: exec.Run & {cmd: "curl -X GET --retry 5 --retry-connrefused --retry-delay 1 http://localhost:\(port)"}
hurl: exec.Run & {
cmd: ["hurl", "--continue-on-error", "--test", "--report-html", "report", "--connect-to", "localhost:8080:localhost:\(port)"] + list.files
dir: vars.testdir
dir: vars.testdir
after: ready.$done
}
}

Expand All @@ -107,6 +98,15 @@ task: gotest: {
}
}

task: build_test: {
vars: #vars

build: task.build & {"vars": vars, outfile: "\(vars.testdir)/xtemplate"}
run: task.run & {"vars": vars, start: $after: build.gobuild.$done}
test: task.test & {"vars": vars, ready: $after: run.start}
kill: exec.Run & {cmd: "pkill xtemplate", $after: test.hurl}
}

task: dist: {
vars: #vars

Expand All @@ -133,6 +133,20 @@ task: dist: {
}
}

task: build_docker: {
vars: #vars

tags: [
"infogulch/xtemplate:\(vars.version)",
if vars.version == vars.latest {"infogulch/xtemplate:latest"},
]

build: exec.Run & {
cmd: ["docker", "build"] + list.FlattenN([for t in tags {["-t", t]}], 1) + ["--build-arg", "LDFLAGS=\(vars.ldflags)", "."]
dir: vars.rootdir
}
}

task: test_docker: {
vars: #vars

Expand All @@ -141,60 +155,76 @@ task: test_docker: {
dir: vars.rootdir
}
run: exec.Run & {
cmd: "docker run -d --rm --name xtemplate-test -p 8081:80 xtemplate-test"
cmd: ["docker", "run", "-d", "--rm", "--name", "xtemplate-test", "-p", "8081:80", "xtemplate-test"]
$after: build.$done
}
ready: exec.Run & {
cmd: ["bash", "-c", "grep -q 'starting server' <(docker logs -f xtemplate-test)"]
$after: run.$done
}
test: task.test & {"vars": vars, port: 8081, hurl: $after: ready.$done}
test: task.test & {"vars": vars, port: 8081}
stop: exec.Run & {cmd: "docker stop xtemplate-test", $after: test.hurl.$done} // be nice if we can always run this even if previous steps fail
}

task: build_docker: {
task: build_caddy: {
vars: #vars

tags: [
"infogulch/xtemplate:\(vars.version)",
if vars.version == vars.latest {"infogulch/xtemplate:latest"},
]
osenv: os.Environ

build: exec.Run & {
cmd: ["docker", "build"] + list.FlattenN([for t in tags {["-t", t]}], 1) + ["--build-arg", "LDFLAGS=\(vars.ldflags)", "."]
cmd: ["xcaddy", "build",
"--with", "github.com/infogulch/xtemplate-caddy",
"--with", "github.com/infogulch/xtemplate=.",
"--with", "github.com/infogulch/xtemplate/providers=./providers",
"--with", "github.com/infogulch/xtemplate/providers/nats=./providers/nats",
"--with", "github.com/mattn/go-sqlite3",
"--output", "./caddy"]
dir: vars.rootdir
env: osenv & {CGO_ENABLED: "1"}
}
}

command: {
for k, t in task {
(k): {cfg: meta, vars: cfg.vars, t}
task: run_caddy: {
vars: #vars

rmdataw: file.RemoveAll & {path: "\(vars.testdir)/dataw"}
mkdataw: file.Mkdir & {path: "\(vars.testdir)/dataw", $after: rmdataw.$done}

start: exec.Run & {
cmd: ["../caddy", "start", "--config", "caddy.json"]
dir: vars.testdir
$after: mkdataw.$done
}
}

command: run_test: {
cfg: meta
task: build_test_caddy: {
vars: #vars

build: task.build_caddy & {"vars": vars}
run_caddy: task.test_caddy & {"vars": vars, start: $after: build.build.$done}
test_caddy: task.test & {"vars": vars, port: 8082, ready: $after: run_caddy.start.$done}
kill_caddy: exec.Run & {cmd: "pkill caddy", $after: test_caddy.hurl.$done} // is there a better way?

$done: kill_caddy.$done
}

run: task.run & {"vars": cfg.vars, start: mustSucceed: false}
test: task.test & {"vars": cfg.vars, hurl: $after: run.ready.$done}
kill: exec.Run & {cmd: "pkill xtemplate", $after: test.hurl.$done}
command: {
for k, t in task {
(k): {cfg: meta, vars: cfg.vars, t}
}
}

command: ci: {
cfg: meta

gotest: task.gotest & {"vars": cfg.vars}

run: task.run & {"vars": cfg.vars, start: mustSucceed: false} // it's killed so it will never succeed
test: task.test & {"vars": cfg.vars, hurl: $after: run.ready.$done}
kill: exec.Run & {cmd: "pkill xtemplate", $after: test.hurl.$done} // is there a better way?
build_test: task.build_test & {"vars": cfg.vars}
build_test_caddy: task.build_test_caddy & {"vars": cfg.vars, run_caddy: start: $after: build_test.kill}

dist: task.dist & {"vars": cfg.vars, rmdist: $after: kill.$done}
dist: task.dist & {"vars": cfg.vars, rmdist: $after: build_test.$done}

test_docker: task.test_docker & {"vars": cfg.vars}
build_docker: task.build_docker & {"vars": cfg.vars, build: $after: test_docker.stop.$done}
push: exec.Run & {
cmd: ["docker", "push"] + build_docker.tags
$after: build_docker.build.$done
}

}
59 changes: 59 additions & 0 deletions test/caddy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"apps": {
"http": {
"servers": {
"": {
"listen": [
":8082"
],
"routes": [
{
"handle": [
{
"handler": "xtemplate",
"minify": true,
"dot": [
{
"type": "sql",
"name": "DB",
"driver": "sqlite3",
"connstr": "file:./dataw/test.sqlite"
},
{
"type": "fs",
"name": "FS",
"path": "./data"
},
{
"type": "fs",
"name": "FSW",
"path": "./dataw"
},
{
"type": "fs",
"name": "Migrations",
"path": "./migrations"
},
{
"type": "kv",
"name": "KV",
"values": {
"a": "1",
"b": "2",
"hello": "world"
}
},
{
"type": "nats",
"name": "Nats"
}
]
}
]
}
]
}
}
}
}
}
1 change: 1 addition & 0 deletions test/templates/ready.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OK

0 comments on commit a775d5f

Please sign in to comment.