Skip to content

Commit

Permalink
all: add release build
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Mar 24, 2021
1 parent 93cd8e8 commit 0f538e4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ migrate/dump/ips
migrate/dump/alias
data/mongo
node_modules
build
24 changes: 22 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ IMAGE = redir
BINARY = redir
TARGET = -o $(BINARY)
BUILD_FLAGS = $(TARGET) -mod=vendor
GOOS = linux darwin
GOARCH = amd64 arm64

all:
go build $(BUILD_FLAGS)

$(GOOS): $(GOARCH)
echo $(VERSION) > internal/version/.version
for goarch in $^ ; do \
mkdir -p build/$(BINARY); \
cp internal/config/config.yml build/$(BINARY)/config.yml; \
CGO_ENABLED=0 GOARCH=$${goarch} GOOS=$@ go build -o build/$(BINARY)/$(BINARY) -mod=vendor; \
zip -r build/redir-$(VERSION)-$@-$${goarch}.zip build/$(BINARY); \
rm -rf build/$(BINARY); \
done
# restore
echo dev > internal/version/.version

run:
./$(BINARY) -s
dashboard:
Expand All @@ -21,8 +36,13 @@ up:
docker-compose -f docker/docker-compose.yml up -d
down:
docker-compose -f docker/docker-compose.yml down


release: $(GOOS)

clean:
rm -rf $(BINARY)
rm -rf $(BINARY) build
docker rmi -f $(shell docker images -f "dangling=true" -q) 2> /dev/null; true
docker rmi -f $(IMAGE):latest 2> /dev/null; true
.PHONY: run dashboard build up down clean

.PHONY: $(GOOS) $(GOARCH) run dashboard build up down clean
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# redir [![Latest relsease](https://img.shields.io/github/v/tag/changkun/redir?label=latest)](https:/changkun/redir/releases) [![PkgGoDev](https://pkg.go.dev/badge/changkun.de/x/redir)](https://pkg.go.dev/changkun.de/x/redir) ![](https://changkun.de/urlstat?mode=github&repo=changkun/redir)

Self-hosted link shortener and request redirector.
Self-hosted URL shortener.

## Features

Expand Down Expand Up @@ -45,7 +45,15 @@ The `redir` command offers server side operation feature from shell:
```
$ redir
usage: redir [-s] [-f <file>] [-d <file>] [-op <operator> -a <alias> -l <link> -p -vt <time>]
redir is a featured URL shortener. The redir server (run via '-s' option),
will connect to the default database address mongodb://localhost:27018.
It is possible to reconfig redir using an external configuration file.
See https://changkun.de/s/redir for more details.
Command line usage:
$ redir [-s] [-f <file>] [-d <file>] [-op <operator> -a <alias> -l <link> -p -vt <time>]
options:
-a string
Alias for a new link
Expand Down Expand Up @@ -91,9 +99,22 @@ redir -op delete -a changkun
Delete the alias from database
```

## Customization

You can configure redir using a configuration file.
The [default configuration](./internal/config/config.yml) is embedded into the binary.

Alternative configuration can be used to replace default config and specified in environtment variable `REDIR_CONF`, for example `REDIR_CONF=/path/to/config.yml redir -s` to run the redir server under given configuration.

## Deployment

### Build
### Download Pre-Builds

Please check the [release](https:/changkun/redir/releases) page.

### Build from Source

You need install [Go](https://golang.org) to build the `redir` command.

Build everything into a single native binary:

Expand All @@ -111,7 +132,7 @@ $ docker network create traefik_proxy
$ make dashboard && make build && make up
```

### APIs
## APIs

All possible routers: `/s`, `/r`, and `/x`. The `/s` is the most
complicated router because we are limited to use these prefixes
Expand All @@ -120,7 +141,7 @@ different routers. The prefix is configurable).

Thus, all kinds of data, pages, static files are served under this router.

#### GET /s
### GET /s

The GET request query parameters of `/s` and `/r` are listed as follows:

Expand All @@ -138,7 +159,7 @@ The GET request query parameters of `/s` and `/r` are listed as follows:
- `t0`, start time
- `t1`, end time

#### POST /s
### POST /s

The POST request body of `/s` and `/r` is in the following format:

Expand All @@ -155,12 +176,6 @@ The POST request body of `/s` and `/r` is in the following format:
}
```

### Configuration

The [default configuration](./config.yml) is embedded into the binary.

Alternative configuration can be used to replace default config and specified in environtment variable `REDIR_CONF`, for example `REDIR_CONF=/path/to/config.yml redir -s` to run the redir server under given configuration.

## Who is using this service?

Existing famous link shortener services, such as `bitly`, `tinyurl`, etc.,
Expand Down
1 change: 1 addition & 0 deletions internal/version/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev
6 changes: 6 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package version

import _ "embed"

//go:embed .version
var Version string
18 changes: 15 additions & 3 deletions redir.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"log"
"net/http"
"os"
"runtime"
"time"

"changkun.de/x/redir/internal/config"
"changkun.de/x/redir/internal/models"
"changkun.de/x/redir/internal/short"
"changkun.de/x/redir/internal/utils"
"changkun.de/x/redir/internal/version"
)

var (
Expand All @@ -31,10 +33,20 @@ var (
)

func usage() {
fmt.Fprintf(os.Stderr,
`usage: redir [-s] [-f <file>] [-d <file>] [-op <operator> -a <alias> -l <link> -p -vt <time>]
fmt.Fprintf(os.Stderr, `redir is a featured URL shortener. The redir server (run via '-s' option),
will connect to the default database address %s.
It is possible to reconfig redir using an external configuration file.
See https://changkun.de/s/redir for more details.
Version: %s
GoVersion: %s
Command line usage:
$ redir [-s] [-f <file>] [-d <file>] [-op <operator> -a <alias> -l <link> -p -vt <time>]
options:
`)
`, config.Conf.Store, version.Version, runtime.Version())
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
examples:
Expand Down
4 changes: 3 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ func newServer(ctx context.Context) *server {

db, err := db.NewStore(context.Background(), config.Conf.Store)
if err != nil {
log.Fatalf("cannot establish connection to database: %v", err)
log.Fatalf("cannot establish connection to %s, details: \n\n%v",
config.Conf.Store, err)
}
log.Printf("connected to %s", config.Conf.Store)
return &server{db: db, cache: cache.NewLRU(true)}
}

Expand Down

0 comments on commit 0f538e4

Please sign in to comment.