@@ -9,14 +9,17 @@ import (
"io/ioutil"
"net"
"os"
"os/exec"
"github.com/writeas/nerds/store"
)
var (
banner []byte
outDir string
staticDir string
debugging bool
rsyncHost string
db *sql.DB
)
@@ -34,16 +37,22 @@ const (
func main() {
// Get any arguments
outDirPtr := flag.String("o", "/var/write", "Directory where text files will be stored.")
staticDirPtr := flag.String("s", "./static", "Directory where required static files exist.")
rsyncHostPtr := flag.String("h", "", "Hostname of the server to rsync saved files to.")
portPtr := flag.Int("p", 2323, "Port to listen on.")
debugPtr := flag.Bool("debug", false, "Enables garrulous debug logging.")
flag.Parse()
outDir = *outDirPtr
staticDir = *staticDirPtr
rsyncHost = *rsyncHostPtr
debugging = *debugPtr
fmt.Print("\nCONFIG:\n")
fmt.Printf("Output directory : %s\n", outDir)
fmt.Printf("Static directory : %s\n", staticDir)
fmt.Printf("rsync host : %s\n", rsyncHost)
fmt.Printf("Debugging enabled : %t\n\n", debugging)
fmt.Print("Initializing...")
@@ -57,21 +66,25 @@ func main() {
// Connect to database
dbUser := os.Getenv("WA_USER")
dbPassword := os.Getenv("WA_PASSWORD")
dbName := os.Getenv("WA_DB")
dbHost := os.Getenv("WA_HOST")
if dbUser == "" || dbPassword == "" {
fmt.Println("Database user or password not set.")
if outDir == "" && (dbUser == "" || dbPassword == "" || dbName == "") {
// Ensure parameters needed for storage (file or database) are given
fmt.Println("Database user, password, or database name not set.")
return
}
fmt.Print("Connecting to database...")
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/writeas?charset=utf8mb4", dbUser, dbPassword, dbHost))
if err != nil {
fmt.Printf("\n%s\n", err)
return
if outDir == "" {
fmt.Print("Connecting to database...")
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8mb4", dbUser, dbPassword, dbHost, dbName))
if err != nil {
fmt.Printf("\n%s\n", err)
return
}
defer db.Close()
fmt.Println("CONNECTED")
}
defer db.Close()
fmt.Println("CONNECTED")
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", *portPtr))
if err != nil {
@@ -169,13 +182,24 @@ func readInput(c net.Conn) {
friendlyId := store.GenerateFriendlyRandomString(store.FriendlyIdLen)
editToken := store.Generate62RandomString(32)
_, err := db.Exec("INSERT INTO posts (id, content, modify_token, text_appearance) VALUES (?, ?, ?, 'mono')", friendlyId, post.Bytes(), editToken)
if err != nil {
fmt.Printf("There was an error saving: %s\n", err)
output(c, "Something went terribly wrong, sorry. Try again later?\n\n")
break
if outDir == "" {
_, err := db.Exec("INSERT INTO posts (id, content, modify_token, text_appearance) VALUES (?, ?, ?, 'mono')", friendlyId, post.Bytes(), editToken)
if err != nil {
fmt.Printf("There was an error saving: %s\n", err)
output(c, "Something went terribly wrong, sorry. Try again later?\n\n")
break
}
output(c, fmt.Sprintf("\n%s\nPosted! View at %shttps://write.as/%s%s", hr, colBlue, friendlyId, noCol))
} else {
output(c, fmt.Sprintf("\n%s\nPosted to %shttp://nerds.write.as/%s%s", hr, colBlue, friendlyId, noCol))
if rsyncHost != "" {
output(c, "\nPosting to secure site...")
exec.Command("rsync", "-ptgou", outDir+"/"+friendlyId, rsyncHost+":").Run()
output(c, fmt.Sprintf("\nPosted! View at %shttps://write.as/%s%s", colBlue, friendlyId, noCol))
}
}
output(c, fmt.Sprintf("\n%s\nPosted! View at %shttps://write.as/%s%s", hr, colBlue, friendlyId, noCol))
output(c, "\nSee you later.\n\n")
break
}