Compare commits

...

19 Commits

Author SHA1 Message Date
d00b9e1c9e Update 'README.md' 2022-09-27 15:05:16 +00:00
e4ca15f0c2 Update 'README.md' 2022-08-28 21:36:38 +00:00
8f1cb33eba Update 'README.md' 2022-08-28 13:24:55 +00:00
9d68dfc3ba Updated README.md 2022-08-28 15:23:17 +02:00
0110d6994b Return JSON-encoded errors 2022-08-28 15:07:46 +02:00
9da60fa4e7 Added link info endpoint 2022-08-28 15:01:02 +02:00
0944a14a97 Merge branch 'main' of ssh://git.cesium.pw:2335/niku/5feet11 2022-08-28 13:46:02 +02:00
a74ba3baf3 Changed TZ and base image of Dockerfile 2022-08-28 12:45:00 +02:00
363c8405e3 Changed name of dev docker-compose 2022-08-28 12:27:01 +02:00
9aee40d3b1 Changed shortener endpoint to /api/v1/links 2022-08-28 11:45:07 +02:00
4539c926ec Added CreatedAt field to URL model 2022-08-27 17:22:44 +02:00
131cc2640a Obey supplied expiration time ShortenReq.ExpiresAfter 2022-08-27 17:02:15 +02:00
0e8dccb9d0 Fixed invalid collumn selector in ExpandUrl 2022-08-27 16:56:49 +02:00
6d36b8f8ea Removed non-existent scylla config 2022-08-27 16:19:56 +02:00
6db423d170 Added boilerplate for Prometheus 2022-08-27 16:04:27 +02:00
092cbcfdd1 Globally renamed property RedirectUrl to LongUrl 2022-08-27 14:16:23 +02:00
590906d555 Globally renamed Snowflake to ID 2022-08-27 14:05:59 +02:00
03d845b00d Removed secret field from types.ShortenReq 2022-08-27 14:02:33 +02:00
bf770ee0db Added air 2022-08-27 14:00:41 +02:00
22 changed files with 325 additions and 57 deletions

37
.air.toml Normal file
View File

@@ -0,0 +1,37 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
kill_delay = "0s"
log = "build-errors.log"
send_interrupt = false
stop_on_error = true
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
time = false
[misc]
clean_on_exit = false
[screen]
clear_on_rebuild = false

View File

@@ -0,0 +1,6 @@
groups:
- name: DemoAlerts
rules:
- alert: InstanceDown
expr: up{job="services"} < 1
for: 5m

View File

@@ -0,0 +1,17 @@
global:
scrape_interval: 30s
scrape_timeout: 10s
rule_files:
- alert.yml
scrape_configs:
- job_name: services
metrics_path: /metrics
static_configs:
- targets:
- 'prometheus:9090'
- job_name: 'file_ds'
file_sd_configs:
- files:
- targets.json

View File

@@ -0,0 +1,10 @@
[
{
"targets": [
"api:9091"
],
"labels": {
"job": "5f11-api"
}
}
]

1
.gitignore vendored
View File

@@ -23,3 +23,4 @@ go.work
# Artefacts # Artefacts
5feet11 5feet11
tmp

View File

