main.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }
  26. if hasPortL {
  27. i, err := strconv.ParseInt(strings.ReplaceAll(arg, "--port=", ""), 10, 32)
  28. if err == nil {
  29. serverPort = int(i)
  30. break
  31. }
  32. }
  33. if hasPortS {
  34. i, err := strconv.ParseInt(strings.ReplaceAll(arg, "-p=", ""), 10, 32)
  35. if err == nil {
  36. serverPort = int(i)
  37. break
  38. }
  39. }
  40. if hasPath {
  41. path = strings.ReplaceAll(arg, "--path=", "")
  42. }
  43. }
  44. }
  45. if strings.HasSuffix(path, "/") {
  46. path = strings.TrimSuffix(path, "/")
  47. }
  48. if len(path) > 0 && !strings.HasPrefix(path, "/") {
  49. path = fmt.Sprintf("/%s", path)
  50. }
  51. http.HandleFunc(fmt.Sprintf("%s/", path), createIndexHandler(reverseHost))
  52. http.HandleFunc(fmt.Sprintf("%s/jrebel/leases", path), jrebelLeasesHandler)
  53. http.HandleFunc(fmt.Sprintf("%s/jrebel/leases/1", path), jrebelLeases1Handler)
  54. http.HandleFunc(fmt.Sprintf("%s/agent/leases", path), jrebelLeasesHandler)
  55. http.HandleFunc(fmt.Sprintf("%s/agent/leases/1", path), jrebelLeases1Handler)
  56. http.HandleFunc(fmt.Sprintf("%s/jrebel/validate-connection", path), jrebelValidateHandler)
  57. http.HandleFunc(fmt.Sprintf("%s/rpc/ping.action", path), pingHandler)
  58. http.HandleFunc(fmt.Sprintf("%s/rpc/obtainTicket.action", path), obtainTicketHandler)
  59. http.HandleFunc(fmt.Sprintf("%s/rpc/releaseTicket.action", path), releaseTicketHandler)
  60. fmt.Printf("start server with port = %d\n", serverPort)
  61. _ = http.ListenAndServe(":"+strconv.Itoa(serverPort), nil)
  62. }