From 868b49685825df218f2e9c5e67ad75b73c0a68a4 Mon Sep 17 00:00:00 2001 From: Tom Andrade Date: Mon, 18 Mar 2019 14:12:13 +0100 Subject: [PATCH] shortener initial version --- main.go | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..b4dc9ec --- /dev/null +++ b/main.go @@ -0,0 +1,148 @@ +package main + +import ( + "flag" + "log" + "math/rand" + "net/url" + "regexp" + "time" + + "github.com/gomodule/redigo/redis" + "github.com/hoisie/web" +) + +const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Base strings for RandStringBytesMaskImprSrc +const ( + letterIdxBits = 6 // 6 bits to represent a letter index + letterIdxMask = 1<= 0; { + if remain == 0 { + cache, remain = src.Int63(), letterIdxMax + } + if idx := int(cache & letterIdxMask); idx < len(letterBytes) { + b[i] = letterBytes[idx] + i-- + } + cache >>= letterIdxBits + remain-- + } + return string(b) +} + +func main() { + flag.StringVar(&domain, "domain", "localhost", "Domain to write to the URLs") + flag.StringVar(&redisServer, "redis", "localhost:6379", "ip/hostname of the redis server to connect") + flag.StringVar(&listenAddr, "addr", "localhost:8080", "Address to listen for connections") + flag.Parse() + web.Post("/", shortner) + web.Get("/(.*)", redirect) + log.Printf("Domain: %s, Redis: %s\n", domain, redisServer) + web.Run(listenAddr) +}