a simple url shortening service, in the same vein as bit.ly and tinyurl.com, written in Go and using BoltDB as a backend
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.

80 lines
2.2KB

  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. //go:generate go run ./togo -n Favicon_ico -i assets/favicon.ico -p static -o static/favicon_ico.go
  12. //go:generate go run ./togo -n Index_html -i assets/index.html -p static -o static/index_html.go
  13. //go:generate go run ./togo -n Qurl_css -i assets/qurl.css -p static -o static/qurl_css.go
  14. //go:generate go run ./togo -n Submit_html -i assets/submit.html -p static -o static/submit_html.go
  15. //go:generate go run ./togo -n Usage_html -i assets/usage.html -p static -o static/usage_html.go
  16. func main() {
  17. dburl := flag.String("u", "bolt:./qurl.db", "url to database")
  18. lsaddr := flag.String("l", "127.0.0.1:8080", "listen address/port")
  19. jsonfile := flag.String("j", "", "path to json to load into database")
  20. backupint := flag.Int("i", 86400, "seconds between database backups")
  21. backupdir := flag.String("b", "", "destination directory for database backups")
  22. flag.Parse()
  23. // Open storage backend
  24. stor, err := storage.NewStorage(*dburl)
  25. if err != nil {
  26. fmt.Fprintf(os.Stderr, "Database connection error: %s\n", err.Error())
  27. return
  28. }
  29. defer stor.Shutdown()
  30. // If there's a backup dir specified, do backups
  31. // at a specific interval
  32. if *backupdir != "" {
  33. stat, err := os.Stat(*backupdir)
  34. if err != nil {
  35. fmt.Fprintf(os.Stderr, "Directory stat error: %s\n", err.Error())
  36. return
  37. }
  38. if !stat.IsDir() {
  39. fmt.Fprintf(os.Stderr, "Backup directory does not exist: %s\n", *backupdir)
  40. return
  41. }
  42. go manageBackup(stor, *backupdir, *backupint)
  43. }
  44. // Load data if asked
  45. if *jsonfile != "" {
  46. err := loadjson(stor, *jsonfile)
  47. if err != nil {
  48. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  49. return
  50. }
  51. }
  52. // Open listener port
  53. listen, err := net.Listen("tcp", *lsaddr)
  54. if err != nil {
  55. fmt.Fprintf(os.Stderr, "Listen error: %s\n", err.Error())
  56. return
  57. }
  58. root := &pages.RootHandler{Storage: stor}
  59. err = root.Init()
  60. if err != nil {
  61. fmt.Fprintf(os.Stderr, "Handler Init Error: %s\n", err.Error())
  62. return
  63. }
  64. fmt.Fprintf(os.Stdout, "qurl listening .. \n")
  65. err = http.Serve(listen, root)
  66. if err != nil {
  67. fmt.Fprintf(os.Stderr, "Serve error: %s\n", err.Error())
  68. }
  69. }