a simple url shortener in Go (check it out at qurl.org)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7 KiB

3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "git.binarythought.com/cdramey/qurl/pages"
  6. "git.binarythought.com/cdramey/qurl/storage"
  7. "net"
  8. "net/http"
  9. "os"
  10. )
  11. func main() {
  12. dburl := flag.String("u", "bolt:./qurl.db", "url to database")
  13. lsaddr := flag.String("l", "127.0.0.1:8080", "listen address/port")
  14. jsonfile := flag.String("j", "", "path to json to load into database")
  15. backupint := flag.Int("i", 86400, "seconds between database backups")
  16. backupdir := flag.String("b", "", "destination directory for database backups")
  17. flag.Parse()
  18. // Open storage backend
  19. stor, err := storage.NewStorage(*dburl)
  20. if err != nil {
  21. fmt.Fprintf(os.Stderr, "Database connection error: %s\n", err.Error())
  22. return
  23. }
  24. defer stor.Shutdown()
  25. // If there's a backup dir specified, do backups
  26. // at a specific interval
  27. if *backupdir != "" {
  28. stat, err := os.Stat(*backupdir)
  29. if err != nil {
  30. fmt.Fprintf(os.Stderr, "Directory stat error: %s\n", err.Error())
  31. return
  32. }
  33. if !stat.IsDir() {
  34. fmt.Fprintf(os.Stderr, "Backup directory does not exist: %s\n", *backupdir)
  35. return
  36. }
  37. go manageBackup(stor, *backupdir, *backupint)
  38. }
  39. // Load data if asked
  40. if *jsonfile != "" {
  41. err := loadjson(stor, *jsonfile)
  42. if err != nil {
  43. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  44. return
  45. }
  46. }
  47. // Open listener port
  48. listen, err := net.Listen("tcp", *lsaddr)
  49. if err != nil {
  50. fmt.Fprintf(os.Stderr, "Listen error: %s\n", err.Error())
  51. return
  52. }
  53. root := &pages.RootHandler{Storage: stor}
  54. err = root.Init()
  55. if err != nil {
  56. fmt.Fprintf(os.Stderr, "Handler Init Error: %s\n", err.Error())
  57. return
  58. }
  59. fmt.Fprintf(os.Stdout, "qurl listening .. \n")
  60. err = http.Serve(listen, root)
  61. if err != nil {
  62. fmt.Fprintf(os.Stderr, "Serve error: %s\n", err.Error())
  63. }
  64. }