server.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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("/user/list", UserList)
  29. r.HandleFunc("/user/detail", UserDetail)
  30. r.HandleFunc("/user/set", UserSet)
  31. r.HandleFunc("/user/del", UserDel)
  32. r.HandleFunc("/user/online", UserOnline)
  33. r.HandleFunc("/user/offline", UserOffline)
  34. r.HandleFunc("/user/reline", UserReline)
  35. r.HandleFunc("/user/otp_qr", UserOtpQr)
  36. r.HandleFunc("/user/ip_map/list", UserIpMapList)
  37. r.HandleFunc("/user/ip_map/detail", UserIpMapDetail)
  38. r.HandleFunc("/user/ip_map/set", UserIpMapSet)
  39. r.HandleFunc("/user/ip_map/del", UserIpMapDel)
  40. r.HandleFunc("/group/list", GroupList)
  41. r.HandleFunc("/group/names", GroupNames)
  42. r.HandleFunc("/group/detail", GroupDetail)
  43. r.HandleFunc("/group/set", GroupSet)
  44. r.HandleFunc("/group/del", GroupDel)
  45. // pprof
  46. if base.Cfg.Pprof {
  47. r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline).Name("debug")
  48. r.HandleFunc("/debug/pprof/profile", pprof.Profile).Name("debug")
  49. r.HandleFunc("/debug/pprof/symbol", pprof.Symbol).Name("debug")
  50. r.HandleFunc("/debug/pprof/trace", pprof.Trace).Name("debug")
  51. r.HandleFunc("/debug/pprof", location("/debug/pprof/"))
  52. r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index).Name("debug")
  53. }
  54. base.Info("Listen admin", base.Cfg.AdminAddr)
  55. err := http.ListenAndServe(base.Cfg.AdminAddr, r)
  56. if err != nil {
  57. base.Fatal(err)
  58. }
  59. }
  60. func location(url string) http.HandlerFunc {
  61. return func(w http.ResponseWriter, r *http.Request) {
  62. w.Header().Set("Location", url)
  63. w.WriteHeader(http.StatusFound)
  64. }
  65. }