handler.go 764 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package desktop
  2. import (
  3. "context"
  4. "github.com/astaxie/beego/logs"
  5. "net/http"
  6. "strings"
  7. )
  8. const prefix = "/api/nginx-ui/api"
  9. type ApiHandler struct {
  10. http.Handler
  11. ctx context.Context
  12. api *Api
  13. }
  14. func NewApiHandler() *ApiHandler {
  15. return &ApiHandler{
  16. api: NewApi(),
  17. }
  18. }
  19. func (h *ApiHandler) Startup(ctx context.Context) {
  20. ApiSession.ctx = ctx
  21. h.ctx = ctx
  22. h.api.ctx = ctx
  23. }
  24. // 这也是一种方法,不过感觉比较复杂,需要自己处理请求参数之类的
  25. func (h *ApiHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
  26. requestUrl := strings.TrimPrefix(req.URL.Path, prefix)
  27. method := req.Method
  28. switch method {
  29. case http.MethodGet:
  30. }
  31. logs.Info("ServerHTTP: %s,%s", method, requestUrl)
  32. res.Write([]byte("{}"))
  33. }