From 3f214169af847701c6a8adb0e353a69d14f2f2a2 Mon Sep 17 00:00:00 2001 From: spelufo Date: Sat, 1 Apr 2023 12:53:14 -0300 Subject: [PATCH] Make dev mode and prod mode both work. --- .gitignore | 3 ++- README.md | 38 +++++++++++++++++++++++++++---------- backend/embed.go | 31 ++++++++++++++++++++++++++++++ backend/server.go | 17 +++++++++++++++-- dev.cljs.edn | 3 ++- dev.sh | 23 +++++++++++++++++++--- package.json | 27 -------------------------- prod.cljs.edn | 3 +++ resources/public/index.html | 2 +- 9 files changed, 102 insertions(+), 45 deletions(-) create mode 100644 backend/embed.go delete mode 100644 package.json create mode 100644 prod.cljs.edn diff --git a/.gitignore b/.gitignore index c1e7c91..55e86b9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ /_notes.yaml local node_modules -/backend/config.json +/backend/public +/backend/js diff --git a/README.md b/README.md index ea7ccb5..37a2701 100644 --- a/README.md +++ b/README.md @@ -52,31 +52,45 @@ You should now be able to write to the file. Check with: $ touch /etc/nginx/nginx.conf ``` - -## Install and run +## Running `noport` is a self-contained static binary. Download it and put it somewhere in your PATH to install it. Run `noport` without arguments to start the server. Open http://localhost:8012 for the web interface. +You can keep it running however you plan on running your other services. One +option is to user systemd user services. Or you can just start it manually when +you want to change the configuration. The changes persist in your nginx.conf. + ## Development -The entrypoint for running development tasks is `./dev.sh`. To work on it, run -both the backend (`./dev.sh server`) and the figwheel development server -(`./dev.sh front`), which compiles cljs and hot reloads it and css on file -changes. +### Build it + +``` +./dev.sh build ~/bin/noport +``` + + +### Run in development mode +Start the server. -## TODO +``` +./dev.sh server +``` -* [ ] ./dev.sh build_release # to bundle everything into a single binary. +Run the frontend figwheel server, that builds and hotloads cljs and css code. +``` +./dev.sh front +``` -## Features -This are some features that noport lacks that we would like to implement. +### Missing features + +This are some features that noport lacks that it would be nice to add. * [ ] SSL certificates with mkcert * [ ] Reorder items. @@ -85,3 +99,7 @@ This are some features that noport lacks that we would like to implement. * [ ] Leverage ports.json to suggest servers and avoid collisions. * [ ] Other OSes. + +### TODO + +* [ ] Fix gin warnings. Run in gin production mode. diff --git a/backend/embed.go b/backend/embed.go new file mode 100644 index 0000000..8667634 --- /dev/null +++ b/backend/embed.go @@ -0,0 +1,31 @@ +package noport + +import ( + "embed" + "io/fs" + "net/http" + + "github.com/gin-contrib/static" +) + +type embedFileSystem struct { + http.FileSystem +} + +func (e embedFileSystem) Exists(prefix string, path string) bool { + _, err := e.Open(path) + if err != nil { + return false + } + return true +} + +func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem { + fsys, err := fs.Sub(fsEmbed, targetPath) + if err != nil { + panic(err) + } + return embedFileSystem{ + FileSystem: http.FS(fsys), + } +} diff --git a/backend/server.go b/backend/server.go index 6de91e3..218e8f7 100644 --- a/backend/server.go +++ b/backend/server.go @@ -1,7 +1,10 @@ package noport import ( + // "fmt" + "embed" "net/http" + "os" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" @@ -12,10 +15,20 @@ const ( theConfigURL = "/.noport.json" ) +var devmode = os.Getenv("NOPORT_DEV") != "" + +//go:embed public +var publicFS embed.FS + func runServer() { r := gin.Default() - r.Use(static.Serve("/", static.LocalFile("./resources/public", false))) - r.Use(static.Serve("/cljs-out", static.LocalFile("./target/public/cljs-out", false))) + if devmode { + r.Use(static.Serve("/", static.LocalFile("./resources/public", false))) + r.Use(static.Serve("/js", static.LocalFile("./target/public/js", false))) + r.Use(static.Serve("/cljs-out", static.LocalFile("./target/public/cljs-out", false))) + } else { + r.Use(static.Serve("/", EmbedFolder(publicFS, "public"))) + } r.GET(theConfigURL, handleConfigGet) r.POST(theConfigURL, handleConfigPost) r.POST("/install", handleInstallPost) diff --git a/dev.cljs.edn b/dev.cljs.edn index e8469c2..27ee275 100644 --- a/dev.cljs.edn +++ b/dev.cljs.edn @@ -1 +1,2 @@ -{ :main noport.main } +{ :main noport.main + :output-to "target/public/js/noport.js" } diff --git a/dev.sh b/dev.sh index cafedfa..94dd718 100755 --- a/dev.sh +++ b/dev.sh @@ -2,9 +2,23 @@ set -e +bin_default=~/bin/noport + build() { - (cd backend; go build -o ~/bin/noport ./cmd/noport) - # figwheel -bo dev + build_front + build_back "$1" +} + +build_back() { + # Assumes we just did build_front. + rm -rf backend/public + cp -r resources/public backend/public + cp -r target/public/js backend/public/js + (cd backend; go build -o "${1:-$bin_default}" ./cmd/noport) +} + +build_front() { + figwheel -bo prod } fmt() { @@ -12,11 +26,14 @@ fmt() { } figwheel() { + export NOPORT_DEV=1 clojure -M -m figwheel.main "$@" } server() { - build && noport + export NOPORT_DEV=1 + build_back "$1" + noport } front() { diff --git a/package.json b/package.json deleted file mode 100644 index d3aa5cd..0000000 --- a/package.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "devDependencies": { - "@rollup/plugin-buble": "^1.0.2", - "@rollup/plugin-commonjs": "^24.0.1", - "@rollup/plugin-json": "^6.0.0", - "@rollup/plugin-node-resolve": "^15.0.1", - "rollup": "^3.20.0", - "rollup-plugin-polyfill-node": "^0.12.0" - }, - "dependencies": { - "markdown-it": "^13.0.1", - "prosemirror-collab": "^1.3.0", - "prosemirror-commands": "^1.5.1", - "prosemirror-example-setup": "^1.2.1", - "prosemirror-gapcursor": "^1.3.1", - "prosemirror-history": "^1.3.0", - "prosemirror-inputrules": "^1.2.0", - "prosemirror-keymap": "^1.2.1", - "prosemirror-markdown": "^1.10.1", - "prosemirror-model": "^1.19.0", - "prosemirror-schema-basic": "^1.2.1", - "prosemirror-schema-list": "^1.2.2", - "prosemirror-state": "^1.4.2", - "prosemirror-transform": "^1.7.1", - "prosemirror-view": "^1.30.2" - } -} diff --git a/prod.cljs.edn b/prod.cljs.edn new file mode 100644 index 0000000..d2719c5 --- /dev/null +++ b/prod.cljs.edn @@ -0,0 +1,3 @@ +{ :main noport.main + :optimizations :advanced + :output-to "target/public/js/noport.js" } diff --git a/resources/public/index.html b/resources/public/index.html index 7755635..703503b 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -10,6 +10,6 @@
- +