Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update json-server-gui #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 82 additions & 66 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,66 +1,82 @@
SOURCES=src/node_modules $(shell find src -type f)
NWDIR=./node_modules/node-webkit-builder
NWBUILD=$(NWDIR)/bin/nwbuild.linux
VERSION=$(shell cat src/package.json | grep -F '"version":' | grep -o "[0-9]\+.[0-9]\+.[0.9]\+")

.PHONY: build clean build-linux32 build-linux64 build-windows build-osx dist release

build: build-linux32 build-linux64 build-windows build-osx

build-linux32: build/json-server-gui/linux32/json-server-gui

build-linux64: build/json-server-gui/linux64/json-server-gui

build-windows: build/json-server-gui/win/json-server-gui.exe

build-osx: build/json-server-gui/osx/json-server-gui.app

dist: clean build
@rm -rf dist/$(VERSION)
cd build/json-server-gui && mv linux32 json-server-gui-linux32 && tar zcvf json-server-gui-linux32.tar.gz json-server-gui-linux32 && rm -rf json-server-gui-linux32
cd build/json-server-gui && mv linux64 json-server-gui-linux64 && tar zcvf json-server-gui-linux64.tar.gz json-server-gui-linux64 && rm -rf json-server-gui-linux64
cd build/json-server-gui && mv osx json-server-gui-osx && zip -r json-server-gui-osx.zip json-server-gui-osx && rm -rf json-server-gui-osx
cd build/json-server-gui && mv win json-server-gui-win && zip -r json-server-gui-win.zip json-server-gui-win && rm -rf json-server-gui-win
mkdir -p dist/$(VERSION)
mv build/json-server-gui/json-server-gui-linux32.tar.gz dist/$(VERSION)/
mv build/json-server-gui/json-server-gui-linux64.tar.gz dist/$(VERSION)/
mv build/json-server-gui/json-server-gui-osx.zip dist/$(VERSION)/
mv build/json-server-gui/json-server-gui-win.zip dist/$(VERSION)/

release: dist
mv dist/$(VERSION) ~/Dropbox/Public/json-server-gui/
sed -i "s/json-server-gui\/[0-9]\+.[0-9]\+.[0-9]\+\/json-server-gui-/json-server-gui\/$(VERSION)\/json-server-gui-/g" README.md

clean:
rm -rf build

$(NWBUILD): node_modules
cp $(NWDIR)/bin/nwbuild $(NWDIR)/bin/nwbuild.win
tr -d '\015' < $(NWDIR)/bin/nwbuild.win > $@
chmod +x $@

src/node_modules:
cd src && npm install

node_modules:
npm install

build/json-server-gui/linux32/json-server-gui: $(NWBUILD) $(SOURCES)
@mv src/db.json db.json.bak 2> /dev/null || echo "no db.json"
$(NWBUILD) -p linux32 -o build src
@mv db.json.bak src/db.json 2> /dev/null || echo "no db.json"

build/json-server-gui/linux64/json-server-gui: $(NWBUILD) $(SOURCES)
@mv src/db.json db.json.bak 2> /dev/null || echo "no db.json"
$(NWBUILD) -p linux64 -o build src
@mv db.json.bak src/db.json 2> /dev/null || echo "no db.json"

build/json-server-gui/win/json-server-gui.exe: $(NWBUILD) $(SOURCES)
@mv src/db.json db.json.bak 2> /dev/null || echo "no db.json"
$(NWBUILD) -p win -o build src
@mv db.json.bak src/db.json 2> /dev/null || echo "no db.json"

build/json-server-gui/osx/json-server-gui.app: $(NWBUILD) $(SOURCES)
@mv src/db.json db.json.bak 2> /dev/null || echo "no db.json"
$(NWBUILD) -p osx -o build src
@mv db.json.bak src/db.json 2> /dev/null || echo "no db.json"
VERSION = $(shell cat src/package.json | grep -F version | grep -o "[0-9]\+.[0-9]\+.[0.9]\+")

ifndef NPM

NPM = npm

endif

build: build-linux32 build-linux64 build-windows32 build-windows64

build-linux32: build/linux32

build-linux64: build/linux64

build-windows32: build/win32

build-windows64: build/win64

build-osx: build/osx

