diff --git a/Makefile b/Makefile index 2bf3f00..85afbd2 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index f301762..570d4da 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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 @@ -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."git@github.com:".insteadOf "https://github.com/"` +4. add `export GOPRIVATE=golang.design/x` to your bash profile (e.g. `.zshrc`). ## License diff --git a/config.go b/config.go index c60e13d..7e8f691 100644 --- a/config.go +++ b/config.go @@ -21,6 +21,7 @@ var ( ) type config struct { + Title string `yaml:"title"` Host string `yaml:"host"` Addr string `yaml:"addr"` Store string `yaml:"store"` @@ -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() { diff --git a/config.yml b/config.yml index 643e547..c5b6011 100644 --- a/config.yml +++ b/config.yml @@ -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 @@ -15,4 +16,14 @@ x: prefix: /x/ vcs: git import_path: golang.design/x/* - repo_path: https://github.com/golang-design/* \ No newline at end of file + repo_path: https://github.com/golang-design/* + godoc_host: https://pkg.go.dev/ +google_analytics: | + + + diff --git a/data/container.yml b/data/container.yml index 372348b..5cda2fa 100644 --- a/data/container.yml +++ b/data/container.yml @@ -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 @@ -15,4 +16,14 @@ x: prefix: /x/ vcs: git import_path: golang.design/x/* - repo_path: https://github.com/golang-design/* \ No newline at end of file + repo_path: https://github.com/golang-design/* + godoc_host: https://pkg.go.dev/ +google_analytics: | + + + diff --git a/docker/Dockerfile b/docker/Dockerfile index 5ccca69..3abee3a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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"] \ No newline at end of file +CMD ["/app/redir.app", "-s"] \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 05a5af5..b9c28b4 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -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 \ No newline at end of file + - subnet: 172.16.1.0/24 \ No newline at end of file diff --git a/public/stats.html b/public/stats.html index ad69679..745120f 100644 --- a/public/stats.html +++ b/public/stats.html @@ -1,14 +1,7 @@ - - - + {{ .GoogleAnalytics }} golang.design link aliases @@ -64,7 +57,7 @@
-

The golang.design Initiative

+

{{ .Title }}

An alias request redirector
@@ -76,7 +69,7 @@
An alias request redirector
- + {{end}}
{{ .PV }} {{ .UV }}https://golang.design/s/{{ .Alias }}{{ $.Host }}/s/{{ .Alias }}
diff --git a/public/x.html b/public/x.html index c3d2db5..a5c1992 100644 --- a/public/x.html +++ b/public/x.html @@ -1,13 +1,6 @@ - - - +{{ .GoogleAnalytics }} diff --git a/redir.go b/redir.go index 7f860f9..e791469 100644 --- a/redir.go +++ b/redir.go @@ -33,10 +33,6 @@ func main() { return } - if daemon == nil { - flag.Usage() - return - } if *daemon { processServer() return @@ -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 } diff --git a/short.go b/short.go index 56530e9..b418fa3 100644 --- a/short.go +++ b/short.go @@ -10,6 +10,7 @@ import ( "encoding/json" "errors" "fmt" + "html/template" "io/ioutil" "log" "net" @@ -190,7 +191,10 @@ 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) { @@ -198,8 +202,13 @@ func (s *server) stats(ctx context.Context, w http.ResponseWriter) (retErr error 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 { diff --git a/x.go b/x.go index 1edb949..67b7d62 100644 --- a/x.go +++ b/x.go @@ -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 @@ -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+"/") { @@ -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)