http_api.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // package http_api -- различные API для работы веб-морды.
  2. package http_api
  3. import (
  4. "fmt"
  5. "github.com/gofiber/fiber/v2"
  6. mKh "gitp78su.ipnodns.ru/svi/kern/v4/lev0/helpers"
  7. mKt "gitp78su.ipnodns.ru/svi/kern/v4/lev0/ktypes"
  8. mKr "gitp78su.ipnodns.ru/svi/kern/v4/lev1/result"
  9. "gitp78su.ipnodns.ru/svi/kern/v4/lev2/kctx"
  10. )
  11. // HttpApi -- различные API для работы веб-морды.
  12. type HttpApi struct{}
  13. // NewHttpApi -- возвращает новое HttpApi.
  14. func NewHttpApi() mKt.IResult[*HttpApi] {
  15. sf := &HttpApi{}
  16. resKernCtx := kctx.GetKernelCtx()
  17. if resKernCtx.IsErr() {
  18. err := fmt.Errorf("NewHttpApi(): in kern ctx, err=\n\t%w", resKernCtx.Err())
  19. return mKr.NewErr[*HttpApi](err)
  20. }
  21. kCtx := resKernCtx.Val()
  22. optFiber := kCtx.Get("fiberApp")
  23. if optFiber.IsNone() {
  24. err := fmt.Errorf("NewHttpApi(): not found fiberApp in kernel ctx")
  25. return mKr.NewErr[*HttpApi](err)
  26. }
  27. fiberApp := optFiber.Val().Val().(*fiber.App)
  28. fiberApp.Post("/api_time", sf.postTime)
  29. return mKr.NewRes(sf)
  30. }
  31. // Возвращает текущее время сервера.
  32. func (sf *HttpApi) postTime(ctx *fiber.Ctx) error {
  33. strTime := mKh.TimeNowStr()
  34. return ctx.SendString(string(strTime))
  35. }