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.

67 lines
1.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. "qurl/storage"
  9. "qurl/static"
  10. "runtime"
  11. )
  12. //go:generate bindata -m Assets -r assets -p static -o static/assets.go assets
  13. func main() {
  14. dburl := flag.String("u", "bolt:./qurl.db", "url to database")
  15. lsaddr := flag.String("l", "127.0.0.1:8080", "listen address/port")
  16. jsonfile := flag.String("j", "", "path to json to load into database")
  17. maxpro := flag.Int("m", runtime.NumCPU()+2,
  18. "maximum number of threads to use")
  19. flag.Parse()
  20. if *maxpro < 3 {
  21. fmt.Fprintf(os.Stderr, "Thread limit too low: %d (min 3)\n", *maxpro)
  22. return
  23. }
  24. // Limit max processes
  25. runtime.GOMAXPROCS(*maxpro)
  26. // Open storage backend
  27. stor, err := storage.NewStorage(*dburl)
  28. if err != nil {
  29. fmt.Fprintf(os.Stderr, "Database connection error: %s\n", err.Error())
  30. return
  31. }
  32. defer stor.Shutdown()
  33. // Load data if asked
  34. if *jsonfile != "" {
  35. err := loadjson(stor, *jsonfile)
  36. if err != nil {
  37. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  38. return
  39. }
  40. }
  41. // Open listener port
  42. listen, err := net.Listen("tcp", *lsaddr)
  43. if err != nil {
  44. fmt.Fprintf(os.Stderr, "Listen error: %s\n", err.Error())
  45. return
  46. }
  47. mux := http.NewServeMux()
  48. mux.Handle("/index.html", &static.StaticContent{Content: "index.html"})
  49. mux.Handle("/favicon.ico", &static.StaticContent{Content: "favicon.ico"})
  50. mux.Handle("/qurl.css", &static.StaticContent{Content: "qurl.css"})
  51. fmt.Fprintf(os.Stdout, "qurl listening .. \n")
  52. err = http.Serve(listen, mux)
  53. if err != nil {
  54. fmt.Fprintf(os.Stderr, "Serve error: %s\n", err.Error())
  55. }
  56. }