Skip to content

Commit

Permalink
all: add few more customization
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Oct 30, 2020
1 parent bfe5c97 commit cae3f36
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 76 deletions.
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@
VERSION = $(shell git describe --always --tags)
BUILDTIME = $(shell date +%FT%T%z)
GOPATH=$(shell go env GOPATH)
IMAGE = golang-design/redir
IMAGE = redir
BINARY = redir.app
TARGET = -o $(BINARY)
BUILD_SETTINGS = -ldflags="-X main.Version=$(VERSION) -X main.BuildTime=$(BUILDTIME)"
BUILD_FLAGS = $(TARGET) $(BUILD_SETTINGS)
BUILD_FLAGS = $(TARGET) $(BUILD_SETTINGS) -mod=vendor

all: native
native:
GO111MODULE=on go build $(BUILD_FLAGS)
go build $(BUILD_FLAGS)
run:
./$(BINARY) -s
build:
GOOS=linux go build $(BUILD_FLAGS)
docker build -t $(IMAGE):$(VERSION) -t $(IMAGE):latest -f docker/Dockerfile .
compose:
up: down
docker-compose -f docker/docker-compose.yml up -d
compose-down:
down:
docker-compose -f docker/docker-compose.yml down
test:
mkdir -p build
go test -cover -coverprofile=build/cover.test -v ./...
go tool cover -html=build/cover.test -o build/cover.html
clean:
clean: down
rm redir.app
docker rmi -f $(shell docker images -f "dangling=true" -q) 2> /dev/null; true
docker rmi -f $(IMAGE):latest $(IMAGE):$(VERSION) 2> /dev/null; true
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ a request redirector that is dedicated for golang.design

The current `redir` implementation talks to a redis data store for PV/UV counting,
as well as short alias storage. In the booting phase, it will read `REDIR_CONF`
from environment variable to identify configuration file (default: `./config.yml`).
from environment variable to identify configuration file (default: [`./config.yml`](./config.yml)).

`redir` is designed for the following purpose: serve two major
redirectors `/s` and `/x` (at the moment).
Expand Down Expand Up @@ -72,12 +72,12 @@ Moreover, it is possible to visit [`/s`](https://golang.design/s) directly listi
`Makefile` defines different ways to build the service:

```bash
make # build native binary
make run # assume your local redis is running
make build # build docker images
make compose # run via docker-compose
make compose-down # remove compose stuff
make clean # cleanup
make # build native binary
make run # assume your local redis is running
make build # build docker images
make up # run via docker-compose
make down # remove compose stuff
make clean # cleanup
```

## Troubleshooting
Expand All @@ -87,6 +87,7 @@ make clean # cleanup
1. make sure you are a member of golang.design
2. add ssh public key to your account
3. `git config --global url."[email protected]:".insteadOf "https://github.com/"`
4. add `export GOPRIVATE=golang.design/x` to your bash profile (e.g. `.zshrc`).

## License

Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
)

type config struct {
Title string `yaml:"title"`
Host string `yaml:"host"`
Addr string `yaml:"addr"`
Store string `yaml:"store"`
Expand All @@ -35,7 +36,9 @@ type config struct {
VCS string `yaml:"vcs"`
ImportPath string `yaml:"import_path"`
RepoPath string `yaml:"repo_path"`
GoDocHost string `yaml:"godoc_host"`
} `yaml:"x"`
GoogleAnalytics string `yaml:"google_analytics"`
}

