Skip to content

Commit

Permalink
feat: added go-generate and ui.Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
spy16 committed Jan 4, 2023
1 parent a045219 commit b004483
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NAME:="go-svelte"
DIST_DIR:="dist"

all: clean fe tidy be
all: clean tidy be

clean:
@echo "Cleaning up old build..."
Expand All @@ -12,10 +12,8 @@ tidy:
@gofumpt -l -w .
@go mod tidy -v

fe:
@echo "Building frontend..."
@cd ui && yarn && yarn build

be:
@echo "Running go generate..."
@go generate ./...
@echo "Building backend..."
@go build -o $(DIST_DIR)/$(NAME) ./
3 changes: 1 addition & 2 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -44,7 +43,7 @@ func cmdServe() *cobra.Command {
cfg := loadConf(cmd)

router := chi.NewRouter()
router.Mount("/", http.FileServer(http.FS(ui.DistFS)))
router.Mount("/", ui.Handler())
router.Mount("/api", api.Router(cfg.API))

log.Infof(cmd.Context(), "listening at '%s'...", cfg.Addr)
Expand Down
13 changes: 10 additions & 3 deletions ui/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package ui
import (
"embed"
"io/fs"
"net/http"
)

var DistFS = mustSubFS(distFS, "dist")

//go:embed dist/**
//go:generate yarn
//go:generate yarn build
//go:embed all:dist/*
var distFS embed.FS

// Handler returns a static file serving handler.
func Handler() http.Handler {
fileSys := http.FS(mustSubFS(distFS, "dist"))
return http.FileServer(fileSys)
}

func mustSubFS(efs embed.FS, prefix string) fs.FS {
f, err := fs.Sub(efs, prefix)
if err != nil {
Expand Down

0 comments on commit b004483

Please sign in to comment.