Skip to content

Commit

Permalink
Better build/watch
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli committed Apr 6, 2023
1 parent 9fadfe7 commit b606b67
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ gist.db
.DS_Store
/**/.DS_Store
public/assets/*
public/manifest.json
public/manifest.json
opengist
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ gist.db
.DS_Store
/**/.DS_Store
public/assets/*
public/manifest.json
public/manifest.json
opengist
27 changes: 20 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.PHONY: all install_deps build_frontend build_backend build_docker clean clean_docker
.PHONY: all install build_frontend build_backend build build_docker watch_frontend watch_backend watch clean clean_docker

# Specify the name of your Go binary output
BINARY_NAME := opengist

all: install_deps build_frontend build_backend
all: install build

install_deps:
install:
@echo "Installing NPM dependencies..."
@npm ci || (echo "Error: Failed to install NPM dependencies." && exit 1)
@echo "Installing Go dependencies..."
Expand All @@ -17,17 +17,30 @@ build_frontend:

build_backend:
@echo "Building Opengist binary..."
go build -o $(BINARY_NAME) opengist.go
go build -tags fs_embed -o $(BINARY_NAME) .

build: build_frontend build_backend

build_docker:
@echo "Building Docker image..."
docker build -t opengist .
docker build -t $(BINARY_NAME):latest .

watch_frontend:
@echo "Building frontend assets..."
./node_modules/.bin/vite dev --port 16157

watch_backend:
@echo "Building Opengist binary..."
DEV=1 ./node_modules/.bin/nodemon --watch '**/*' -e html,yml,go,js --signal SIGTERM --exec 'go' run .

watch:
@bash ./watch.sh

clean:
@echo "Cleaning up build artifacts..."
@rm -f $(BINARY_NAME) public/manifest.json
@rm -rf node_modules public/assets
@rm -rf public/assets

clean_docker:
@echo "Cleaning up Docker image..."
@docker rmi opengist
@docker rmi $(BINARY_NAME)
8 changes: 8 additions & 0 deletions fs_embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build fs_embed

package main

import "embed"

//go:embed templates/*/*.html public/manifest.json public/assets/*.js public/assets/*.css public/assets/*.svg
var dirFS embed.FS
7 changes: 7 additions & 0 deletions fs_os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !fs_embed

package main

import "os"

var dirFS = os.DirFS(".")
14 changes: 8 additions & 6 deletions internal/web/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package web
import (
"context"
"crypto/md5"
"embed"
"encoding/json"
"fmt"
"github.com/gorilla/sessions"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/rs/zerolog/log"
"html/template"
"io"
"io/fs"
"net/http"
"opengist/internal/config"
"opengist/internal/git"
Expand All @@ -24,7 +24,7 @@ import (
"time"
)

var devAssets = os.Getenv("DEV_ASSETS") == "1"
var dev = os.Getenv("DEV") == "1"
var store *sessions.CookieStore
var re = regexp.MustCompile("[^a-z0-9]+")
var fm = template.FuncMap{
Expand Down Expand Up @@ -77,14 +77,14 @@ var fm = template.FuncMap{
return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(email)))))
},
"asset": func(jsfile string) string {
if devAssets {
if dev {
return "https://localhost:16157/" + jsfile
}
return "/" + manifestEntries[jsfile].File
},
}

var EmbedFS embed.FS
var EmbedFS fs.FS

type Template struct {
templates *template.Template
Expand Down Expand Up @@ -142,8 +142,10 @@ func Start() {

e.Validator = NewValidator()

parseManifestEntries()
e.GET("/assets/*", cacheControl(echo.WrapHandler(http.StripPrefix("/assets", http.FileServer(http.FS(assetsFS))))))
if !dev {
parseManifestEntries()
e.GET("/assets/*", cacheControl(echo.WrapHandler(http.StripPrefix("/assets", http.FileServer(http.FS(assetsFS))))))
}

// Web based routes
g1 := e.Group("")
Expand Down
6 changes: 1 addition & 5 deletions opengist.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"embed"
"flag"
"fmt"
"github.com/rs/zerolog/log"
Expand All @@ -14,9 +13,6 @@ import (
"path/filepath"
)

//go:embed templates/*/*.html public/manifest.json public/assets/*.js public/assets/*.css public/assets/*.svg
var embedFS embed.FS

func initialize() {
configPath := flag.String("config", "config.yml", "Path to a config file in YML format")
flag.Parse()
Expand Down Expand Up @@ -61,7 +57,7 @@ func initialize() {
log.Fatal().Err(err).Msg("Failed to initialize database")
}

web.EmbedFS = embedFS
web.EmbedFS = dirFS
}

func main() {
Expand Down
Loading

0 comments on commit b606b67

Please sign in to comment.