web.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "runtime"
  8. "github.com/710leo/urlooker/modules/web/api"
  9. "github.com/710leo/urlooker/modules/web/cron"
  10. "github.com/710leo/urlooker/modules/web/g"
  11. "github.com/710leo/urlooker/modules/web/http"
  12. "github.com/710leo/urlooker/modules/web/http/cookie"
  13. "github.com/710leo/urlooker/modules/web/model"
  14. "github.com/710leo/urlooker/modules/web/sender"
  15. "github.com/710leo/urlooker/modules/web/store"
  16. "github.com/710leo/urlooker/modules/web/utils"
  17. "github.com/toolkits/file"
  18. )
  19. func prepare() {
  20. runtime.GOMAXPROCS(runtime.NumCPU())
  21. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  22. }
  23. func init() {
  24. prepare()
  25. cfg := flag.String("c", "", "configuration file")
  26. version := flag.Bool("v", false, "show version")
  27. help := flag.Bool("h", false, "help")
  28. flag.Parse()
  29. handleVersion(*version)
  30. handleHelp(*help)
  31. handleConfig(*cfg)
  32. store.InitMysql()
  33. cron.Init()
  34. sender.Init()
  35. cookie.Init()
  36. }
  37. func main() {
  38. err := model.AdminRegister()
  39. if err != nil {
  40. log.Fatalln(err)
  41. }
  42. if g.Config.Statsd.Enable {
  43. utils.InitStatsd(g.Config.Statsd.Addr)
  44. }
  45. go api.Start()
  46. http.Start()
  47. }
  48. func handleVersion(displayVersion bool) {
  49. if displayVersion {
  50. fmt.Println(g.VERSION)
  51. os.Exit(0)
  52. }
  53. }
  54. func handleHelp(displayHelp bool) {
  55. if displayHelp {
  56. flag.Usage()
  57. os.Exit(0)
  58. }
  59. }
  60. func handleConfig(configFile string) {
  61. if configFile == "" {
  62. configFile = "configs/web.yml"
  63. }
  64. if file.IsExist("configs/web.local.yml") {
  65. configFile = "configs/web.local.yml"
  66. }
  67. err := g.Parse(configFile)
  68. if err != nil {
  69. log.Fatalln(err)
  70. }
  71. }