I'm bad at commits...

This commit is contained in:
Tom Andrade 2020-03-26 11:41:41 +01:00
parent 5e119a1c88
commit 430d388c57
Signed by: wolvie
GPG Key ID: 31AAB07872E82669

412
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log" "log"
@ -12,7 +13,6 @@ import (
"net/url" "net/url"
"os" "os"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time" "time"
@ -22,20 +22,6 @@ import (
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
) )
type config struct {
addr string
domain string
dumpFile string
path string
proto string
hostSuf string
listenAddr string
port int
urlSize int
version bool
templates *template.Template
}
type body struct { type body struct {
FullHeader bool FullHeader bool
IsGhost bool IsGhost bool
@ -52,74 +38,19 @@ const (
letterIdxBits = 6 // 6 bits to represent a letter index letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
appVersion = "1.2.1" appVersion = "1.3.0"
) )
var ( var (
cfg config // tiny entropy pool
src = rand.NewSource(time.Now().UnixNano()) src = rand.NewSource(time.Now().UnixNano())
pool = cache.New(240*time.Hour, 1*time.Hour) // KV memory DB
pool *cache.Cache
// Error codes
errBadRequest = fmt.Errorf("Bad Request")
errNotFound = fmt.Errorf("Not Found")
) )
func init() {
flag.StringVar(&cfg.addr, "addr", "localhost", "Address to listen for connections")
flag.StringVar(&cfg.domain, "domain", "localhost", "Domain to write to the URLs")
flag.StringVar(&cfg.dumpFile, "dump", "urls.json", "Path to the file to dump the kv db")
flag.StringVar(&cfg.path, "path", "", "Path to the base URL (https://localhost/PATH/... remember to append a / at the end")
flag.StringVar(&cfg.proto, "proto", "https", "proto to the base URL (HTTPS://localhost/path/... no real https here just to set the url (for like a proxy offloading https")
flag.IntVar(&cfg.port, "port", 8080, "Port to listen for connections")
flag.IntVar(&cfg.urlSize, "urlsize", 10, "Define the size of the shortened String, default 10")
flag.BoolVar(&cfg.version, "v", false, "prints current version")
flag.Parse()
if cfg.version {
log.SetFlags(0)
log.Println(appVersion)
os.Exit(0)
}
if cfg.port > 65535 || cfg.port < 1 {
log.Fatalln("Invalid port number")
}
if cfg.path != "" && !strings.HasSuffix(cfg.path, "/") {
cfg.path = cfg.path + "/"
}
if cfg.port != 80 && cfg.proto == "http" {
cfg.hostSuf = ":" + strconv.Itoa(cfg.port) + "/"
} else if cfg.port != 443 && cfg.proto == "https" {
cfg.hostSuf = ":" + strconv.Itoa(cfg.port) + "/"
} else if cfg.port == 443 || cfg.port == 80 {
cfg.hostSuf = "/"
}
ip := net.ParseIP(cfg.addr)
if ip != nil {
cfg.listenAddr = ip.String() + ":" + strconv.Itoa(cfg.port)
} else {
if govalidator.IsDNSName(cfg.addr) {
cfg.listenAddr = cfg.addr + ":" + strconv.Itoa(cfg.port)
} else {
log.Fatalln("Invalid ip address")
}
}
if !govalidator.IsDNSName(cfg.domain) {
log.Fatalln("Invalid domain address")
}
cfg.templates = template.Must(template.ParseFiles("templates/response.html"))
}
func (c config) index(w http.ResponseWriter, r *http.Request) {
b := body{
HasForm: true,
Line1: "Welcome to Short, the simple URL shortener,",
Line2: "Type an URL below to shorten it",
}
c.templates.Execute(w, b)
}
// get executes the GET command // get executes the GET command
func get(key string) (string, bool) { func get(key string) (string, bool) {
value, status := pool.Get(key) value, status := pool.Get(key)
@ -134,76 +65,42 @@ func set(key, suffix string) {
pool.Set(suffix, key, 0) pool.Set(suffix, key, 0)
} }
// redirect reads the key from the requests url (GET /key) searches the // redirect receives a key searches the kv database for it and if
// kv database for it and if found redirects the user to value, if not // found returns the value, or a error if not found
// found return a 404. func redirect(k string) (string, error) {
func (c config) redirect(w http.ResponseWriter, r *http.Request) {
vals := mux.Vars(r)
key := vals["key"]
b := body{
FullHeader: true,
IsGhost: true,
HasForm: true,
H1: "404",
H3: "page not found",
Line1: "Boo, looks like this ghost stole this page!",
Line2: "But you can type an URL below to shorten it",
}
if c.path != "" {
key = strings.Replace(key, c.path, "", 1)
}
rgx, _ := regexp.Compile("[a-zA-Z0-9]+") rgx, _ := regexp.Compile("[a-zA-Z0-9]+")
key = rgx.FindString(key) key := rgx.FindString(k)
key, status := get(key) key, status := get(key)
if !status { if !status {
w.WriteHeader(http.StatusNotFound) return "", errNotFound
c.templates.Execute(w, b)
return
} }
u, _ := url.Parse(key) u, _ := url.Parse(key)
if u.Scheme == "" { if u.Scheme == "" {
u.Scheme = "https" u.Scheme = "https"
} }
http.Redirect(w, r, u.String(), http.StatusFound) return u.String(), nil
} }
// shortner reads url from a POST request, validates the url, generate a // shortener receive a url, validates the url, generate a random suffix string
// random suffix string of urlSize size, checks if the suffix string is // of urlSize size, checks if the suffix string is ensure on the kv database
// unique on the kv database and if not unique regenerates it and checks again, // and then writes the kv pair (suffix, url) to the database, returning the suffix
// then if writes the kv pair suffix, url to the database and return the func shortener(u string, s int) (string, error) {
// shortened url to the user var su string
func (c config) shortner(w http.ResponseWriter, r *http.Request) { if !govalidator.IsURL(u) {
if !govalidator.IsURL(r.FormValue("url")) { return su, errBadRequest
b := body{
FullHeader: true,
IsGhost: true,
HasForm: true,
H1: "400",
H3: "bad request",
Line1: "Boo, looks like this ghost stole this page!",
Line2: "But you can type an URL below to shorten it",
} }
w.WriteHeader(http.StatusBadRequest) pu, _ := url.Parse(u)
c.templates.Execute(w, b)
return
}
u, _ := url.Parse(r.FormValue("url"))
suffix := randStringBytesMaskImprSrc(c.urlSize)
for { for {
_, status := get(suffix) su = randStringBytesMaskImprSrc(s)
_, status := get(su)
if !status { if !status {
break break
} }
suffix = randStringBytesMaskImprSrc(c.urlSize)
} }
set(u.String(), suffix)
shortend := c.proto + "://" + c.domain + c.hostSuf + c.path + suffix set(pu.String(), su)
b := body{ return su, nil
IsLink: true,
Line1: shortend,
}
c.templates.Execute(w, b)
} }
// randStringBytesMaskImprSrc Generate random string of n size // randStringBytesMaskImprSrc Generate random string of n size
@ -224,108 +121,211 @@ func randStringBytesMaskImprSrc(n int) string {
return string(b) return string(b)
} }
// internalError receives a http.ResponseWriter, msg and error and func internalError(msg string, err error) body {
// return a internal error page with http code 500 to the user log.Println(err)
func (c config) internalError(w http.ResponseWriter, msg string, err error) { return body{
b := body{
FullHeader: true, FullHeader: true,
IsGhost: true, IsGhost: true,
HasForm: true, HasForm: true,
H1: "500", H1: "500",
H3: "internal erver error", H3: msg,
Line1: "Boo, the ghost is broken :(", Line1: "Boo, the ghost is broken :(",
Line2: "His last words where: " + err.Error(), Line2: "His last words where: " + err.Error(),
} }
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
c.templates.Execute(w, b)
} }
// itemsCount returns the number of kv pairs on the in meomry database // loadFromFile loads kv pairs from the dumpFile json to the in memory database
func (c config) itemsCount(w http.ResponseWriter, r *http.Request) { func loadFromFile(file string, e, c int) (int, error) {
w.Write( dumpObj := make(map[string]cache.Item)
[]byte( jsonFile, err := ioutil.ReadFile(file)
strconv.Itoa(
pool.ItemCount(),
),
),
)
}
// itemsDump returns a json with all the kv pairs on the in memory database
func (c config) itemsDump(w http.ResponseWriter, r *http.Request) {
dumpObj, err := json.Marshal(
pool.Items(),
)
if err != nil { if err != nil {
c.internalError(w, "Unable to dump key value db: ", err) return 0, err
} }
w.Write(
[]byte(dumpObj),
)
}
// itemsFromFile loads kv pairs from the dumpFile json to the in memory database err = json.Unmarshal([]byte(jsonFile), &dumpObj)
func (c config) itemsFromFile(w http.ResponseWriter, r *http.Request) {
jsonFile, err := ioutil.ReadFile(c.dumpFile)
var dumpObj map[string]cache.Item
json.Unmarshal([]byte(jsonFile), &dumpObj)
if err != nil { if err != nil {
c.internalError(w, "Cannot open file "+c.dumpFile+": ", err) return 0, err
return
} }
pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj)
b := body{ pool = cache.NewFrom(time.Duration(e)*time.Hour, time.Duration(c)*time.Hour, dumpObj)
Line1: "Imported " + strconv.Itoa(len(dumpObj)) + " items to the DB", return len(dumpObj), err
}
c.templates.Execute(w, b)
} }
// itemsFromPost loads kv pairs from a json POST to the in memory database // itemsFromPost loads kv pairs from a json POST to the in memory database
func (c config) itemsFromPost(w http.ResponseWriter, r *http.Request) { func loadFromJSON(j []byte, e, c int) (int, error) {
decoder := json.NewDecoder(r.Body) dumpObj := make(map[string]cache.Item)
var dumpObj map[string]cache.Item err := json.Unmarshal(j, &dumpObj)
err := decoder.Decode(&dumpObj)
if err != nil { if err != nil {
c.internalError(w, "Cannot parse JSON: ", err) return 0, err
return
} }
pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj)
b := body{ pool = cache.NewFrom(time.Duration(e)*time.Hour, time.Duration(c)*time.Hour, dumpObj)
Line1: "Imported " + strconv.Itoa(len(dumpObj)) + " items to the DB", return len(dumpObj), nil
}
c.templates.Execute(w, b)
} }
// itemsDumpToFile dumps the kv pairs from the in memory database to the dumpFile // dumpDbToFile dumps the kv pairs from the in memory database to file
func (c config) itemsDumpToFile(w http.ResponseWriter, r *http.Request) { func dumpDbTOFile(file string) (int, error) {
dumpObj, _ := json.Marshal( i := pool.Items()
pool.Items(), dumpObj, _ := json.Marshal(i)
) return len(i), ioutil.WriteFile(file, dumpObj, 0644)
err := ioutil.WriteFile(c.dumpFile, dumpObj, 0644)
if err != nil {
c.internalError(w, "Failed to open json file: ", err)
return
}
b := body{
Line1: "Imported " + "Dump writen to: " + c.dumpFile,
}
c.templates.Execute(w, b)
} }
func main() { func main() {
var (
addr = flag.String("addr", "localhost", "Address to listen for connections")
domain = flag.String("domain", "localhost", "Domain to write to the URLs")
dumpFile = flag.String("dump", "urls.json", "Path to the file to dump the kv db")
path = flag.String("path", "", "Path to the base URL (https://localhost/PATH/...")
proto = flag.String("proto", "https", "proto to the base URL (HTTPS://localhost/path/... no real https here just to set the url (for like a proxy offloading https")
port = flag.Int("port", 8080, "Port to listen for connections")
urlSize = flag.Int("urlsize", 10, "Define the size of the shortened String, default 10")
exp = flag.Int("exp", 240, "Default expiration time in hours, default 240")
cleanup = flag.Int("cleanup", 1, "Cleanup interval in hours, default 1")
version = flag.Bool("v", false, "prints current version")
listenAddr string
)
flag.Parse()
if *version {
fmt.Println(appVersion)
os.Exit(0)
}
if *port > 65535 || *port < 1 {
log.Fatalln("Invalid port number")
}
if *path != "" && !strings.HasSuffix(*path, "/") {
*path = *path + "/"
}
ip := net.ParseIP(*addr)
if ip != nil {
listenAddr = fmt.Sprintf("%s:%v", ip.String(), *port)
} else {
if govalidator.IsDNSName(*addr) {
listenAddr = fmt.Sprintf("%s:%v", *addr, *port)
} else {
log.Fatalln("Invalid ip address")
}
}
if !govalidator.IsDNSName(*domain) {
log.Fatalln("Invalid domain address")
}
pool = cache.New(time.Duration(*exp)*time.Hour, time.Duration(*cleanup)*time.Hour)
t := template.Must(template.ParseFiles("templates/response.html"))
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", cfg.index).Methods("GET") // Index
r.HandleFunc("/", cfg.shortner).Methods("POST") r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
r.HandleFunc("/{key}", cfg.redirect).Methods("GET") t.Execute(w, body{
r.HandleFunc("/v1/dumpToFile", cfg.itemsDumpToFile).Methods("GET") HasForm: true,
r.HandleFunc("/v1/fromFile", cfg.itemsFromFile).Methods("GET") Line1: "Welcome to Short, the simple URL shortener,",
r.HandleFunc("/v1/count", cfg.itemsCount).Methods("GET") Line2: "Type an URL below to shorten it",
r.HandleFunc("/v1/dump", cfg.itemsDump).Methods("GET") })
r.HandleFunc("/v1/fromPost", cfg.itemsFromPost).Methods("POST") }).Methods("GET")
log.Printf("Domain: %s, URL Proto: %s, Listen Address: %s\n", cfg.domain, cfg.proto, cfg.listenAddr) // URL Shortener
log.Fatal(http.ListenAndServe(cfg.listenAddr, handlers.CombinedLoggingHandler(os.Stdout, r))) r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
suf, err := shortener(r.FormValue("url"), *urlSize)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
t.Execute(w, body{
FullHeader: true,
IsGhost: true,
HasForm: true,
H1: "400",
H3: err.Error(),
Line1: "Boo, looks like this ghost stole this page!",
Line2: "But you can type an URL below to shorten it",
})
return
}
ru, _ := url.Parse(fmt.Sprintf("%s://%s:%v/%s%s", *proto, *domain, *port, *path, suf))
t.Execute(w, body{
IsLink: true,
Line1: ru.String(),
})
}).Methods("POST")
// URL Redirect
r.HandleFunc("/{key}", func(w http.ResponseWriter, r *http.Request) {
vals := mux.Vars(r)
key := vals["key"]
if *path != "" {
key = strings.Replace(key, *path, "", 1)
}
u, err := redirect(key)
if err != nil {
w.WriteHeader(http.StatusNotFound)
t.Execute(w, body{
FullHeader: true,
IsGhost: true,
HasForm: true,
H1: "404",
H3: err.Error(),
Line1: "Boo, looks like this ghost stole this page!",
Line2: "But you can type an URL below to shorten it",
})
return
}
http.Redirect(w, r, u, http.StatusFound)
}).Methods("GET")
// Dump DB to file
r.HandleFunc("/v1/toFile", func(w http.ResponseWriter, r *http.Request) {
i, err := dumpDbTOFile(*dumpFile)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Execute(w, internalError("Failed to dump kv DB to file", err))
return
}
t.Execute(w, body{Line1: fmt.Sprintf("Exported %v items to %v", i, dumpFile)})
}).Methods("GET")
// Read DB from file
r.HandleFunc("/v1/fromFile", func(w http.ResponseWriter, r *http.Request) {
i, err := loadFromFile(*dumpFile, *exp, *cleanup)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Execute(w, internalError("Error loading DB from file", err))
return
}
t.Execute(w, body{Line1: fmt.Sprintf("Imported %v items to the DB", i)})
}).Methods("GET")
// Count items on DB
r.HandleFunc("/v1/count", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%v", pool.ItemCount())
}).Methods("GET")
r.HandleFunc("/v1/dump", func(w http.ResponseWriter, r *http.Request) {
dumpObj, err := json.Marshal(pool.Items())
if err != nil {
t.Execute(w, internalError("Unable to dump key value db: ", err))
return
}
fmt.Fprintf(w, "%s", dumpObj)
}).Methods("GET")
// Loads DB from json POST
r.HandleFunc("/v1/fromPost", func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Execute(w, internalError("Unable to dump key value db: ", err))
return
}
i, err := loadFromJSON(b, *exp, *cleanup)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
t.Execute(w, internalError("Error loading DB", err))
return
}
t.Execute(w, body{Line1: fmt.Sprintf("Imported %v items to the DB", i)})
}).Methods("POST")
log.Printf("Domain: %s, URL Proto: %s, Listen Address: %s\n", *domain, *proto, listenAddr)
log.Fatal(http.ListenAndServe(listenAddr, handlers.CombinedLoggingHandler(os.Stdout, r)))
} }