Skip to content

Commit 7a43e1e

Browse files
authored
feat: implement base functionality (#1)
1 parent 13e500a commit 7a43e1e

38 files changed

+4695
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# tests
2+
coverage.out
3+
coverage.html

.trunk/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*out
2+
*logs
3+
*actions
4+
*notifications
5+
*tools
6+
plugins
7+
user_trunk.yaml
8+
user.yaml
9+
tmp

.trunk/configs/.markdownlint.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Prettier friendly markdownlint config (all formatting rules disabled)
2+
extends: markdownlint/style/prettier

.trunk/configs/.yamllint.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
rules:
2+
quoted-strings:
3+
required: only-when-needed
4+
extra-allowed: ["{|}"]
5+
key-duplicates: {}
6+
octal-values:
7+
forbid-implicit-octal: true

.trunk/configs/svgo.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
plugins: [
3+
{
4+
name: "preset-default",
5+
params: {
6+
overrides: {
7+
removeViewBox: false, // https://github.com/svg/svgo/issues/1128
8+
sortAttrs: true,
9+
removeOffCanvasPaths: true,
10+
},
11+
},
12+
},
13+
],
14+
};

.trunk/trunk.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This file controls the behavior of Trunk: https://docs.trunk.io/cli
2+
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
3+
version: 0.1
4+
cli:
5+
version: 1.22.2
6+
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
7+
plugins:
8+
sources:
9+
- id: trunk
10+
ref: v1.6.1
11+
uri: https://github.com/trunk-io/plugins
12+
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
13+
runtimes:
14+
enabled:
15+
- go@1.21.0
16+
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
17+
lint:
18+
ignore:
19+
- linters: [ALL]
20+
paths:
21+
- examples/**
22+
enabled:
23+
- checkov@3.2.219
24+
- git-diff-check
25+
- gofmt@1.20.4
26+
- golangci-lint@1.59.1
27+
- markdownlint@0.41.0
28+
- osv-scanner@1.8.3
29+
- prettier@3.3.3
30+
- svgo@3.3.2
31+
- trivy@0.54.1
32+
- trufflehog@3.81.7
33+
- yamllint@1.35.1
34+
actions:
35+
disabled:
36+
- trunk-announce
37+
- trunk-check-pre-push
38+
- trunk-fmt-pre-commit
39+
enabled:
40+
- trunk-upgrade-available

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${fileDirname}"
13+
}
14+
]
15+
}

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
# Go Vite Env
1+
# Go SPA Serve
2+
3+
Go SPA Serve is a simple package that serves static files specifically focused on serving single page applications (SPA).
4+
Generally, this can be used to instead of the stdlib `http.FileServer`, but the main use case is to serve a SPA with a
5+
single entry point (e.g. `index.html`) and let the client-side router handle the rest.
6+
7+
In addition to serving static files, this package also provides a way to load environment variables from a Go struct at
8+
runtime by injecting them into the head tag of the served HTML file.
9+
10+
## Problem
211

312
Vite is a fantastic build tool, but it doesn't support loading environment variables at runtime.
413
This becomes quite a problem when you build a single image for multiple environments or need to
514
build images for on-premises deployments where you can't bake in the environment variables.
615

7-
This package provides a way to load environment variables from a Go struct at runtime.
16+
## Code Coverage
17+
18+
[![codecov](https://codecov.io/gh/jrschumacher/go-spaserve/graph/badge.svg?token=W99WAK10IX)](https://codecov.io/gh/jrschumacher/go-spaserve)

codecov.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
# basic
6+
target: auto #default
7+
threshold: 5
8+
base: auto

copyFilesys.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package spaserve
2+
3+
import (
4+
"errors"
5+
"io"
6+
"io/fs"
7+
8+
"github.com/psanford/memfs"
9+
)
10+
11+
// OnHookFunc is a function that can be used to modify the data of a file before it is written to the memfs.
12+
// The function should return the modified data and an error if one occurred.
13+
type OnHookFunc func(path string, data []byte) ([]byte, error)
14+
15+
func CopyFileSys(filesys fs.FS, onHook OnHookFunc) (*memfs.FS, error) {
16+
mfs := memfs.New()
17+
err := fs.WalkDir(filesys, ".", func(path string, d fs.DirEntry, err error) error {
18+
if err != nil {
19+
return errors.Join(ErrUnexpectedWalkError, err)
20+
}
21+
22+
// create dir and continue
23+
if d.IsDir() {
24+
if err := mfs.MkdirAll(path, 0o755); err != nil {
25+
return errors.Join(ErrCouldNotMakeDir, err)
26+
}
27+
return nil
28+
}
29+
30+
// open file
31+
f, err := filesys.Open(path)
32+
if err != nil {
33+
return errors.Join(ErrCouldNotOpenFile, err)
34+
}
35+
defer f.Close()
36+
37+
// read file
38+
data, err := io.ReadAll(f)
39+
if err != nil {
40+
return errors.Join(ErrCouldNotReadFile, err)
41+
}
42+
43+
// run onHook
44+
if onHook != nil {
45+
data, err = onHook(path, data)
46+
if err != nil {
47+
return err
48+
}
49+
}
50+
51+
// write file to memfs
52+
if err := mfs.WriteFile(path, data, fs.ModeAppend); err != nil {
53+
return errors.Join(ErrCouldNotWriteFile, err)
54+
}
55+
56+
return nil
57+
})
58+
59+
return mfs, err
60+
}

0 commit comments

Comments
 (0)