Generate default config.json and split code into functions

Should help with readability, and makes the binary effectively completely self suffecient and portable 👍
This commit is contained in:
gmemstr 2017-12-31 23:34:26 -08:00
parent 8fcbd02fc4
commit b0e7a69771

142
setup.go
View file

@ -6,10 +6,12 @@ import (
"crypto/rand" "crypto/rand"
"database/sql" "database/sql"
"encoding/base64" "encoding/base64"
"encoding/json"
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -17,62 +19,37 @@ import (
"github.com/google/go-github/github" "github.com/google/go-github/github"
) )
func GenerateRandomBytes(n int) ([]byte, error) { type Configuration struct {
b := make([]byte, n) Name string `json:"Name"`
_, err := rand.Read(b) Host string `json:"Host"`
if err != nil { Email string `json:"Email"`
return nil, err Description string `json:"Description"`
} Image string `json:"Image"`
PodcastUrl string `json:"PodcastUrl"`
return b, nil
}
// GenerateRandomString returns a URL-safe, base64 encoded
// securely generated random string.
func GenerateRandomString(s int) (string, error) {
b, err := GenerateRandomBytes(s)
return base64.URLEncoding.EncodeToString(b), err
} }
func Setup() { func Setup() {
go GenerateRss() // Create directories
defer LockFile()
// Create users SQLite3 file
fmt.Println("Initializing the database")
os.MkdirAll("assets/config/", 0755) os.MkdirAll("assets/config/", 0755)
os.Mkdir("podcasts", 0755) os.Mkdir("podcasts", 0755)
os.Create("assets/config/users.db")
db, err := sql.Open("sqlite3", "assets/config/users.db") // Write basic configuration file
if err != nil { WriteSkeletonConfig()
fmt.Println("Problem opening database file! %v", err)
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS `users` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `username` TEXT UNIQUE, `hash` TEXT, `realname` TEXT, `email` TEXT, `permissions` INTEGER )") // Generate neccesary feed files
if err != nil { go GenerateRss()
fmt.Println("Problem creating database! %v", err)
}
text, err := GenerateRandomString(12) // Create "first run" lockfile when function exits
if err != nil { defer LockFile()
fmt.Println("Error randomly generating password", err)
} // Create users SQLite3 file
fmt.Println("Admin password: ", text) CreateDatabase()
hash, err := bcrypt.GenerateFromPassword([]byte(text), 4)
if err != nil {
fmt.Println("Error generating hash", err)
}
if bcrypt.CompareHashAndPassword(hash, []byte(text)) == nil {
fmt.Println("Password hashed")
}
_, err = db.Exec("INSERT INTO users(id,username,hash,realname,email,permissions) VALUES (0,'admin','" + string(hash) + "','Administrator','admin@localhost',2)")
if err != nil {
fmt.Println("Problem creating database! %v", err)
}
defer db.Close()
// Download web assets // Download web assets
GetWebAssets()
}
func GetWebAssets() {
fmt.Println("Downloading web assets") fmt.Println("Downloading web assets")
os.MkdirAll("assets/web/", 0755) os.MkdirAll("assets/web/", 0755)
@ -115,6 +92,39 @@ func Setup() {
} }
} }
func CreateDatabase() {
fmt.Println("Initializing the database")
os.Create("assets/config/users.db")
db, err := sql.Open("sqlite3", "assets/config/users.db")
if err != nil {
fmt.Println("Problem opening database file! %v", err)
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS `users` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `username` TEXT UNIQUE, `hash` TEXT, `realname` TEXT, `email` TEXT, `permissions` INTEGER )")
if err != nil {
fmt.Println("Problem creating database! %v", err)
}
text, err := GenerateRandomString(12)
if err != nil {
fmt.Println("Error randomly generating password", err)
}
fmt.Println("Admin password: ", text)
hash, err := bcrypt.GenerateFromPassword([]byte(text), 4)
if err != nil {
fmt.Println("Error generating hash", err)
}
if bcrypt.CompareHashAndPassword(hash, []byte(text)) == nil {
fmt.Println("Password hashed")
}
_, err = db.Exec("INSERT INTO users(id,username,hash,realname,email,permissions) VALUES (0,'admin','" + string(hash) + "','Administrator','admin@localhost',2)")
if err != nil {
fmt.Println("Problem creating database! %v", err)
}
defer db.Close()
}
func LockFile() { func LockFile() {
lock, err := os.Create(".lock") lock, err := os.Create(".lock")
if err != nil { if err != nil {
@ -124,6 +134,46 @@ func LockFile() {
defer lock.Close() defer lock.Close()
} }
func WriteSkeletonConfig() {
fmt.Println("Writing basic config file to disk")
os.Create("assets/config/config.json")
config := Configuration{
"Pogo Podcast",
"John Doe",
"johndoe@localhost",
"A Podcast About Stuff",
"localhost:3000/assets/logo-large.png",
"http://localhost:3000",
}
c, err := json.Marshal(config)
filename := "config.json"
err = ioutil.WriteFile("./assets/config/"+filename, c, 0644)
if err != nil {
fmt.Println("Error: ", err)
}
}
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
// GenerateRandomString returns a URL-safe, base64 encoded
// securely generated random string.
func GenerateRandomString(s int) (string, error) {
b, err := GenerateRandomBytes(s)
return base64.URLEncoding.EncodeToString(b), err
}
// From https://stackoverflow.com/questions/20357223/easy-way-to-unzip-file-with-golang // From https://stackoverflow.com/questions/20357223/easy-way-to-unzip-file-with-golang
func Unzip(src, dest string) error { func Unzip(src, dest string) error {
r, err := zip.OpenReader(src) r, err := zip.OpenReader(src)