agent.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "runtime"
  8. "github.com/710leo/urlooker/modules/agent/backend"
  9. "github.com/710leo/urlooker/modules/agent/cron"
  10. "github.com/710leo/urlooker/modules/agent/g"
  11. "github.com/710leo/urlooker/modules/agent/utils"
  12. "github.com/toolkits/file"
  13. )
  14. func prepare() {
  15. runtime.GOMAXPROCS(runtime.NumCPU())
  16. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  17. }
  18. func init() {
  19. prepare()
  20. cfg := flag.String("c", "", "configuration file")
  21. version := flag.Bool("v", false, "show version")
  22. help := flag.Bool("h", false, "help")
  23. flag.Parse()
  24. handleVersion(*version)
  25. handleHelp(*help)
  26. handleConfig(*cfg)
  27. fmt.Println("g.Config.Web.Addrs: ", g.Config)
  28. backend.InitClients(g.Config.Web.Addrs)
  29. g.Init()
  30. }
  31. func main() {
  32. ips, err := utils.IntranetIP()
  33. if len(ips) > 0 {
  34. g.IP = ips[0]
  35. } else {
  36. log.Println("get local ip err: ", err)
  37. }
  38. go cron.Push()
  39. cron.StartCheck()
  40. }
  41. func handleVersion(displayVersion bool) {
  42. if displayVersion {
  43. fmt.Println(g.VERSION)
  44. os.Exit(0)
  45. }
  46. }
  47. func handleHelp(displayHelp bool) {
  48. if displayHelp {
  49. flag.Usage()
  50. os.Exit(0)
  51. }
  52. }
  53. func handleConfig(configFile string) {
  54. if configFile == "" {
  55. configFile = "configs/agent.yml"
  56. }
  57. if file.IsExist("configs/agent.local.yml") {
  58. configFile = "configs/agent.local.yml"
  59. }
  60. err := g.Parse(configFile)
  61. if err != nil {
  62. log.Fatalln(err)
  63. }
  64. }