Add support for specifying root driectory for zs serve

This commit is contained in:
James Mills 2023-03-12 14:24:34 +10:00
parent 339de0457c
commit d980980a0b
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
2 changed files with 12 additions and 6 deletions

View File

@ -47,6 +47,8 @@ RUN addgroup -g "${PGID}" zs && \
adduser -D -H -G zs -h /var/empty -u "${PUID}" zs && \ adduser -D -H -G zs -h /var/empty -u "${PUID}" zs && \
mkdir -p /data && chown -R zs:zs /data mkdir -p /data && chown -R zs:zs /data
EXPOSE 8000/tcp
VOLUME /data VOLUME /data
WORKDIR / WORKDIR /
@ -57,4 +59,4 @@ COPY .dockerfiles/entrypoint.sh /init
ENTRYPOINT ["/init"] ENTRYPOINT ["/init"]
CMD ["zs""] CMD ["zs"", "serve", "0.0.0.0:8000", "/data"]

14
main.go
View File

@ -329,13 +329,13 @@ func buildAll(ctx context.Context, watch bool) {
} }
// serve runs a static web server and builds and continuously watches for changes to rebuild // serve runs a static web server and builds and continuously watches for changes to rebuild
func serve(ctx context.Context, bind string) error { func serve(ctx context.Context, bind, root string) error {
os.Mkdir(PUBDIR, 0755) os.Mkdir(root, 0755)
svr, err := static.NewServer( svr, err := static.NewServer(
static.WithBind(bind), static.WithBind(bind),
static.WithDir(true), static.WithDir(true),
static.WithRoot(PUBDIR), static.WithRoot(root),
static.WithSPA(true), static.WithSPA(true),
) )
if err != nil { if err != nil {
@ -386,10 +386,14 @@ func main() {
buildAll(ctx, true) buildAll(ctx, true)
case "serve": case "serve":
bind := ":8000" bind := ":8000"
if len(args) > 1 { root := PUBDIR
if len(args) > 0 {
bind = args[0] bind = args[0]
} }
if err := serve(ctx, bind); err != nil { if len(args) > 1 {
root = args[1]
}
if err := serve(ctx, bind, root); err != nil {
fmt.Println("ERROR: " + err.Error()) fmt.Println("ERROR: " + err.Error())
} }
case "var": case "var":