74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"git.binarythought.com/cdramey/qurl/pages"
|
|
"git.binarythought.com/cdramey/qurl/storage"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
dburl := flag.String("u", "bolt:./qurl.db", "url to database")
|
|
lsaddr := flag.String("l", "127.0.0.1:8080", "listen address/port")
|
|
jsonfile := flag.String("j", "", "path to json to load into database")
|
|
backupint := flag.Int("i", 86400, "seconds between database backups")
|
|
backupdir := flag.String("b", "", "destination directory for database backups")
|
|
flag.Parse()
|
|
|
|
// Open storage backend
|
|
stor, err := storage.NewStorage(*dburl)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Database connection error: %s\n", err.Error())
|
|
return
|
|
}
|
|
defer stor.Shutdown()
|
|
|
|
// If there's a backup dir specified, do backups
|
|
// at a specific interval
|
|
if *backupdir != "" {
|
|
stat, err := os.Stat(*backupdir)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Directory stat error: %s\n", err.Error())
|
|
return
|
|
}
|
|
if !stat.IsDir() {
|
|
fmt.Fprintf(os.Stderr, "Backup directory does not exist: %s\n", *backupdir)
|
|
return
|
|
}
|
|
|
|
go manageBackup(stor, *backupdir, *backupint)
|
|
}
|
|
|
|
// Load data if asked
|
|
if *jsonfile != "" {
|
|
err := loadjson(stor, *jsonfile)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
// Open listener port
|
|
listen, err := net.Listen("tcp", *lsaddr)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Listen error: %s\n", err.Error())
|
|
return
|
|
}
|
|
|
|
root := &pages.RootHandler{Storage: stor}
|
|
err = root.Init()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Handler Init Error: %s\n", err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "qurl listening .. \n")
|
|
err = http.Serve(listen, root)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Serve error: %s\n", err.Error())
|
|
}
|
|
}
|