Skip to content

Commit

Permalink
Make dev mode and prod mode both work.
Browse files Browse the repository at this point in the history
  • Loading branch information
spelufo committed Apr 1, 2023
1 parent 8bf322e commit 3f21416
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/_notes.yaml
local
node_modules
/backend/config.json
/backend/public
/backend/js
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
31 changes: 31 additions & 0 deletions backend/embed.go
Original file line number Diff line number Diff line change
@@ -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),
}
}
17 changes: 15 additions & 2 deletions backend/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package noport

import (
// "fmt"
"embed"
"net/http"
"os"

"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion dev.cljs.edn
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{ :main noport.main }
{ :main noport.main
:output-to "target/public/js/noport.js" }
23 changes: 20 additions & 3 deletions dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@

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() {
(cd backend; go fmt ./...)
}

figwheel() {
export NOPORT_DEV=1
clojure -M -m figwheel.main "$@"
}

server() {
build && noport
export NOPORT_DEV=1
build_back "$1"
noport
}

front() {
Expand Down
27 changes: 0 additions & 27 deletions package.json

This file was deleted.

3 changes: 3 additions & 0 deletions prod.cljs.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{ :main noport.main
:optimizations :advanced
:output-to "target/public/js/noport.js" }
2 changes: 1 addition & 1 deletion resources/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<body>
<div id="app"></div>
<script type="text/javascript">window.API_URL = location.origin</script>
<script src="/cljs-out/dev-main.js"></script>
<script src="/js/noport.js"></script>
</body>
</html>

0 comments on commit 3f21416

Please sign in to comment.