main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "strconv"
  7. "strings"
  8. )
  9. func main() {
  10. serverPort := 12345
  11. path := ""
  12. reverseHost := ""
  13. if len(os.Args) > 1 {
  14. for k, v := range os.Args {
  15. if k == 0 {
  16. continue
  17. }
  18. arg := strings.TrimSpace(v)
  19. println("arg:", arg)
  20. hasPortL := strings.HasPrefix(arg, "--port=")
  21. hasPortS := strings.HasPrefix(arg, "-p=")
  22. hasPath := strings.HasPrefix(arg, "--path=")
  23. if strings.HasPrefix(arg, "--host=") {
  24. reverseHost = strings.TrimPrefix(arg, "--host=")
  25. continue
  26. }
  27. if hasPortL {
  28. i, err := strconv.ParseInt(strings.ReplaceAll(arg, "--port=", ""), 10, 32)
  29. if err == nil {
  30. serverPort = int(i)
  31. continue
  32. }
  33. }
  34. if hasPortS {
  35. i, err := strconv.ParseInt(strings.ReplaceAll(arg, "-p=", ""), 10, 32)
  36. if err == nil {
  37. serverPort = int(i)
  38. continue
  39. }
  40. }
  41. if hasPath {
  42. path = strings.ReplaceAll(arg, "--path=", "")
  43. continue
  44. }
  45. }
  46. }
  47. if strings.HasSuffix(path, "/") {
  48. path = strings.TrimSuffix(path, "/")
  49. }
  50. if len(path) > 0 && !strings.HasPrefix(path, "/") {
  51. path = fmt.Sprintf("/%s", path)
  52. }
  53. http.HandleFunc(fmt.Sprintf("%s/", path), createIndexHandler(reverseHost))
  54. http.HandleFunc(fmt.Sprintf("%s/jrebel/leases", path), jrebelLeasesHandler)
  55. http.HandleFunc(fmt.Sprintf("%s/jrebel/leases/1", path), jrebelLeases1Handler)
  56. http.HandleFunc(fmt.Sprintf("%s/agent/leases", path), jrebelLeasesHandler)
  57. http.HandleFunc(fmt.Sprintf("%s/agent/leases/1", path), jrebelLeases1Handler)
  58. http.HandleFunc(fmt.Sprintf("%s/jrebel/validate-connection", path), jrebelValidateHandler)
  59. http.HandleFunc(fmt.Sprintf("%s/rpc/ping.action", path), pingHandler)
  60. http.HandleFunc(fmt.Sprintf("%s/rpc/obtainTicket.action", path), obtainTicketHandler)
  61. http.HandleFunc(fmt.Sprintf("%s/rpc/releaseTicket.action", path), releaseTicketHandler)
  62. fmt.Printf("start server with port = %d\n", serverPort)
  63. _ = http.ListenAndServe(":"+strconv.Itoa(serverPort), nil)
  64. }