Skip to content

Commit

Permalink
Merge pull request #66 from PlakarKorp/jcastets/rework-ui-v2-build
Browse files Browse the repository at this point in the history
Jcastets/rework UI v2 build
  • Loading branch information
poolpOrg authored Nov 12, 2024
2 parents fd90e63 + 4824b66 commit 56f5dcf
Show file tree
Hide file tree
Showing 58 changed files with 826 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/.DS_Store
/plakar
/vendor
31 changes: 31 additions & 0 deletions ui/v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Plakar UI

Plakar UI is available at the private repository [plakar-ui](https://github.com/plakarkorp/plakar-ui). This repository only stores the compiled build version of the UI.

To synchronize the build assets with this repository, use the provided `sync.sh` script.

## Development

To sync the Plakar UI build from your local filesystem, use the `--local` flag with `sync.sh`:

```bash
$ cd ~/dev/plakar/plakar-ui
$ npm run build
$ ./sync.sh --local ~/dev/plakar/plakar-ui
$ cat ./frontend/VERSION
version: local
commit: 4e5613ea839beba564d57cdad97664bf24d9b582
date: 2024-11-12T14:41:26+0100
```

## Production build

For syncing the UI build from the latest Plakar UI release on GitHub, use the `--release` flag. Optionally, specify a release version if needed:

```bash
$ ./sync.sh --release
$ cat ./frontend/VERSION
version: 0.1.0
commit: 4e5613ea839beba564d57cdad97664bf24d9b582
date: 2024-11-12T14:41:26+0100
```
3 changes: 3 additions & 0 deletions ui/v2/frontend/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: local
commit: e1eb6c4d93f7764ce78528dba5677ed948ef0686
date: 2024-11-12T17:04:05+0100
668 changes: 668 additions & 0 deletions ui/v2/frontend/assets/index-BUmZTsSd.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ui/v2/frontend/assets/index-DEagmxS9.css

Large diffs are not rendered by default.

Binary file added ui/v2/frontend/assets/plakup-UPFDsr4d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ui/v2/frontend/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions ui/v2/frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Plakar UI</title>
<script type="module" crossorigin src="/assets/index-BUmZTsSd.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-DEagmxS9.css">
</head>
<body>
<div id="root"></div>
</body>
</html>
81 changes: 81 additions & 0 deletions ui/v2/sync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/sh

set -e

cd `dirname $0`

usage() {
echo "Usage: $0 --release [RELEASE_NUMBER] | --local PLAKAR_UI_PATH"
exit 1
}

if [ "$#" -lt 1 ]; then
usage
fi

# Initialize variables
mode=""
path=""
release_number="latest"

while [ "$#" -gt 0 ]; do
case "$1" in
--release)
if [ -n "$mode" ]; then
echo "Error: Only one of --release or --local can be specified."
usage
fi
mode="release"
if [ -n "$2" ] && [ "${2#-}" = "$2" ]; then
release_number="$2"
shift
fi
;;
--local)
if [ -n "$mode" ]; then
echo "Error: Only one of --release or --local can be specified."
usage
fi
mode="local"
if [ -z "$2" ] || [ "${2#-}" != "$2" ]; then
echo "Error: --local requires a path argument."
usage
fi
path="$2"
shift
;;
*)
echo "Error: Invalid argument '$1'"
usage
;;
esac
shift
done

if [ "$mode" = "release" ]; then
echo "Downloading release: $release_number"
# TODO

elif [ "$mode" = "local" ]; then
if [ ! -d "$path" ]; then
echo "Error: Path '$path' does not exist."
exit 1
fi

if [ ! -d "$path/dist" ]; then
echo "Error: before syncing, please build the UI using 'npm run build' in the UI directory."
exit 1
fi

rm -rf -- ./frontend/
cp -r ${path}/dist ./frontend

cat<<EOF > ./frontend/VERSION
version: local
commit: $(git -C ${path} log -1 --format="%H")
date: $(date +"%Y-%m-%dT%H:%M:%S%z")
EOF

else
usage
fi
63 changes: 26 additions & 37 deletions ui/v2/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"embed"
_ "embed"
"fmt"
"io/fs"
"math/rand/v2"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/PlakarKorp/plakar/api"
"github.com/PlakarKorp/plakar/repository"
Expand All @@ -35,51 +37,38 @@ import (
var content embed.FS

func Ui(repo *repository.Repository, addr string, spawn bool, cors bool) error {

r := api.NewRouter(repo)

r.PathPrefix("/static/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Strip the "/static/" prefix from the request path
httpPath := r.URL.Path
// Serve files from the ./frontend directory
r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Join internally call path.Clean to prevent directory traversal
path := filepath.Join("frontend", r.URL.Path)

// Read the file from the embedded content
data, err := content.ReadFile("frontend" + httpPath)
if err != nil {
http.Error(w, "File not found", http.StatusNotFound)
_, err := content.Open(path)
if os.IsNotExist(err) {
// File does not exist, serve index.html
index, err := content.ReadFile(filepath.Join("frontend", "index.html"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write(index)
return
} else if err != nil {
// If we got an error (that wasn't that the file doesn't exist) stating the
// file, return a 500 internal server error and stop
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Determine the content type based on the file extension
contentType := ""
switch {
case strings.HasSuffix(httpPath, ".css"):
contentType = "text/css"
case strings.HasSuffix(httpPath, ".js"):
contentType = "application/javascript"
case strings.HasSuffix(httpPath, ".png"):
contentType = "image/png"
case strings.HasSuffix(httpPath, ".jpg"), strings.HasSuffix(httpPath, ".jpeg"):
contentType = "image/jpeg"
// Add more content types as needed
}

// Set the content type header
if contentType != "" {
w.Header().Set("Content-Type", contentType)
}
w.WriteHeader(http.StatusOK)
w.Write(data)
})

r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, err := content.ReadFile("frontend/index.html")
statics, err := fs.Sub(content, "frontend")
if err != nil {
http.Error(w, "App not found", http.StatusNotFound)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
http.FileServer(http.FS(statics)).ServeHTTP(w, r)
})

var url string
Expand Down

0 comments on commit 56f5dcf

Please sign in to comment.