Skip to content

Commit cae3f36

Browse files
committed
all: add few more customization
1 parent bfe5c97 commit cae3f36

File tree

12 files changed

+87
-76
lines changed

12 files changed

+87
-76
lines changed

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@
55
VERSION = $(shell git describe --always --tags)
66
BUILDTIME = $(shell date +%FT%T%z)
77
GOPATH=$(shell go env GOPATH)
8-
IMAGE = golang-design/redir
8+
IMAGE = redir
99
BINARY = redir.app
1010
TARGET = -o $(BINARY)
1111
BUILD_SETTINGS = -ldflags="-X main.Version=$(VERSION) -X main.BuildTime=$(BUILDTIME)"
12-
BUILD_FLAGS = $(TARGET) $(BUILD_SETTINGS)
12+
BUILD_FLAGS = $(TARGET) $(BUILD_SETTINGS) -mod=vendor
1313

1414
all: native
1515
native:
16-
GO111MODULE=on go build $(BUILD_FLAGS)
16+
go build $(BUILD_FLAGS)
1717
run:
1818
./$(BINARY) -s
1919
build:
20+
GOOS=linux go build $(BUILD_FLAGS)
2021
docker build -t $(IMAGE):$(VERSION) -t $(IMAGE):latest -f docker/Dockerfile .
21-
compose:
22+
up: down
2223
docker-compose -f docker/docker-compose.yml up -d
23-
compose-down:
24+
down:
2425
docker-compose -f docker/docker-compose.yml down
2526
test:
2627
mkdir -p build
2728
go test -cover -coverprofile=build/cover.test -v ./...
2829
go tool cover -html=build/cover.test -o build/cover.html
29-
clean:
30+
clean: down
3031
rm redir.app
3132
docker rmi -f $(shell docker images -f "dangling=true" -q) 2> /dev/null; true
3233
docker rmi -f $(IMAGE):latest $(IMAGE):$(VERSION) 2> /dev/null; true

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ a request redirector that is dedicated for golang.design
66

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

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

7474
```bash
75-
make # build native binary
76-
make run # assume your local redis is running
77-
make build # build docker images
78-
make compose # run via docker-compose
79-
make compose-down # remove compose stuff
80-
make clean # cleanup
75+
make # build native binary
76+
make run # assume your local redis is running
77+
make build # build docker images
78+
make up # run via docker-compose
79+
make down # remove compose stuff
80+
make clean # cleanup
8181
```
8282

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

9192
## License
9293

config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
)
2222

2323
type config struct {
24+
Title string `yaml:"title"`
2425
Host string `yaml:"host"`
2526
Addr string `yaml:"addr"`
2627
Store string `yaml:"store"`
@@ -35,7 +36,9 @@ type config struct {
3536
VCS string `yaml:"vcs"`
3637
ImportPath string `yaml:"import_path"`
3738
RepoPath string `yaml:"repo_path"`
39+
GoDocHost string `yaml:"godoc_host"`
3840
} `yaml:"x"`
41+
GoogleAnalytics string `yaml:"google_analytics"`
3942
}
4043

