use a standard Base62 library for slug generation, attempt to create output directory if it doesn't exist
This commit is contained in:
1 parent
a7e5e803cf
commit
d9c4c90936
4 files changed
+71
-69
No files matched your search
@@ -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)))
|
||||
}
|
||||
Reference in new issue
Block a user