proto_route.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package data
  2. import "f1-game/internal/code"
  3. type (
  4. protoRouteRow struct {
  5. Enable bool `json:"enable"`
  6. Name string `json:"name"`
  7. Route string `json:"route"`
  8. Request string `json:"request"`
  9. Response string `json:"response"`
  10. Push string `json:"push"`
  11. Notify string `json:"notify"`
  12. Desc string `json:"desc"`
  13. AllowRequest bool `json:"-"`
  14. }
  15. protoRouteConfig struct {
  16. maps map[string]*protoRouteRow
  17. }
  18. )
  19. func (p *protoRouteConfig) Name() string {
  20. return "proto_route"
  21. }
  22. func (p *protoRouteConfig) Init() {
  23. p.maps = map[string]*protoRouteRow{}
  24. }
  25. func (p *protoRouteConfig) OnLoad(maps any, _ bool) (int, error) {
  26. var list []*protoRouteRow
  27. if err := DecodeData(maps, &list); err != nil {
  28. return 0, err
  29. }
  30. loadMaps := map[string]*protoRouteRow{}
  31. for _, row := range list {
  32. loadMaps[row.Route] = row
  33. if row.Request != "" || row.Notify != "" {
  34. row.AllowRequest = true
  35. continue
  36. }
  37. }
  38. p.maps = loadMaps
  39. return len(list), nil
  40. }
  41. func (p *protoRouteConfig) OnAfterLoad(_ bool) {
  42. }
  43. func (p *protoRouteConfig) Contain(route string) int32 {
  44. row, found := p.maps[route]
  45. if !found {
  46. return code.RouteNotFound
  47. }
  48. if !row.Enable {
  49. return code.RouteIsDisabled
  50. }
  51. if !row.AllowRequest {
  52. return code.RouteTypeError
  53. }
  54. return code.OK
  55. }