use a standard Base62 library for slug generation, attempt to create output directory if it doesn't exist

This commit is contained in:
Amrit Panesar committed 2017-12-28 19:46:22 -08:00
1 parent a7e5e803cf
commit d9c4c90936
4 files changed
+71 -69

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"time"
"github.com/pilu/go-base62"
"github.com/spf13/viper"
)
func handleConnection(conn net.Conn) {
remoteHost, err := net.LookupCNAME(conn.RemoteAddr().String())
if err != nil {
remoteHost = conn.RemoteAddr().String()
}
defer func() {
log.Printf("Closing connection to: %s (%s).\n", remoteHost, conn.RemoteAddr().String())
conn.Close()
}()
log.Printf("Incoming connection from: %s (%s).\n", remoteHost, conn.RemoteAddr().String())
conn.SetReadDeadline(time.Now().Add(time.Second * 5))
slug := base62.Encode(int(time.Now().UnixNano()))
slugFullpath := fmt.Sprintf("%s/%s", viper.GetString("output"), slug)
log.Printf("Writing file to %s", slugFullpath)
file, err := os.Create(slugFullpath)
if err != nil {
conn.Write([]byte(fmt.Sprintf("%s", "Internal error encountered - Please try again later...\n\n")))
log.Fatalf("Unable to create slug file: %s", slugFullpath)
return
}
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
file.WriteString(scanner.Text())
file.Write([]byte("\n"))
conn.SetReadDeadline(time.Now().Add(time.Second * 5))
}
stat, _ := file.Stat()
if stat.Size() <= 0 {
file.Close()
log.Printf("0 bytes received, aborting...")
os.Remove(slugFullpath)
return
}
file.Close()
log.Printf("Wrote %d bytes to %s", stat.Size(), slugFullpath)
conn.Write([]byte(fmt.Sprintf("%s://%s/%s\r\n", viper.GetString("uriprefix"), viper.GetString("domain"), slug)))
}
+13
View File
@@ -2,6 +2,7 @@ package main
import (
"fmt"
"log"
"os"
"github.com/spf13/pflag"
@@ -46,4 +47,16 @@ func init() {
} else {
viper.Set("uriprefix", "http")
}
_, err := os.Stat(viper.GetString("output"))
if err != nil {
log.Printf("unable to stat() output directory: %s\n", viper.GetString("output"))
log.Printf("attempting to create %s\n", viper.GetString("output"))
err = os.Mkdir(viper.GetString("output"), os.ModeDir)
if err != nil {
log.Printf("Unable to create directory %s\n", viper.GetString("output"))
log.Fatalf("System error: %s\n", err)
os.Exit(1)
}
}
}
-46
View File
@@ -1,7 +1,6 @@
package main
import (
"bufio"
"fmt"
"log"
"net"
@@ -32,48 +31,3 @@ func main() {
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
remoteHost, err := net.LookupCNAME(conn.RemoteAddr().String())
if err != nil {
remoteHost = conn.RemoteAddr().String()
}
defer func() {
log.Printf("Closing connection to: %s (%s).\n", remoteHost, conn.RemoteAddr().String())
conn.Close()
}()
log.Printf("Incoming connection from: %s (%s).\n", remoteHost, conn.RemoteAddr().String())
conn.SetReadDeadline(time.Now().Add(time.Second * 5))
slug := generateSlug(time.Now().UnixNano())
slugFullpath := fmt.Sprintf("%s/%s", viper.GetString("output"), slug)
log.Printf("Writing file to %s", slugFullpath)
file, err := os.Create(slugFullpath)
if err != nil {
log.Fatalf("Unable to create slug file: %s", slugFullpath)
conn.Write([]byte(fmt.Sprintf("%s", "Internal error encountered - Please try again later...")))
return
}
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
file.WriteString(scanner.Text())
file.Write([]byte("\n"))
conn.SetReadDeadline(time.Now().Add(time.Second * 5))
}
stat, _ := file.Stat()
if stat.Size() <= 0 {
file.Close()
log.Printf("0 bytes received, aborting...")
os.Remove(slugFullpath)
return
}
file.Close()
log.Printf("Wrote %d bytes to %s", stat.Size(), slugFullpath)
conn.Write([]byte(fmt.Sprintf("%s://%s/%s\r\n", viper.GetString("uriprefix"), viper.GetString("domain"), slug)))
}
-23
View File
@@ -1,23 +0,0 @@
package main
import (
"fmt"
"strconv"
)
// slugMap contains a sequence of valid characters for slug generation
const slugMap = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789"
// generateSlug takes an integer and generates a string based on slugMap
func generateSlug(seed int64) string {
stringSeed := fmt.Sprintf("%d", seed)
evenLength := (len(stringSeed) / 2) * 2
digitHold := int64(0)
out := ""
for i := 0; i < evenLength; i += 2 {
digitHold, _ = strconv.ParseInt(stringSeed[i:i+2], 10, 8)
digitHold = digitHold % int64(len(slugMap))
out += slugMap[digitHold : digitHold+1]
}
return out
}