Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
unblee committed Feb 27, 2017
0 parents commit 150fb5a
Show file tree
Hide file tree
Showing 16 changed files with 830 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[{*.go,Makefile}]
indent_style = tab
indent_size = 4

[*.md]
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mirror
vendor/
release/*
!release/.gitkeep
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM scratch
COPY mirror /
ENTRYPOINT ["/mirror"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 unblee

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.
81 changes: 81 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
VERSION := 0.1.0
USERNAME := unblee
BINNAME := mirror
REPONAME := $(BINNAME)
REVISION := $(shell git rev-parse --short HEAD)
GO_VERSION := 1.8.0
LDFLAGS := -w -s \
-X main.Version=$(VERSION) \
-X main.Revision=$(REVISION) \
-X main.GoVersion=$(GO_VERSION) \
-extldflags '-static'

BUILD_REPO_ROOT := /go/src/$(REPONAME)
RELEASE_BIN_DIR := $(BUILD_REPO_ROOT)/release/bin
RELEASE_ARCHIVE_DIR := $(BUILD_REPO_ROOT)/release/archive

DOCKER_ENV := -e VERSION=$(VERSION) \
-e USERNAME=$(USERNAME) \
-e BINNAME=$(BINNAME) \
-e REPONAME=$(REPONAME) \
-e LDFLAGS='$(LDFLAGS)' \
-e BUILD_REPO_ROOT='$(BUILD_REPO_ROOT)' \
-e RELEASE_BIN_DIR='$(RELEASE_BIN_DIR)' \
-e RELEASE_ARCHIVE_DIR='$(RELEASE_ARCHIVE_DIR)' \
-e GITHUB_TOKEN='$(GITHUB_TOKEN)'

DOCKER_RUN_OPT := -v $(PWD):$(BUILD_REPO_ROOT) \
-w /go/src/$(BINNAME) \
$(DOCKER_ENV)
DOCKER_RUN := docker run -it --rm $(DOCKER_RUN_OPT) golang:$(GO_VERSION)-alpine

.PHONY: gh-release
gh-release: archive
$(DOCKER_RUN) sh scripts/gh_release.sh

.PHONY: archive
archive: bin-build
$(DOCKER_RUN) sh scripts/archive.sh

.PHONY: dh-release
dh-release: docker-build
docker push $(USERNAME)/$(BINNAME):$(VERSION)

.PHONY: docker-build
docker-build: bin-build
cp -f release/bin/$(BINNAME)-$(VERSION)-linux-amd64/$(BINNAME) $(BINNAME)
docker build -t $(USERNAME)/$(BINNAME):$(VERSION) .
docker tag $(USERNAME)/$(BINNAME):$(VERSION) $(USERNAME)/$(BINNAME):latest
rm -f $(BINNAME)

.PHONY: bin-build
bin-build: deps
$(DOCKER_RUN) sh scripts/build.sh

.PHONY: deps
deps:
glide install

.PHONY: example-up
example-up: example-stop docker-build
{ \
cd example; \
docker-compose up -d; \
}

.PHONY: example-stop
example-stop:
{ \
cd example; \
docker-compose stop; \
docker-compose rm -f; \
}

.PHONY: test
test:
go test -v `glide novendor | grep -v example`

.PHONY: clean
clean:
rm -fr release/archive
rm -fr release/bin
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# mirror

VirtualHost based dynamic reverse proxy

TODO: write document

## Available Environment Values

```
Flags:
-h, --help: this message
--version: show version
Available Environment Values:
LISTEN_PORT Listening port number
default: 8080
DEFAULT_DEST_PORT Default destination port number
default: 80
BASE_DOMAIN Base Domain
default: not set(required)
DEFAULT_DEST_URL Default destination URL
default: not set(allow empty)
DB_HOST Hostname of the database to connect
default: 127.0.0.1
DB_PORT Port number of the database to connect
default: 6379
```
57 changes: 57 additions & 0 deletions example/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: "3"

services:
proxy:
image: unblee/mirror:latest
ports:
- "80:5000"
environment:
- LISTEN_PORT=5000
- DEFAULT_DEST_PORT=80
- BASE_DOMAIN=127.0.0.1.xip.io
- DEFAULT_DEST_URL=http://default:8080
- DB_HOST=redis
- DB_PORT=6379
depends_on:
- redis

redis:
image: redis:alpine
expose:
- "6379"

rcli:
image: redis:alpine
command: sh -c "redis-cli -h redis HSET mirror-store dest1 http://dest1; redis-cli -h redis HSET mirror-store dest2 http://dest2:8080/{}"
depends_on:
- redis

dest1:
image: golang:alpine
environment:
- PORT=80
volumes:
- .:/go/src
expose:
- "80"
command: ["go", "run", "/go/src/exampleDestServer.go"]

dest2:
image: golang:alpine
environment:
- PORT=8080
volumes:
- .:/go/src
expose:
- "8080"
command: ["go", "run", "/go/src/exampleDestServer.go"]

default:
image: golang:alpine
environment:
- PORT=8080
volumes:
- .:/go/src
expose:
- "8080"
command: ["go", "run", "/go/src/exampleDestServer.go"]
18 changes: 18 additions & 0 deletions example/exampleDestServer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

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

func main() {
port := os.Getenv("PORT")
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, req.Host+req.URL.Path+req.URL.RawPath)
})
http.HandleFunc("/dest2", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, req.Host+req.URL.Path+req.URL.RawPath)
})
http.ListenAndServe(":"+port, nil)
}
22 changes: 22 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package: github.com/unblee/mirror
import:
- package: github.com/vulcand/oxy
subpackages:
- forward
- package: github.com/Sirupsen/logrus
version: v0.11.0
- package: github.com/garyburd/redigo
subpackages:
- redis
- package: github.com/pkg/errors
version: v0.8.0
Loading

0 comments on commit 150fb5a

Please sign in to comment.