Skip to content

Commit 596e1af

Browse files
authored
Merge pull request #2 from mxstbr/cli
Add CLI
2 parents 5fce45f + d7c10a9 commit 596e1af

File tree

7 files changed

+94
-36
lines changed

7 files changed

+94
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ views.db
22
node_modules
33
.DS_STORE
44
coverage
5+
dist

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ A tiny analytics server with less than 100 lines of code, easy to run and hack a
1010

1111
## Setup
1212

13-
1. `git clone [email protected]:mxstbr/micro-analytics` to get the repo.
14-
2. `npm install` to install the dependencies.
15-
3. `npm start` to start the service.
13+
```
14+
npm install -g micro-analytics-cli
15+
micro-analytics
16+
```
17+
18+
And that's it! 🎉 The analytics server is now running at `localhost:3000`. (see [`server-setup.md`](./server-setup.md) for instructions on acquiring a server and setting up `nginx` to make this publicly available)
1619

17-
And that's it! 🎉 (see [`deployment.md`](./deployment.md) for deployment instructions)
20+
> **Note**: You can pass any option to the `micro-analytics` command that you can pass to [`micro`](https://github.com/zeit/micro). As an example, to change the host you'd do `micro-analytics -H 127.0.0.1`
1821
1922
## Usage
2023

2124
### Tracking views
2225

23-
To track a view, simply send a request to `/<yourpath>`. If you send a `GET` request, the request will increment the views and return the total views. If you send a `POST` request, the views will increment but you don't get the total views back.
26+
To track a view, simply send a request to `/<id>`. If you send a `GET` request, the request will increment the views and return the total views for that id. If you send a `POST` request, the views will increment but you don't get the total views back.
2427

2528
This is how you'd track pageviews for a website: (though note that this can be used to track anything you want)
2629

@@ -34,13 +37,9 @@ This is how you'd track pageviews for a website: (though note that this can be u
3437
</script>
3538
```
3639

37-
If you just want to get the views for a path and don't want to increment the views during a `GET` request, set `inc` to `false` in your query parameter. (`/<yourpath>?inc=false`)
38-
39-
If you want to get all views for all paths, set the `all` query parameter to `true`. (`/?all=true`)
40-
41-
## Contributing
40+
If you just want to get the views for an id and don't want to increment the views during a `GET` request, set `inc` to `false` in your query parameter. (`/<id>?inc=false`)
4241

43-
If you run `npm run dev` the server will restart every time you edit the code. Perfect for development of `micro-analytics`!
42+
If you want to get all views for all ids, set the `all` query parameter to `true`. (`/?all=true`)
4443

4544
## Built with
4645

cli.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
3+
const { exec } = require('shelljs')
4+
5+
exec('npm start -- ' + process.argv.join(' '), {
6+
async: true,
7+
cwd: __dirname
8+
})

contributing.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Contributing
2+
3+
## Local setup
4+
5+
1. Clone the repository with `git clone [email protected]:mxstbr/micro-analytics.git`
6+
2. Install the dependencies with `npm install` (or `yarn install` if you have `yarn`)
7+
3. Run the server in dev mode with `npm run dev`, which will restart the server everytime you edit the code.

package.json

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
{
2-
"name": "micro-analytics",
3-
"version": "0.1.0",
2+
"name": "micro-analytics-cli",
3+
"version": "0.1.4-4",
44
"description": "Public analytics as a Node.js microservice, no sysadmin experience required.",
5-
"main": "index.js",
5+
"bin": {
6+
"micro-analytics": "./cli.js"
7+
},
8+
"files": [
9+
"dist",
10+
"cli.js"
11+
],
612
"scripts": {
7-
"start": "micro src/index.js",
13+
"start": "micro dist/index.js",
14+
"build": "./node_modules/.bin/async-to-gen src --out-dir dist",
15+
"prepublish": "npm run build",
816
"dev": "NODE_ENV=development nodemon --config package.json src/index.js",
917
"test": "jest"
1018
},
1119
"author": "Max Stoiber <[email protected]> (http://mxstbr.com/)",
1220
"license": "MIT",
21+
"repository": "https://github.com/mxstbr/micro-analytics",
1322
"dependencies": {
1423
"flat-file-db": "^1.0.0",
1524
"micro": "6.1.0",
16-
"promise": "^7.1.1"
25+
"promise": "^7.1.1",
26+
"shelljs": "^0.7.6"
1727
},
1828
"devDependencies": {
29+
"async-to-gen": "^1.3.0",
1930
"babel-jest": "^18.0.0",
2031
"babel-plugin-transform-async-to-generator": "^6.16.0",
2132
"babel-polyfill": "^6.20.0",

deployment.md renamed to server-setup.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
# Deployment
1+
# Server Setup
22

3-
Deploying this service is a short three step process. This assumes you already have a server with an OS of your choice that runs Node setup, if not refer to the [acquire a server](#acquire-a-server) section.
3+
Running `micro-analytics` on a server is a simple two step process:
44

5-
1. Upload the files of the service to your server (you should be able to do this on your own)
6-
2. Start and daemonize the app with `pm2`
7-
3. Setup `nginx` as a reverse proxy to make your service publicy accessible
5+
1. Start and daemonize the app with `pm2`
6+
2. Setup `nginx` as a reverse proxy to make your service publicy accessible
7+
8+
## Prerequisite: A server
9+
10+
If you don't have a server to run this on yet, here is what I do:
11+
12+
I use and like [DigitalOcean](https://m.do.co/c/d371ed7f99af) (referral link) for my servers. A simple, 5$/month droplet will suffice to run this service reliably. (upgrading to more power in case of big traffic is two clicks away too)
13+
14+
Make sure to choose the `NodeJS x.x.x on 16.04` (where `x.x.x` is the highest version number you can find) one-click app when creating the droplet to get the server fully setup with Node and Ubuntu.
815

916
## Start and deamonize the app
1017

11-
Once all the files for this microservice are uploaded we'll use `pm2` to start and deamonize the app. This means that the app isn't bound to the terminal, and we'll also make `pm2` start the app when the server start.
18+
Install `micro-analytics` on your server by running `npm install -g micro-analytics-cli` in its shell.
19+
20+
We'll use `pm2` to start and deamonize the app. This means that the app isn't bound to the terminal, and we'll also make `pm2` start the app when the server start. (this avoids the service being down in case your server reboots)
1221

13-
First run `npm install -g pm2` to get `pm2`, then run `pm2 start npm -- start`. This tells `pm2` to run `npm start` in the background.
22+
First run `npm install -g pm2` to get `pm2`, then run `pm2 start micro-analytics`. This tells `pm2` to run `micro-analytics` in the background.
1423

1524
To make sure our service stays up even if the server reboots we run `pm2 startup ubuntu` (replace `ubuntu` with whatever OS you're using) and `pm2 save`.
1625

1726
## Set up `nginx`
1827

19-
Get `nginx` by running `sudo apt-get install nginx`, then configure it to reroute all incoming requests to the server to our server.
28+
Get `nginx` by running `sudo apt-get install nginx`, then configure it to reroute all incoming requests to the server to `micro-analytics`.
2029

2130
Delete the default site from nginx by running `sudo rm /etc/nginx/sites-enabled/default`, then create a new one by running `sudo touch /etc/nginx/sites-enabled/<yoursite>`. (replace `<yoursite>` with your site name)
2231

23-
We want to reroute all requests for any path that come in to our service, so edit the `/etc/nginx/sites-enabled/<yoursite>` file to look like this:
32+
We want to reroute all requests for any path that come in to `localhost:3000` (where `micro-analytics` is running), so edit the `/etc/nginx/sites-enabled/<yoursite>` file to look like this:
2433

2534
```nginx
2635
server {
@@ -37,11 +46,3 @@ server {
3746
```
3847

3948
(note that I have no idea what I'm doing here, this was copied from [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-for-apache))
40-
41-
## Acquire a server
42-
43-
If you don't have a server to deploy this on just yet, here is what I do:
44-
45-
I use and like [DigitalOcean](https://m.do.co/c/d371ed7f99af) (referral link) for my servers. A simple, 5$/month droplet will suffice to run this service reliably. (upgrading to more power in case of big traffic is two clicks away too)
46-
47-
Make sure to choose the `NodeJS x.x.x on 16.04` (where `x.x.x` is the highest version number you can find) one-click app when creating the droplet to get the server fully setup with Node and Ubuntu.

yarn.lock

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ [email protected]:
9999
babylon "^6.11.4"
100100
magic-string "^0.16.0"
101101

102+
async-to-gen@^1.3.0:
103+
version "1.3.0"
104+
resolved "https://registry.yarnpkg.com/async-to-gen/-/async-to-gen-1.3.0.tgz#ec1ab301ca5881de64e8ad654cd96bfd5fcc4366"
105+
dependencies:
106+
babylon "^6.14.0"
107+
magic-string "^0.19.0"
108+
102109
async@^1.4.0, async@^1.4.2:
103110
version "1.5.2"
104111
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
@@ -335,7 +342,7 @@ babel-types@^6.16.0, babel-types@^6.18.0, babel-types@^6.20.0, babel-types@^6.21
335342
lodash "^4.2.0"
336343
to-fast-properties "^1.0.1"
337344

338-
babylon@^6.11.0, babylon@^6.11.4, babylon@^6.13.0:
345+
babylon@^6.11.0, babylon@^6.11.4, babylon@^6.13.0, babylon@^6.14.0:
339346
version "6.14.1"
340347
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.14.1.tgz#956275fab72753ad9b3435d7afe58f8bf0a29815"
341348

@@ -737,7 +744,7 @@ glob-parent@^2.0.0:
737744
dependencies:
738745
is-glob "^2.0.0"
739746

740-
glob@^7.0.3, glob@^7.0.5:
747+
glob@^7.0.0, glob@^7.0.3, glob@^7.0.5:
741748
version "7.1.1"
742749
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8"
743750
dependencies:
@@ -846,6 +853,10 @@ inherits@2:
846853
version "2.0.3"
847854
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
848855

856+
interpret@^1.0.0:
857+
version "1.0.1"
858+
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.0.1.tgz#d579fb7f693b858004947af39fa0db49f795602c"
859+
849860
invariant@^2.2.0:
850861
version "2.2.2"
851862
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.2.tgz#9e1f56ac0acdb6bf303306f338be3b204ae60360"
@@ -1417,6 +1428,12 @@ magic-string@^0.16.0:
14171428
dependencies:
14181429
vlq "^0.2.1"
14191430

1431+
magic-string@^0.19.0:
1432+
version "0.19.0"
1433+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.19.0.tgz#198948217254e3e0b93080e01146b7c73b2a06b2"
1434+
dependencies:
1435+
vlq "^0.2.1"
1436+
14201437
14211438
version "1.0.11"
14221439
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c"
@@ -1732,6 +1749,12 @@ read-pkg@^1.0.0:
17321749
normalize-package-data "^2.3.2"
17331750
path-type "^1.0.0"
17341751

1752+
rechoir@^0.6.2:
1753+
version "0.6.2"
1754+
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
1755+
dependencies:
1756+
resolve "^1.1.6"
1757+
17351758
redeyed@~1.0.0:
17361759
version "1.0.1"
17371760
resolved "https://registry.yarnpkg.com/redeyed/-/redeyed-1.0.1.tgz#e96c193b40c0816b00aec842698e61185e55498a"
@@ -1814,7 +1837,7 @@ [email protected]:
18141837
version "1.1.7"
18151838
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
18161839

1817-
resolve@^1.2.0:
1840+
resolve@^1.1.6, resolve@^1.2.0:
18181841
version "1.2.0"
18191842
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.2.0.tgz#9589c3f2f6149d1417a40becc1663db6ec6bc26c"
18201843

@@ -1853,6 +1876,14 @@ set-blocking@^2.0.0:
18531876
version "2.0.0"
18541877
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
18551878

1879+
shelljs@^0.7.6:
1880+
version "0.7.6"
1881+
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.6.tgz#379cccfb56b91c8601e4793356eb5382924de9ad"
1882+
dependencies:
1883+
glob "^7.0.0"
1884+
interpret "^1.0.0"
1885+
rechoir "^0.6.2"
1886+
18561887
shellwords@^0.1.0:
18571888
version "0.1.0"
18581889
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.0.tgz#66afd47b6a12932d9071cbfd98a52e785cd0ba14"

0 commit comments

Comments
 (0)