dist: clean build
@rm -rf dist/$(VERSION)
cd build/json-server-gui && mv linux32 json-server-gui-linux32 && tar zcvf json-server-gui-linux32.tar.gz json-server-gui-linux32 && rm -rf json-server-gui-linux32
cd build/json-server-gui && mv linux64 json-server-gui-linux64 && tar zcvf json-server-gui-linux64.tar.gz json-server-gui-linux64 && rm -rf json-server-gui-linux64
cd build/json-server-gui && mv osx json-server-gui-osx && zip -r json-server-gui-osx.zip json-server-gui-osx && rm -rf json-server-gui-osx
cd build/json-server-gui && mv win32 json-server-gui-win32 && zip -r json-server-gui-win32.zip json-server-gui-win32 && rm -rf json-server-gui-win32
cd build/json-server-gui && mv win64 json-server-gui-win64 && zip -r json-server-gui-win64.zip json-server-gui-win64 && rm -rf json-server-gui-win64
mkdir -p dist/$(VERSION)
mv build/json-server-gui/json-server-gui-linux32.tar.gz dist/$(VERSION)/
mv build/json-server-gui/json-server-gui-linux64.tar.gz dist/$(VERSION)/
mv build/json-server-gui/json-server-gui-osx.zip dist/$(VERSION)/
mv build/json-server-gui/json-server-gui-win32.zip dist/$(VERSION)/
mv build/json-server-gui/json-server-gui-win64.zip dist/$(VERSION)/

release: dist
mv dist/$(VERSION) ~/Dropbox/Public/json-server-gui/
sed -i "s/json-server-gui\/[0-9]\+.[0-9]\+.[0-9]\+\/json-server-gui-/json-server-gui\/$(VERSION)\/json-server-gui-/g" README.md

clean:
rm -rf build

src/node_modules:
cd src/ && npm install

node_modules: src/node_modules
npm install

update:
cd src/ && npm update
npm update

build/linux32: node_modules
@mv src/db.json db.json.bak 2> /dev/null || echo no db.json
$(NPM) run build-linux32
@mv db.json.bak src/db.json 2> /dev/null || echo
@touch build/linux32

build/linux64: node_modules
@mv src/db.json db.json.bak 2> /dev/null || echo no db.json
$(NPM) run build-linux64
@mv db.json.bak src/db.json 2> /dev/null || echo
@touch build/linux64

build/win32: node_modules
@mv src/db.json db.json.bak 2> /dev/null || echo no db.json
$(NPM) run build-win32
@mv db.json.bak src/db.json 2> /dev/null || echo
@touch build/win32

build/win64: node_modules
@mv src/db.json db.json.bak 2> /dev/null || echo no db.json
$(NPM) run build-win64
@mv db.json.bak src/db.json 2> /dev/null || echo
@touch build/win64

build/osx: node_modules
@mv src/db.json db.json.bak 2> /dev/null || echo no db.json
$(NPM) run build-osx
@mv db.json.bak src/db.json 2> /dev/null || echo
@touch build/osx

.PHONY: build clean build-linux32 build-linux64 build-windows build-osx dist release
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ How?

### Download

Pre-built binaries for latest version are available here:
Pre-built binaries for latest version are available here (not available):

* [Linux - 32 bits](https://dl.dropboxusercontent.com/u/6414656/json-server-gui/2.0.0/json-server-gui-linux32.tar.gz) (~40.3M)
* [Linux - 64 bits](https://dl.dropboxusercontent.com/u/6414656/json-server-gui/2.0.0/json-server-gui-linux64.tar.gz) (~38.6M)
Expand All @@ -31,7 +31,7 @@ Pre-built binaries for latest version are available here:

### Build

Clone the repository, then run `make`. The project will build for Linux (32 & 64 bits), Windows (32 bits), and Max OSX.
Clone the repository, then run `make`. The project will build for Linux (32 & 64 bits), Windows (32 & 64 bits), and Max OSX.

It can last about a century first time as it will download all corresponding `node-webkit` distributions.

Expand All @@ -40,16 +40,20 @@ To build specific for a specific platform:
```sh
make build-linux32
make build-linux64
make build-windows
make build-windows32
make build-windows64
make build-osx
```

Moar!
-----

* `$APP_DIR/public` is served as document root
* `$APP_DIR/config/config.json` is served as configuration file (JSON format)
* `DataPath` = directory where we'll search for `db.json` and `public` folder (default = app's folder)
* `Port` = server's port (default = 26080)
* `$APP_DIR/db.json` is your data file
* Some behavior can be defined from environment variables:
* Some behavior can be defined from environment variables (if not set in the `config.json` file):
* `APP_DIR` = directory where we'll search for `db.json` and `public` folder (default = app's folder)
* `PORT` = server's port (default = 26080)

Expand All @@ -58,11 +62,20 @@ TODO

* Better GUI
* Persisted configuration (instead of env)
* Use less tricks to pass events

How to contribute
-----------------