@@ -8,30 +8,41 @@ info (
type ( type (
ExpandReq { ExpandReq {
Snowflake string `path:"snowflake"` ID string `path:"id"`
} }
ExpandResp { ExpandResp {
RedirectUrl string `json:"redirectUrl"` LongUrl string `json:"longUrl"`
} }
) )
type ( type (
ShortenReq { ShortenReq {
RedirectUrl string `json:"redirectUrl"` LongUrl string `json:"longUrl"`
Secret string `json:"secret,optional"` ExpiresAfter int64 `json:"expiresAfter,optional"`
ExpiresIn int64 `json:"expiresIn,optional"`
} }
ShortenResp { ShortenResp {
Id string `json:"id"` ID string `json:"id"`
}
)
type (
GetLinkResp {
ID string `json:"id"`
LongUrl string `json:"longUrl"`
CreatedAt string `json:"createdAt"`
Lifespan int64 `json:"lifespan"`
} }
) )
service fivefeeteleven-api { service fivefeeteleven-api {
@handler ExpandUrl @handler ExpandUrl
get /:snowflake(ExpandReq) returns(ExpandResp) get /:id(ExpandReq) returns(ExpandResp)
@handler ShortenUrl @handler ShortenUrl
post /redirect(ShortenReq) returns(ShortenResp) post /api/v1/links(ShortenReq) returns(ShortenResp)
}
@handler GetLink
get /api/v1/links/:id(ExpandReq) returns(GetLinkResp)
}

View File

@@ -1,4 +1,4 @@
FROM golang:alpine AS builder FROM golang:1.19-alpine AS builder
LABEL stage=gobuilder LABEL stage=gobuilder
@@ -19,8 +19,8 @@ RUN go build -ldflags="-s -w" -o /app/5feet11 .
FROM scratch FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Europe/Amsterdam /usr/share/zoneinfo/Europe/Amsterdam COPY --from=builder /usr/share/zoneinfo/Etc/UTC /usr/share/zoneinfo/Etc/UTC
ENV TZ Europe/Amsterdam ENV TZ Etc/UTC
WORKDIR /app WORKDIR /app
COPY --from=builder /app/5feet11 /app/5feet11 COPY --from=builder /app/5feet11 /app/5feet11

View File

@@ -1,15 +1,39 @@
# 5feet11 # 5feet11
A URL shortener that generates links even shorter than 5'11. A scalable URL shortener.
## Prepare
- ScyllaDB
- Prometheus*
*: Optional
## Run
Update the config at `etc/fivefeeteleven-api.yaml`.
```sh
# Docker
docker run -it $(docker build -q .)
# Compile
make build && ./5feet11
```
## Usage ## Usage
```bash ```bash
# Shorten a URL # Shorten a URL
curl --header "Content-Type: application/json" \ curl -X POST -H "Content-Type: application/json" \
--request POST \ -d '{"longUrl":"https://news.ycombinator.com"}' \
--data '{"redirectUrl":"https://news.ycombinator.com"}' \ http://localhost:5111/api/v1/links
http://localhost:5111/redirect
# Shorten a URL that expires after N seconds
curl -X POST -H "Content-Type: application/json" \
-d '{"longUrl":"https://news.ycombinator.com", "expiresAfter": 30}' \
http://localhost:5111/api/v1/links
# Expand the URL # Expand the URL
curl -iL http://localhost:5111/{id} curl -iL http://localhost:5111/{id}
# Get information of a specific URL
curl -iL http://localhost:5111/api/v1/links/{id}
``` ```

22
docker-compose.dev.yml Normal file
View File

@@ -0,0 +1,22 @@
version: '3'
services:
scylla:
image: scylladb/scylla
ports:
- 7000:7000
- 7001:7001
- 9042:9042
- 9160:9160
- 10000:10000
prometheus:
image: prom/prometheus:v2.30.3
ports:
- 9090:9090
volumes:
- .docker/prometheus:/etc/prometheus
- prometheus-data:/prometheus
command: --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml
volumes:
prometheus-data:

View File

@@ -1,11 +0,0 @@
version: '3'
services:
scylla:
image: scylladb/scylla
ports:
- "7000:7000"
- "7001:7001"
- "9042:9042"
- "9160:9160"
- "10000:10000"

View File

@@ -4,4 +4,8 @@ Host: 0.0.0.0
Port: 5111 Port: 5111
ScyllaDB: ScyllaDB:
Hosts: Hosts:
- "localhost" - localhost
# Prometheus:
# Host: 0.0.0.0
# Port: 9091
# Path: /metrics

View File

@@ -2,14 +2,17 @@ package main
import ( import (
"flag" "flag"
"net/http"
"5feet11/internal/config" "5feet11/internal/config"
"5feet11/internal/errorx"
"5feet11/internal/handler" "5feet11/internal/handler"
"5feet11/internal/svc" "5feet11/internal/svc"
"github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest" "github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
) )
var configFile = flag.String("f", "etc/fivefeeteleven-api.yaml", "the config file") var configFile = flag.String("f", "etc/fivefeeteleven-api.yaml", "the config file")
@@ -26,6 +29,15 @@ func main() {
ctx := svc.NewServiceContext(c) ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx) handler.RegisterHandlers(server, ctx)
httpx.SetErrorHandler(func(err error) (int, interface{}) {
switch e := err.(type) {
case *errorx.CodeError:
return http.StatusBadRequest, e.Data()
default:
return http.StatusInternalServerError, nil
}
})
logx.Infof("Starting server at http://%s:%d", c.Host, c.Port) logx.Infof("Starting server at http://%s:%d", c.Host, c.Port)
server.Start() server.Start()
} }