4144
func (c *config) parse() {

config.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
# by a MIT license that can be found in the LICENSE file.
44

55
---
6+
title: "The golang.design Initiative"
67
host: https://golang.design
7-
addr: :8080
8+
addr: :9123
89
store: redis://localhost:6379/9
910
backup_min: 10
1011
backup_dir: ./data/backup
@@ -15,4 +16,14 @@ x:
1516
prefix: /x/
1617
vcs: git
1718
import_path: golang.design/x/*
18-
repo_path: https://github.com/golang-design/*
19+
repo_path: https://github.com/golang-design/*
20+
godoc_host: https://pkg.go.dev/
21+
google_analytics: |
22+
<!-- Global site tag (gtag.js) - Google Analytics -->
23+
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
24+
<script>
25+
window.dataLayer = window.dataLayer || [];
26+
function gtag(){dataLayer.push(arguments);}
27+
gtag('js', new Date());
28+
gtag('config', 'UA-80889616-4');
29+
</script>

data/container.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# by a MIT license that can be found in the LICENSE file.
44

55
---
6-
host: https://golang.design/
6+
title: The golang.design Initiative
7+
host: https://golang.design
78
addr: :8080
89
store: redis://redis:6379/9
910
backup_min: 1440 # 24h
@@ -15,4 +16,14 @@ x:
1516
prefix: /x/
1617
vcs: git
1718
import_path: golang.design/x/*
18-
repo_path: https://github.com/golang-design/*
19+
repo_path: https://github.com/golang-design/*
20+
godoc_host: https://pkg.go.dev/
21+
google_analytics: |
22+
<!-- Global site tag (gtag.js) - Google Analytics -->
23+
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
24+
<script>
25+
window.dataLayer = window.dataLayer || [];
26+
function gtag(){dataLayer.push(arguments);}
27+
gtag('js', new Date());
28+
gtag('config', 'UA-80889616-4');
29+
</script>

docker/Dockerfile

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
# All rights reserved. Use of this source code is governed
33
# by a MIT license that can be found in the LICENSE file.
44

5-
FROM golang:1.14.9
5+
FROM golang:1.15.3
66
WORKDIR /app
77
COPY . .
8-
RUN go build -o redir -ldflags \
9-
"-X main.Version=v$(git describe --always --tags) \
10-
-X main.BuildTime=$(date +%FT%T%z)"
11-
# @changkun: could use alpine instead, maybe in the future.
12-
EXPOSE 8080
13-
CMD ["/app/redir", "-s"]
8+
CMD ["/app/redir.app", "-s"]

docker/docker-compose.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@ services:
99
container_name: redir
1010
restart: always
1111
volumes:
12-
- ../data/backup:/data/backup
13-
image: golang-design/redir:latest
12+
- ../data/backup:/app/data/backup
13+
image: redir:latest
1414
environment:
1515
REDIR_CONF: ./data/container.yml
1616
cap_add:
1717
- SYS_PTRACE # for debugging
1818
ports:
19-
- "8080:8080"
19+
- "9123:8080"
2020
depends_on:
2121
- redis
2222
networks:
2323
redirnet:
24-
ipv4_address: 172.16.238.10
24+
ipv4_address: 172.16.1.10
2525
redis:
2626
container_name: redis
2727
restart: always
2828
volumes:
2929
- ../data/redis:/data
30-
image: redis:3.2
30+
image: redis:6.0
3131
ports:
3232
- "6379:6379"
3333
networks:
3434
redirnet:
35-
ipv4_address: 172.16.238.11
35+
ipv4_address: 172.16.1.11
3636
networks:
3737
redirnet:
3838
driver: bridge
3939
ipam:
4040
driver: default
4141
config:
42-
- subnet: 172.16.238.0/24
42+
- subnet: 172.16.1.0/24

public/stats.html

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<!-- Global site tag (gtag.js) - Google Analytics -->
5-
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
6-
<script>
7-
window.dataLayer = window.dataLayer || [];
8-
function gtag(){dataLayer.push(arguments);}
9-
gtag('js', new Date());
10-
gtag('config', 'UA-80889616-4');
11-
</script>
4+
{{ .GoogleAnalytics }}
125
<meta charset="UTF-8">
136
<meta name="viewport" content="width=device-width, initial-scale=1.0">
147
<title>golang.design link aliases</title>
@@ -64,7 +57,7 @@
6457
</head>
6558
<body>
6659
<div id="app" class="bg-dark">
67-
<h1>The golang.design Initiative</h1>
60+
<h1>{{ .Title }}</h1>
6861
<h5><a href="https://github.com/golang-design/redir">An alias request redirector</a></h5>
6962
<table class="table table-striped table-dark" style="width:100%">
7063
<tr>
@@ -76,7 +69,7 @@ <h5><a href="https://github.com/golang-design/redir">An alias request redirector
7669
<tr>
7770
<td>{{ .PV }}</td>
7871
<td> {{ .UV }}</td>
79-
<td><a href="https://golang.design/s/{{ .Alias }}">https://golang.design/s/{{ .Alias }}</a></td>
72+
<td><a href="{{ $.Host }}/s/{{ .Alias }}">{{ $.Host }}/s/{{ .Alias }}</a></td>
8073
</tr>
8174
{{end}}
8275
</table>

public/x.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
<!DOCTYPE html>
22
<html><head>
3-
<!-- Global site tag (gtag.js) - Google Analytics -->
4-
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
5-
<script>
6-
window.dataLayer = window.dataLayer || [];
7-
function gtag(){dataLayer.push(arguments);}
8-
gtag('js', new Date());
9-
gtag('config', 'UA-80889616-4');
10-
</script>
3+
{{ .GoogleAnalytics }}
114
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
125
<meta name="go-import" content="{{.ImportRoot}} {{.VCS}} {{.VCSRoot}}">
136
<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/{{.ImportRoot}}{{.Suffix}}">

redir.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ func main() {
3333
return
3434
}
3535

36-
if daemon == nil {
37-
flag.Usage()
38-
return
39-
}
4036
if *daemon {
4137
processServer()
4238
return
@@ -68,29 +64,24 @@ redir -op fetch -a alias fetch alias information
6864
}
6965

7066
func processCmd() {
71-
if fromfile != nil {
72-
fname := *fromfile
73-
if fname == "" {
74-
flag.Usage()
75-
return
76-
}
77-
78-
shortFile(fname)
67+
if *fromfile != "" {
68+
shortFile(*fromfile)
7969
return
8070
}
8171

82-
if operate == nil || !op(*operate).valid() {
72+
if !op(*operate).valid() {
8373
flag.Usage()
8474
return
8575
}
76+
8677
switch o := op(*operate); o {
8778
case opCreate, opUpdate:
88-
if alias == nil || link == nil || *alias == "" || *link == "" {
79+
if *alias == "" || *link == "" {
8980
flag.Usage()
9081
return
9182
}
9283
case opDelete, opFetch:
93-
if alias == nil || *alias == "" {
84+
if *alias == "" {
9485
flag.Usage()
9586
return
9687
}

0 commit comments

Comments
 (0)