Skip to content

Commit

Permalink
prepare project for release
Browse files Browse the repository at this point in the history
  • Loading branch information
issmirnov committed Feb 25, 2017
1 parent 37a068f commit 12ba31d
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 383 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# goreleaser
dist/

# OS
*.swp
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2010-2017 Google, Inc. http://angularjs.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
94 changes: 62 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,76 @@
# Zap (WIP)

[![Powered By: GoReleaser](https://img.shields.io/badge/powered%20by-goreleaser-green.svg?style=flat-square)](https://github.com/goreleaser)

A simple recursive URL expander.

Note: As of right now, this repo is still being set up. This message will be removed when everything is ready.
**Note: As of right now, this repo is still being set up. This message will be removed when everything is ready.**

## Overview

ZAP is a simple go app that sends 302 redirects. That's it. It was written in just a few hours in between random household tasks. It helps people be more efficient by providing simple shortcuts for common pages.

It was fun to build because it's super lightweight and insanely fast. Some sample benchmarks:

## Configuration

### gotchas

- `n` and `y` are reserved for canonical yaml booleans. Use quotes in config to avoid issues.
```
# Trial 1: localhost
$ ab -n 10000 -c 100 http://localhost:8927/
Requests per second: 39888.31 [#/sec] (mean)
Time per request: 2.507 [ms] (mean)
# Trial 2: Hitting server on LAN over gigabit, zap behind nginx proxy
$ ab -n 10000 -c 100 http://server/z
Requests per second: 12671.57 [#/sec] (mean)
Time per request: 7.892 [ms] (mean)
# Go benchmarks
$ go test -bench=.
BenchmarkIndexHandler-8 1000000 1679 ns/op
```

## Benchmarks
As you can see, even behind an nginx proxy, running on a server that I
mercilessly abuse we still get a respectable ~13k QPS.

TODO: add `ab` results, as well as `go test -bench .`

---

## Installation

### Quick Install
### Step 0: Quick Install

If you just want to hack around:

1. `go install github.com/issmirnov/zap`
2. `tmux new-session -t zap`
3. `$GOPATH/bin/zap`
4. `curl -I -L -H 'Host: g' localhost:8927/i`
4. `curl -I -L -H 'Host: g' localhost:8927/z` - should return 302 to github.com/issmirnov.zap

If you want to actually install this properly, read on.


### Step 1: Set up Zap to run as a service

Zap takes several command line flags:

- `-config` - path to config file. Default is `./c.yml`
- `-port` - port to bind to. Use either 80 or 8927

#### OSX (brew)

1. `brew install issmirnov/apps/zap`
2. `sudo brew services start zap`

If you already have port 80 in use, you can run zap behind a reverse proxy.

Nginx
1. Change the port in the zap plist config: `sed -i '' 's/8927/80/'  /usr/local/Cellar/zap/*/homebrew.mxcl.zap.plist`
2. Start zap as user service: `brew services start zap`
3. Configure your web server to act as a reverse proxy. For nginx, this will suffice:

```
# File: /usr/local/etc/nginx/servers/zap.conf
server {
listen 80;
server_name s sp sg sf sd;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location / {
listen 80; # Keep as 80, make sure nginx listens on 80 too
server_name e g; # Put your shortcuts here
location / {
proxy_http_version 1.1;
proxy_pass http://localhost:8927;
proxy_set_header X-Forwarded-Host $host;
Expand All @@ -56,28 +79,40 @@ server {
}
```

4. Restart your webserver and test the result: `curl -I -L -H 'Host: g' localhost:8927/z`

#### Ubuntu

TODO: Provide systemd script, add hints about nginx.


### Step 2: Configure DNS

For a lone machine, `/etc/hosts` is easiest. Add one entry per top level shortcut. For the sample config that would be `127.0.0.1 e` and `127.0.0.1 g`. The alternative is `dnsmasqd`, in which case you want to set up dnsmasqd to point to local server.
If you are running `zap` locally, you need to edit `/etc/hosts` and add each top level entry. For the sample config, this would be `127.0.0.1 e` and `127.0.0.1 g`. Adjust accordingly.

For the advanced users, I suggest running `dnsmasqd` and add DNS entries for all TLDs, so that all of your clients will automatically point to the server.

### Step 3: Tweak the config file and launch the service

- `brew services start zap`
- `systemctl start zap`
The config file is located at `/usr/local/etc/zap/` on OSX, and `/path/tbd` on ubuntu.

Open up `c.yml` and update the mappings you would like. You can nest arbitrarily deep. Expansions work on strings and ints.

---
Important gotcha: yaml has [reserved types](http://yaml.org/type/bool.html) and thus `n`, `y`, `no` and the like need to be quoted. See the sample config.

Once you're done editing the file (making sure to keep the DNS entries in sync) restart the service and test it out.

- OSX: `sudo brew services restart zap` or `brew services restart zap`
- Ubuntu: `systemctl restart zap`

## Contributing

Standard GitHub workflow - fork and submit a PR.
Patches are welcome! Please use the standard GitHub workflow - fork this repo and submit a PR. I'll usually get to it within a few days.

### Handy Commands
Handy commands for local dev:

- `go run main.go config.go text.go web.go` to run locally
- `curl -I -L -H 'Host: g' localhost:8927/i/dotfiles` - to test locally e2e
- `curl -I -L -H 'Host: g' localhost:8927/z` - to test locally e2e
- `goconvey` - launches web UI for go tests.
- `go test` runs CLI tests.

Expand All @@ -86,8 +121,8 @@ Standard GitHub workflow - fork and submit a PR.

A short list of upcoming features and fixes, sorted by deadline.

- GoReleaser for release automation
- Homebrew install role
- GoReleaser for release automation - DONE
- Homebrew install script - DONE
- Systemd service script
- configurable index page. so 'start' or 'index.html', set in top level domain config.
- queries: so `s/dns` -> `smirnov.wiki/start?do=search&id=dns`
Expand All @@ -97,11 +132,6 @@ A short list of upcoming features and fixes, sorted by deadline.
- add check for dual "expand" keys in config



## License

`ZAP` is licensed with MIT

## Contributors

- [Ivan Smirnov](http://ivansmirnov.name)
45 changes: 7 additions & 38 deletions c.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
sp:
expand: smirnov.wiki/project
h:
expand: hydra
m:
expand: monitoring
e:
expand: example.com
a:
expand: ansible
z:
expand: zap
sg:
expand: smirnov.wiki/goals
'n':
expand: 2017
'18':
expand: 2018
sd:
expand: smirnov.wiki/device
p:
expand: puma
s:
expand: services
s:
expand: smirnov.wiki
sc:
expand: smirnov.wiki/cycles
w:
expand: weekly
m:
expand: monthly
gh:
expand: apples
b:
expand: bananas
g:
expand: github.com
d:
expand: issmirnov/dotfiles
g:
expand: git.smirnov.work
f:
expand: smivan/fleet
s:
expand: smivan/srm
z:
expand: smivan/zap

expand: issmirnov/zap
Binary file added dist/zap_Darwin_x86_64/zap
Binary file not shown.
Binary file added dist/zap_Linux_x86_64/zap
Binary file not shown.
16 changes: 16 additions & 0 deletions goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
build:
main: .
binary_name: zap
goos:
- darwin
- linux
archive:
replacements:
amd64: 64-bit
386: 32-bit
darwin: MacOS
linux: Linux
files:
- c.yml
- README.md
- LICENSE
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ import (
"flag"
"fmt"
"net/http"
"os"
)

const appName = "zap"

var version = "develop"

func main() {

var (
configName = flag.String("config", "c.yml", "config file")
port = flag.Int("port", 8927, "port to bind to")
host = flag.String("host", "127.0.0.1", "host interface")
i = flag.String("index", "start", "string to append if path has trailing slash")
v = flag.Bool("v", false, "print version info")
)
flag.Parse()

if *v {
fmt.Println(version)
os.Exit(0)
}

c, err := parseYaml(*configName)
if err != nil {
fmt.Printf("error: %s\n", err)
Expand Down
25 changes: 13 additions & 12 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
)

const cYaml = `
sp:
expand: smirnov.wiki/project
h:
expand: hydra
sg:
expand: smirnov.wiki/goals
'n':
expand: 2017
sd:
expand: smirnov.wiki/device
p:
expand: puma
e:
expand: example.com
a:
expand: apples
b:
expand: bananas
g:
expand: github.com
d:
expand: issmirnov/dotfiles
z:
expand: issmirnov/zap
`

func parseDummyYaml() (*gabs.Container, error) {
Expand Down
Loading

0 comments on commit 12ba31d

Please sign in to comment.