main.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "imuslab.com/arozos/ReverseProxy/mod/aroz"
  10. "imuslab.com/arozos/ReverseProxy/mod/database"
  11. "imuslab.com/arozos/ReverseProxy/mod/tlscert"
  12. )
  13. var (
  14. handler *aroz.ArozHandler
  15. sysdb *database.Database
  16. tlsCertManager *tlscert.Manager
  17. )
  18. //Kill signal handler. Do something before the system the core terminate.
  19. func SetupCloseHandler() {
  20. c := make(chan os.Signal, 2)
  21. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  22. go func() {
  23. <-c
  24. log.Println("\r- Shutting down ReverseProxy")
  25. sysdb.Close()
  26. os.Exit(0)
  27. }()
  28. }
  29. func main() {
  30. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  31. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  32. Name: "ReverseProxy",
  33. Desc: "Basic reverse proxy listener",
  34. Group: "Network",
  35. IconPath: "reverseproxy/img/small_icon.png",
  36. Version: "0.3",
  37. StartDir: "reverseproxy/index.html",
  38. SupportFW: true,
  39. LaunchFWDir: "reverseproxy/index.html",
  40. SupportEmb: false,
  41. InitFWSize: []int{1080, 580},
  42. })
  43. //Register the standard web services urls
  44. fs := http.FileServer(http.Dir("./web"))
  45. http.Handle("/", fs)
  46. initAPIs()
  47. SetupCloseHandler()
  48. //Create database
  49. db, err := database.NewDatabase("sys.db", false)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. sysdb = db
  54. //Create tables for the database
  55. sysdb.NewTable("settings")
  56. //Create a TLS certificate manager
  57. tlsCertManager, _ = tlscert.NewManager("./certs")
  58. //Start the reverse proxy server in go routine
  59. go func() {
  60. ReverseProxtInit()
  61. }()
  62. time.Sleep(300 * time.Millisecond)
  63. //Any log println will be shown in the core system via STDOUT redirection. But not STDIN.
  64. log.Println("ReverseProxy started. Visit control panel at http://localhost" + handler.Port)
  65. err = http.ListenAndServe(handler.Port, nil)
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. }