View File

@@ -1,16 +1,21 @@
package db package db
import "github.com/scylladb/gocqlx/v2/table" import (
"time"
"github.com/scylladb/gocqlx/v2/table"
)
var UrlTable = table.New(table.Metadata{ var UrlTable = table.New(table.Metadata{
Name: "fivefeeteleven.urls", Name: "fivefeeteleven.urls",
Columns: []string{"id", "redirect_url", "secret"}, Columns: []string{"id", "long_url", "lifespan", "created_at"},
PartKey: []string{"id"}, PartKey: []string{"id"},
SortKey: []string{}, SortKey: []string{},
}) })
type UrlModel struct { type UrlModel struct {
Id string ID string
RedirectUrl string LongUrl string
Secret *string Lifespan int64
CreatedAt time.Time
} }

View File

@@ -15,8 +15,9 @@ func Seed(session gocqlx.Session) error {
err = session.ExecStmt(` err = session.ExecStmt(`
CREATE TABLE IF NOT EXISTS fivefeeteleven.urls ( CREATE TABLE IF NOT EXISTS fivefeeteleven.urls (
id text PRIMARY KEY, id text PRIMARY KEY,
redirect_url text, long_url text,
secret text created_at timestamp,
lifespan bigint
)`) )`)
if err != nil { if err != nil {

32
internal/errorx/http.go Normal file
View File

@@ -0,0 +1,32 @@
package errorx
const defaultCode = 1001
type CodeError struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
type CodeErrorResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
func NewCodeError(code int, msg string) error {
return &CodeError{Code: code, Msg: msg}
}
func NewDefaultError(msg string) error {
return NewCodeError(defaultCode, msg)
}
func (e *CodeError) Error() string {
return e.Msg
}
func (e *CodeError) Data() *CodeErrorResponse {
return &CodeErrorResponse{
Code: e.Code,
Msg: e.Msg,
}
}

View File

@@ -23,7 +23,7 @@ func ExpandUrlHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
if err != nil { if err != nil {
httpx.Error(w, err) httpx.Error(w, err)
} else { } else {
http.Redirect(w, r, resp.RedirectUrl, http.StatusTemporaryRedirect) http.Redirect(w, r, resp.LongUrl, http.StatusTemporaryRedirect)
} }
} }
} }

View File

@@ -0,0 +1,28 @@
package handler
import (
"net/http"
"5feet11/internal/logic"
"5feet11/internal/svc"
"5feet11/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
func GetLinkHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ExpandReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}
l := logic.NewGetLinkLogic(r.Context(), svcCtx)
resp, err := l.GetLink(&req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.OkJson(w, resp)
}
}
}

View File

@@ -14,14 +14,19 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
[]rest.Route{ []rest.Route{
{ {
Method: http.MethodGet, Method: http.MethodGet,
Path: "/:snowflake", Path: "/:id",
Handler: ExpandUrlHandler(serverCtx), Handler: ExpandUrlHandler(serverCtx),
}, },
{ {
Method: http.MethodPost, Method: http.MethodPost,
Path: "/redirect", Path: "/api/v1/links",
Handler: ShortenUrlHandler(serverCtx), Handler: ShortenUrlHandler(serverCtx),
}, },
{
Method: http.MethodGet,
Path: "/api/v1/links/:id",
Handler: GetLinkHandler(serverCtx),
},
}, },
) )
} }

View File