* Fork & clone
* Install [node-webkit](https://github.com/rogerwang/node-webkit#downloads) for your platform (alternatively, you can run `make build-<your platform>` and grab node-webkit from `node_modules/node-webkit-builder/cache/0.10.5/<your platform>/`, saving you a duplicate download)
* Install [node-webkit](https://github.com/rogerwang/node-webkit#downloads) for your platform (alternatively, you can run `make build-<your platform>` and grab node-webkit from `node_modules/nw-builder/cache/0.10.5/<your platform>/`, saving you a duplicate download)
* Make your changes and test them by running `/path/to/nw src`
* Create a pull request

Contributions
-------------

* Persistent configuration file
* Auto-reload when file changes
* Button to start and stop json-server without exiting GUI
* Update to last version of `json-server` and `node-webkit`
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"version": "1.0.1",
"description": "JSON Server GUI - builder",
"devDependencies": {
"node-webkit-builder": "^0.1.3"
"nw-builder": "latest"
},
"scripts": {
"fix-nwbuild": "cp ./node_modules/.bin/nwbuild ./node_modules/.bin/nwbuild.win && tr -d '\\015' < ./node_modules/.bin/nwbuild.win > ./node_modules/.bin/nwbuild",
"build-win": "nwbuild -p win src",
"build-linux32": "nwbuild -p linux32 src",
"build-win64": "nwbuild -p win64 src",
"build-win32": "nwbuild -p win32 src",
"build-win": "nwbuild -p win32,win64 src",
"build-linux64": "nwbuild -p linux64 src",
"build-linux32": "nwbuild -p linux32 src",
"build-linux": "nwbuild -p linux32,linux64 src",
"build-osx": "nwbuild -p osx src",
"build-all": "nwbuild -p linux64,linux32,win,osx src"
"build-all": "nwbuild -p osx,win64,win32,linux64,linux32 src"
}
}
117 changes: 73 additions & 44 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,73 @@
// Escape = close app
document.addEventListener("keydown", function (e) {
if (e.keyCode == 27) {
process.exit(0);
}
});

// Thanks http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript
function syntaxHighlight(json) {
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}

// Refresh listing
var elData = document.getElementById("data");
elData.innerHTML = syntaxHighlight(JSON.stringify(app.low.db, null, 2));
process.on("data-update", function () {
elData.innerHTML = syntaxHighlight(JSON.stringify(app.low.db, null, 2));
});

// Logs
var elLogs = document.getElementById("logs");
process.on("request", function (method, url, body) {
var li = "<li><strong>" + method + " " + url + "</strong>";
if (method !== "GET") {
li += "<br><code>" + JSON.stringify(body) + "</code>";
}
li += "</li>";
elLogs.innerHTML += li;
});
// Escape = close app
document.addEventListener("keydown", function (e) {
if (e.keyCode == 27) {
process.exit(0);
}
});

// Thanks http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript
function syntaxHighlight(json) {
if (typeof json === "undefined") {
return "";
}
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}

var serv = true;

function clearcontent()
{
elLogs.innerHTML = "<li>Serveur: <strong>http://localhost:" + port + "/</strong> | Status: <strong id=\"status\"></strong></li>\
<li><a href=\"javascript:clearcontent();\">Clear logs</a> | <a href=\"javascript:btnSwitchServ()\" id=\"btnServ\">" + (serv ? "Stop server" : "Start server") + "</a></li>";
process.emit("data-update");
conn(null);
}

function btnSwitchServ()
{
serv = !serv;
document.getElementById("btnServ").innerHTML = (serv ? "Stop server" : "Start server");
if (!serv)
{
stop_server();
}
else
{
start_server();
}
}

// Refresh listing
var elData = document.getElementById("data");
console.log(server);
elData.innerHTML = syntaxHighlight(JSON.stringify(router.db.getState(), null, 2));
process.on("data-update", function () {
elData.innerHTML = syntaxHighlight(JSON.stringify(router.db.getState(), null, 2));
});

// Logs
var elLogs = document.getElementById("logs");
process.on("request", function (method, url, body) {
var li = '<li><strong>' + method + " " + url + '</strong>';
if (method !== "GET" && method !== "DELETE") {
li += '<br><code>' + JSON.stringify(body) + '</code>';
}
li += '</li>';
elLogs.innerHTML += li;
process.emit("data-update");
});
5 changes: 5 additions & 0 deletions src/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Comment": "This is the default configuration :",
"Port": 26080,
"DataPath": "."
}
18 changes: 17 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@
<head>
<title>JSON Server</title>
<link rel="stylesheet" href="styles.css">
<script type="text/javascript">
var currentStatus = "Offline";

function conn(status)
{
if (status != null)
{
currentStatus = status;
}
document.getElementById("status").innerHTML = currentStatus;
}
</script>
<script src="server.js"></script>
</head>
<body>
<div id="container">
<pre id="data"></pre>
<ul id="logs">
<li>Serveur: <strong>http://localhost:<script>document.write(app.port)</script>/</strong></li>
<li>Serveur: <strong>http://localhost:<script>document.write(port)</script>/</strong> | Status: <strong id="status"><script>conn("Offline")</script></strong></li>
<li><a href="javascript:clearcontent();">Clear logs</a> | <a href="javascript:btnSwitchServ()" id="btnServ">Stop server</a></li>
</ul>
</div>
<script src="client.js"></script>
<script type="text/javascript">
start_server();
</script>
</body>
</html>
Loading