server.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // admin:后台管理接口
  2. package admin
  3. import (
  4. "embed"
  5. "net/http"
  6. "net/http/pprof"
  7. "github.com/bjdgyc/anylink/base"
  8. "github.com/gorilla/mux"
  9. )
  10. var UiData embed.FS
  11. // StartAdmin 开启服务
  12. func StartAdmin() {
  13. r := mux.NewRouter()
  14. r.Use(authMiddleware)
  15. r.Handle("/", http.RedirectHandler("/ui/", http.StatusFound)).Name("index")
  16. r.PathPrefix("/ui/").Handler(
  17. // http.StripPrefix("/ui/", http.FileServer(http.Dir(base.Cfg.UiPath))),
  18. http.FileServer(http.FS(UiData)),
  19. ).Name("static")
  20. r.HandleFunc("/base/login", Login).Name("login")
  21. r.HandleFunc("/set/home", SetHome)
  22. r.HandleFunc("/set/system", SetSystem)
  23. r.HandleFunc("/set/soft", SetSoft)
  24. r.HandleFunc("/set/other", SetOther)
  25. r.HandleFunc("/set/other/edit", SetOtherEdit)
  26. r.HandleFunc("/set/other/smtp", SetOtherSmtp)
  27. r.HandleFunc("/set/other/smtp/edit", SetOtherSmtpEdit)
  28. r.HandleFunc("/set/audit/list", SetAuditList)
  29. r.HandleFunc("/user/list", UserList)
  30. r.HandleFunc("/user/detail", UserDetail)
  31. r.HandleFunc("/user/set", UserSet)
  32. r.HandleFunc("/user/del", UserDel)
  33. r.HandleFunc("/user/online", UserOnline)
  34. r.HandleFunc("/user/offline", UserOffline)
  35. r.HandleFunc("/user/reline", UserReline)
  36. r.HandleFunc("/user/otp_qr", UserOtpQr)
  37. r.HandleFunc("/user/ip_map/list", UserIpMapList)
  38. r.HandleFunc("/user/ip_map/detail", UserIpMapDetail)
  39. r.HandleFunc("/user/ip_map/set", UserIpMapSet)
  40. r.HandleFunc("/user/ip_map/del", UserIpMapDel)
  41. r.HandleFunc("/group/list", GroupList)
  42. r.HandleFunc("/group/names", GroupNames)
  43. r.HandleFunc("/group/detail", GroupDetail)
  44. r.HandleFunc("/group/set", GroupSet)
  45. r.HandleFunc("/group/del", GroupDel)
  46. // pprof
  47. if base.Cfg.Pprof {
  48. r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline).Name("debug")
  49. r.HandleFunc("/debug/pprof/profile", pprof.Profile).Name("debug")
  50. r.HandleFunc("/debug/pprof/symbol", pprof.Symbol).Name("debug")
  51. r.HandleFunc("/debug/pprof/trace", pprof.Trace).Name("debug")
  52. r.HandleFunc("/debug/pprof", location("/debug/pprof/")).Name("debug")
  53. r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index).Name("debug")
  54. }
  55. base.Info("Listen admin", base.Cfg.AdminAddr)
  56. err := http.ListenAndServe(base.Cfg.AdminAddr, r)
  57. if err != nil {
  58. base.Fatal(err)
  59. }
  60. }
  61. func location(url string) http.HandlerFunc {
  62. return func(w http.ResponseWriter, r *http.Request) {
  63. w.Header().Set("Location", url)
  64. w.WriteHeader(http.StatusFound)
  65. }
  66. }