Skip to content

Commit

Permalink
chore: merge pull request #175
Browse files Browse the repository at this point in the history
  • Loading branch information
ntorga authored Oct 4, 2024
2 parents 3ca39c3 + 16a4dd2 commit 49edee4
Show file tree
Hide file tree
Showing 24 changed files with 529 additions and 415 deletions.
10 changes: 8 additions & 2 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ tmp_dir = "tmp"

[build]
bin = "bin/os"
cmd = "CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/os ./os.go"
cmd = "make build"
delay = 1000
exclude_dir = [".git", "bin", "logs", "tmp", "vendor", "src/devUtils"]
exclude_regex = [".*_templ.go"]
follow_symlink = false
ignore = ["*_test.go", "testHelpers.go"]
include_ext = ["go", "tpl", "tmpl", "templ", "html"]
include_ext = ["go", "tpl", "tmpl", "templ", "html", "js", "css"]

[log]
main_only = true

[screen]
clear_on_rebuild = true
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"grpcs",
"gsub",
"Hostnames",
"htmx",
"installables",
"Installables",
"ioncube",
Expand Down Expand Up @@ -83,5 +84,6 @@
"webserver",
"zerolog",
"Zerolog"
]
],
"editor.formatOnSave": true
}
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Changelog

```log
0.0.9 - 2024/XX/XX
0.0.9 - 2024/10/04
refactor(front): runtime page with HTMX+Alpine.js
refactor(front): ssls page with HTMX+Alpine.js
feat: chown default dirs after service install/add
feat: add jsonAjax helper
feat: add dev-build.sh script and make file
fix: adjust mapping layout for lower resolutions
fix: bug on match pattern on mappings
fix: bug on error level and error log php update
0.0.8 - 2024/09/23
refactor(front): databases page with HTMX+Alpine.js
Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SHELL=/bin/bash
UI_DIR=src/presentation/ui

dev:
air serve

build:
templ generate -path $(UI_DIR)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/os ./os.go
if podman ps | grep -q "os"; then podman exec os /bin/bash -c "supervisorctl restart os-api"; fi

run:
/var/infinite/os serve
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ When running in production, the `/speedia/.env` file is only used if the environ
Speedia OS commands can harm your system, so it's important to run the unit tests in a proper container:

```
podman build -t sos-unit-test:latest -f Containerfile.test .
podman run --rm -it sos-unit-test:latest
podman build -t os-unit-test:latest -f Containerfile.test .
podman run --rm -it os-unit-test:latest
```

Make sure you have the `.env` file in the root of the git directory before running the tests.
Expand All @@ -87,20 +87,45 @@ For instance there you'll find a `testHelpers.go` file that is used to read the

### Building

#### Simple Build

To build the project, run the command below. It takes two minutes to build the project at first. After that, it takes less than 10 seconds to build.

```
podman build -t sos:latest .
podman build -t os:latest .
```

To run the project you may use the following command:

```
podman run --name sos --env 'PRIMARY_VHOST=speedia.net' --rm -p 1618:1618 -it sos:latest
podman run --name os --env 'PRIMARY_VHOST=speedia.net' --rm -p 1618:1618 -it os:latest
```

When testing, consider publishing port 80 and 443 to the host so that you don't need to use a reverse proxy. You should also consider using `--env 'LOG_LEVEL=debug'` to increase the log verbosity.

#### Development Build

When developing the project, you may want to use a script to automate the build process. The `dev-build.sh` script is available in the root of the project and it will take care of all the steps needed to build and run the container.

To run the script you can simply use `bash dev-build.sh` (bash may be replaced by zsh or similar). By default, the script will expose the port 1618 to the host which is used by the API and the dashboard.

- If you pass the `http` argument, it will also expose the ports 80 and 443 to the host;
- If you pass the `ols` argument, it will expose port 7080 (used by OpenLiteSpeed admin);
- If you pass the `no-cache` argument, it will remove the image cache and rebuild the image from scratch;

The script will also create a `dev` account with the password `123456` so you can access the dashboard.

When you need to stop the container, just CTRL+C to stop and remove it. If you don't want to remove it, just ditch the `--rm` flag from the `podman run` command in the script.

If you look closely at the script, you'll see that it mounts the project's `bin` directory to the container `/speedia/bin` path. This is done to allow the container to access the binary file generated by Air on the host. The script then replace the binary file that comes with the container with the one on the `/bin` directory.

With this approach you don't need to rebuild the container every time you change the code. Although sometimes you may want to restart the container to apply some changes, specially when changing the dependencies or system configurations. In this case, just hit CTRL+C to stop the container and run the script again.

**Notes:**

1. You must run the script from the project's root directory;
2. Until Echo v4.13.0 is released, you'll need to refresh the browser page during development to see the changes in the dashboard as we're not able to use the `DEV_MODE` auto refresh websocket trick for now. To understand how this trick used to work, check the UI router and main layout files.

### Web UIs

This project has two web UIs, the previous Vue.js frontend and the new [Templ](https://templ.guide/) + [Alpine.js](https://alpinejs.dev/) + [HTMX](https://htmx.org/docs/) frontend. The Vue.js frontend is deprecated and will be removed in the future. It's available at `/_/` and the [Templ](https://templ.guide/) + [Alpine.js](https://alpinejs.dev/) + [HTMX](https://htmx.org/docs/) frontend is available at `/`.
Expand All @@ -113,9 +138,9 @@ For the interface code to be read and rendered by Go, we need to convert all `.t
templ generate -path src/presentation/api
```

With this, Go will be able to provide the entire application interface at the previously indicated route (`/`).
It is important that this is done before using Air to create the binary; otherwise, the Web UI will not be embedded, and you will not be able to use it.

**NOTE:** It is important that this is done before using Air to create the binary; otherwise, the Web UI will not be embedded, and you will not be able to use it.
**NOTE:** If you are using the `dev-build.sh` script, you don't need to run the `templ generate` command (or Air for that matter) since the script will take care of everything for you.

### VSCode Extensions

Expand Down
66 changes: 66 additions & 0 deletions dev-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

# Define the execution arguments.
ports=(-p 1618:1618)
case ${1} in
http)
sudo sysctl net.ipv4.ip_unprivileged_port_start=80
ports+=(-p 80:80 -p 443:443)
;;
ols)
ports+=(-p 7080:7080)
;;
no-cache)
podman image prune -a
podman rmi localhost/os -f
;;
esac

echo "=> Building the container..."
make build
podman build -t os:latest .
# TODO: Re-add --env 'DEV_MODE=true' after Echo v4.13.0 release.
podman run --name os -d \
--env 'LOG_LEVEL=debug' --env 'PRIMARY_VHOST=speedia.cloud' \
--hostname=speedia.cloud --cpus=2 --memory=2g --rm \
--volume "$(pwd)/bin:/speedia/bin:Z,ro,bind,slave" \
"${ports[@]}" -it os:latest

echo "=> Waiting for the container to start..."
sleep 5

echo "=> Replacing the standard binary with the development binary..."
podman exec os /bin/bash -c 'rm -f os && ln -s bin/os os && supervisorctl restart os-api'

echo "=> Creating a development account..."
podman exec os /bin/bash -c 'os account create -u dev -p 123456'

echo
echo "<<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>"
echo
echo "=> Starting the development build..."
echo "Any changes to the code will trigger a rebuild automatically."
echo "Please, ignore the 'Only root can run SOS' message."
echo
echo "<<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>"
echo
sleep 3

stopDevBuild() {
kill $airPid
kill $podmanPid
echo
echo "=> Development build stopped."
echo
exit
}

trap stopDevBuild SIGINT

air &
airPid=$!
podman attach os &
podmanPid=$!

wait $airPid
wait $podmanPid
46 changes: 22 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
module github.com/speedianet/os

go 1.22
go 1.23

require (
github.com/alecthomas/chroma v0.10.0
github.com/alessio/shellescape v1.4.2
github.com/dgraph-io/badger/v4 v4.2.0
github.com/dgraph-io/badger/v4 v4.3.0
github.com/glebarez/sqlite v1.11.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.12.0
github.com/rs/zerolog v1.33.0
github.com/samber/slog-zerolog/v2 v2.6.0
github.com/samber/slog-zerolog/v2 v2.7.0
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cobra v1.8.1
github.com/swaggo/echo-swagger v1.4.1
github.com/swaggo/swag v1.16.3
golang.org/x/crypto v0.25.0
golang.org/x/exp v0.0.0-20240707233637-46b078467d37
golang.org/x/net v0.27.0
golang.org/x/term v0.22.0
golang.org/x/crypto v0.28.0
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6
golang.org/x/net v0.30.0
golang.org/x/term v0.25.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/gorm v1.25.10
gorm.io/gorm v1.25.12
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/a-h/templ v0.2.747
github.com/a-h/templ v0.2.778
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/dgraph-io/ristretto v1.0.0 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/glebarez/go-sqlite v1.22.0 // indirect
Expand All @@ -40,41 +40,39 @@ require (
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v24.3.25+incompatible // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/samber/lo v1.44.0 // indirect
github.com/samber/slog-common v0.17.0 // indirect
github.com/samber/lo v1.47.0 // indirect
github.com/samber/slog-common v0.17.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/swaggo/files/v2 v2.0.0 // indirect
github.com/swaggo/files/v2 v2.0.1 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/tklauser/numcpus v0.9.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0
golang.org/x/time v0.7.0 // indirect
golang.org/x/tools v0.26.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
modernc.org/libc v1.54.2 // indirect
modernc.org/libc v1.61.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.30.1 // indirect
modernc.org/sqlite v1.33.1 // indirect
)
Loading

0 comments on commit 49edee4

Please sign in to comment.