@@ -2,9 +2,9 @@ package logic
import ( import (
"context" "context"
"fmt"
"5feet11/internal/db" "5feet11/internal/db"
"5feet11/internal/errorx"
"5feet11/internal/svc" "5feet11/internal/svc"
"5feet11/internal/types" "5feet11/internal/types"
@@ -26,20 +26,20 @@ func NewExpandUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExpandU
} }
func (l *ExpandUrlLogic) ExpandUrl(req *types.ExpandReq) (resp *types.ExpandResp, err error) { func (l *ExpandUrlLogic) ExpandUrl(req *types.ExpandReq) (resp *types.ExpandResp, err error) {
queryUrl := db.UrlTable.SelectBuilder("redirect_url").Query(l.svcCtx.DB) queryUrl := db.UrlTable.SelectBuilder("long_url").Query(l.svcCtx.DB)
queryUrl.BindStruct(db.UrlModel{Id: req.Snowflake}) queryUrl.BindStruct(db.UrlModel{ID: req.ID})
var urls []db.UrlModel var urls []db.UrlModel
if err := queryUrl.Select(&urls); err != nil { if err := queryUrl.Select(&urls); err != nil {
return nil, err return nil, errorx.NewDefaultError(err.Error())
} }
if len(urls) != 1 { if len(urls) != 1 {
return nil, fmt.Errorf("no URL found") return nil, errorx.NewDefaultError("no URL found")
} }
resp = &types.ExpandResp{ resp = &types.ExpandResp{
RedirectUrl: urls[0].RedirectUrl, LongUrl: urls[0].LongUrl,
} }
return resp, err return resp, err

View File

@@ -0,0 +1,49 @@
package logic
import (
"context"
"5feet11/internal/db"
"5feet11/internal/errorx"
"5feet11/internal/svc"
"5feet11/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetLinkLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetLinkLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLinkLogic {
return &GetLinkLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetLinkLogic) GetLink(req *types.ExpandReq) (resp *types.GetLinkResp, err error) {
queryUrl := db.UrlTable.SelectQuery(l.svcCtx.DB)
queryUrl.BindStruct(db.UrlModel{ID: req.ID})
var urls []db.UrlModel
if err := queryUrl.Select(&urls); err != nil {
return nil, errorx.NewDefaultError(err.Error())
}
if len(urls) != 1 {
return nil, errorx.NewDefaultError("no URL found")
}
resp = &types.GetLinkResp{
ID: urls[0].ID,
LongUrl: urls[0].LongUrl,
CreatedAt: urls[0].CreatedAt.String(),
Lifespan: urls[0].Lifespan,
}
return resp, err
}

View File

@@ -5,6 +5,7 @@ import (
"time" "time"
"5feet11/internal/db" "5feet11/internal/db"
"5feet11/internal/errorx"
"5feet11/internal/svc" "5feet11/internal/svc"
"5feet11/internal/types" "5feet11/internal/types"
@@ -27,20 +28,28 @@ func NewShortenUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Shorte
func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.ShortenResp, err error) { func (l *ShortenUrlLogic) ShortenUrl(req *types.ShortenReq) (resp *types.ShortenResp, err error) {
id := l.svcCtx.Snowflake.Generate().Base58() id := l.svcCtx.Snowflake.Generate().Base58()
insertBuilder := db.UrlTable.InsertBuilder()
insertUrl := db.UrlTable.InsertBuilder().TTL(30 * time.Second).Query(l.svcCtx.DB) logx.Info(req.ExpiresAfter)
if req.ExpiresAfter != 0 {
insertBuilder.TTL(time.Second * time.Duration(req.ExpiresAfter))
}
insertUrl := insertBuilder.Query(l.svcCtx.DB)
insertUrl.BindStruct(db.UrlModel{ insertUrl.BindStruct(db.UrlModel{
Id: id, ID: id,
RedirectUrl: req.RedirectUrl, LongUrl: req.LongUrl,
Secret: &req.Secret, Lifespan: req.ExpiresAfter,
CreatedAt: time.Now(),
}) })
if err := insertUrl.ExecRelease(); err != nil { if err := insertUrl.ExecRelease(); err != nil {
return nil, err return nil, errorx.NewDefaultError(err.Error())
} }
resp = &types.ShortenResp{ resp = &types.ShortenResp{
Id: id, ID: id,
} }
return resp, nil return resp, nil
} }

View File

@@ -2,19 +2,25 @@
package types package types
type ExpandReq struct { type ExpandReq struct {
Snowflake string `path:"snowflake"` ID string `path:"id"`
} }
type ExpandResp struct { type ExpandResp struct {
RedirectUrl string `json:"redirectUrl"` LongUrl string `json:"longUrl"`
} }
type ShortenReq struct { type ShortenReq struct {
RedirectUrl string `json:"redirectUrl"` LongUrl string `json:"longUrl"`
Secret string `json:"secret,optional"` ExpiresAfter int64 `json:"expiresAfter,optional"`
ExpiresIn int64 `json:"expiresIn,optional"`
} }
type ShortenResp struct { type ShortenResp struct {
Id string `json:"id"` ID string `json:"id"`
}
type GetLinkResp struct {
ID string `json:"id"`
LongUrl string `json:"longUrl"`
CreatedAt string `json:"createdAt"`
Lifespan int64 `json:"lifespan"`
} }