func (c *config) parse() {
Expand Down
15 changes: 13 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# by a MIT license that can be found in the LICENSE file.

---
title: "The golang.design Initiative"
host: https://golang.design
addr: :8080
addr: :9123
store: redis://localhost:6379/9
backup_min: 10
backup_dir: ./data/backup
Expand All @@ -15,4 +16,14 @@ x:
prefix: /x/
vcs: git
import_path: golang.design/x/*
repo_path: https://github.com/golang-design/*
repo_path: https://github.com/golang-design/*
godoc_host: https://pkg.go.dev/
google_analytics: |
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-80889616-4');
</script>
15 changes: 13 additions & 2 deletions data/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# by a MIT license that can be found in the LICENSE file.

---
host: https://golang.design/
title: The golang.design Initiative
host: https://golang.design
addr: :8080
store: redis://redis:6379/9
backup_min: 1440 # 24h
Expand All @@ -15,4 +16,14 @@ x:
prefix: /x/
vcs: git
import_path: golang.design/x/*
repo_path: https://github.com/golang-design/*
repo_path: https://github.com/golang-design/*
godoc_host: https://pkg.go.dev/
google_analytics: |
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-80889616-4');
</script>
9 changes: 2 additions & 7 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
# All rights reserved. Use of this source code is governed
# by a MIT license that can be found in the LICENSE file.

FROM golang:1.14.9
FROM golang:1.15.3
WORKDIR /app
COPY . .
RUN go build -o redir -ldflags \
"-X main.Version=v$(git describe --always --tags) \
-X main.BuildTime=$(date +%FT%T%z)"
# @changkun: could use alpine instead, maybe in the future.
EXPOSE 8080
CMD ["/app/redir", "-s"]
CMD ["/app/redir.app", "-s"]
14 changes: 7 additions & 7 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ services:
container_name: redir
restart: always
volumes:
- ../data/backup:/data/backup
image: golang-design/redir:latest
- ../data/backup:/app/data/backup
image: redir:latest
environment:
REDIR_CONF: ./data/container.yml
cap_add:
- SYS_PTRACE # for debugging
ports:
- "8080:8080"
- "9123:8080"
depends_on:
- redis
networks:
redirnet:
ipv4_address: 172.16.238.10
ipv4_address: 172.16.1.10
redis:
container_name: redis
restart: always
volumes:
- ../data/redis:/data
image: redis:3.2
image: redis:6.0
ports:
- "6379:6379"
networks:
redirnet:
ipv4_address: 172.16.238.11
ipv4_address: 172.16.1.11
networks:
redirnet:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
- subnet: 172.16.1.0/24
13 changes: 3 additions & 10 deletions public/stats.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-80889616-4');
</script>
{{ .GoogleAnalytics }}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>golang.design link aliases</title>
Expand Down Expand Up @@ -64,7 +57,7 @@
</head>
<body>
<div id="app" class="bg-dark">
<h1>The golang.design Initiative</h1>
<h1>{{ .Title }}</h1>
<h5><a href="https://github.com/golang-design/redir">An alias request redirector</a></h5>
<table class="table table-striped table-dark" style="width:100%">
<tr>
Expand All @@ -76,7 +69,7 @@ <h5><a href="https://github.com/golang-design/redir">An alias request redirector
<tr>
<td>{{ .PV }}</td>
<td> {{ .UV }}</td>
<td><a href="https://golang.design/s/{{ .Alias }}">https://golang.design/s/{{ .Alias }}</a></td>
<td><a href="{{ $.Host }}/s/{{ .Alias }}">{{ $.Host }}/s/{{ .Alias }}</a></td>
</tr>
{{end}}
</table>
Expand Down
9 changes: 1 addition & 8 deletions public/x.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<!DOCTYPE html>
<html><head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-80889616-4');
</script>
{{ .GoogleAnalytics }}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="{{.ImportRoot}} {{.VCS}} {{.VCSRoot}}">
<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/{{.ImportRoot}}{{.Suffix}}">
Expand Down
21 changes: 6 additions & 15 deletions redir.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ func main() {
return
}

if daemon == nil {
flag.Usage()
return
}
if *daemon {
processServer()
return
Expand Down Expand Up @@ -68,29 +64,24 @@ redir -op fetch -a alias fetch alias information
}

func processCmd() {
if fromfile != nil {
fname := *fromfile
if fname == "" {
flag.Usage()
return
}

shortFile(fname)
if *fromfile != "" {
shortFile(*fromfile)
return
}

if operate == nil || !op(*operate).valid() {
if !op(*operate).valid() {
flag.Usage()
return
}

switch o := op(*operate); o {
case opCreate, opUpdate:
if alias == nil || link == nil || *alias == "" || *link == "" {
if *alias == "" || *link == "" {
flag.Usage()
return
}
case opDelete, opFetch:
if alias == nil || *alias == "" {
if *alias == "" {
flag.Usage()
return
}
Expand Down
15 changes: 12 additions & 3 deletions short.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io/ioutil"
"log"
"net"
Expand Down Expand Up @@ -190,16 +191,24 @@ func (s *server) readIP(r *http.Request) string {
}

type arecords struct {
Records []arecord
Title string
Host string
Records []arecord
GoogleAnalytics template.HTML
}

func (s *server) stats(ctx context.Context, w http.ResponseWriter) (retErr error) {
aliases, retErr := s.db.Keys(ctx, prefixalias+"*")
if retErr != nil {
return
}

ars := arecords{Records: make([]arecord, len(aliases))}
fmt.Println(conf.GoogleAnalytics)
ars := arecords{
Title: conf.Title,
Host: conf.Host,
Records: make([]arecord, len(aliases)),
GoogleAnalytics: template.HTML(conf.GoogleAnalytics),
}
for i, a := range aliases {
raw, err := s.db.Fetch(ctx, a)
if err != nil {
Expand Down
21 changes: 12 additions & 9 deletions x.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ package main
import (
"bytes"
"fmt"
"html/template"
"net/http"
"strings"
)

type x struct {
ImportRoot string
VCS string
VCSRoot string
Suffix string
ImportRoot string
VCS string
VCSRoot string
Suffix string
GoogleAnalytics template.HTML
}

// xHandler redirect returns an HTTP handler that redirects requests for
Expand All @@ -42,7 +44,7 @@ func (s *server) xHandler(vcs, importPath, repoPath string) http.Handler {
var importRoot, repoRoot, suffix string
if wildcard {
if path == importPath {
http.Redirect(w, req, "https://pkg.go.dev/"+importPath, 302)
http.Redirect(w, req, conf.X.GoDocHost+importPath, 302)
return
}
if !strings.HasPrefix(path, importPath+"/") {
Expand All @@ -65,10 +67,11 @@ func (s *server) xHandler(vcs, importPath, repoPath string) http.Handler {
suffix = path[len(importPath):]
}
d := &x{
ImportRoot: importRoot,
VCS: vcs,
VCSRoot: repoRoot,
Suffix: suffix,
ImportRoot: importRoot,
VCS: vcs,
VCSRoot: repoRoot,
Suffix: suffix,
GoogleAnalytics: template.HTML(conf.GoogleAnalytics),
}
var buf bytes.Buffer
err := xTmpl.Execute(&buf, d)
Expand Down

0 comments on commit cae3f36

Please sign in to comment.