web_ui.go 828 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. "embed"
  4. "io/fs"
  5. "net/http"
  6. )
  7. //go:embed web/*
  8. var webRootFS embed.FS
  9. type WebUI struct {
  10. fs fs.FS
  11. addr string
  12. mux *http.ServeMux
  13. }
  14. func NewWebUI(addr string) (*WebUI, error) {
  15. sub, err := fs.Sub(webRootFS, "web")
  16. if err != nil {
  17. return nil, err
  18. }
  19. mux := http.NewServeMux()
  20. fs := http.FileServer(http.FS(sub))
  21. mux.Handle("/static/css/", fs) /////////////// 页面样式
  22. mux.Handle("/static/js/", fs) /////////////// 页面脚本
  23. mux.HandleFunc("/login", WebLoginHandler) // 登录接口
  24. mux.HandleFunc("/logout", WebLogoutHandler) // 注销接口
  25. return &WebUI{fs: sub, addr: addr, mux: mux}, nil
  26. }
  27. func (ui *WebUI) Start() error {
  28. StartSessionCleaner() // session过期自动回收
  29. ui.mux.HandleFunc("/", ui.WebRootHandler)
  30. return http.ListenAndServe(ui.addr, ui.mux)
  31. }