github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/examples/main.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"embed"
    19  	"os"
    20  
    21  	"github.com/erda-project/erda-infra/base/logs"
    22  	"github.com/erda-project/erda-infra/base/servicehub"
    23  	componentprotocol "github.com/erda-project/erda-infra/providers/component-protocol"
    24  	"github.com/erda-project/erda-infra/providers/component-protocol/cpregister"
    25  	"github.com/erda-project/erda-infra/providers/component-protocol/protocol"
    26  	"github.com/erda-project/erda-infra/providers/i18n"
    27  	_ "github.com/erda-project/erda-infra/providers/serviceregister"
    28  
    29  	// components
    30  	_ "github.com/erda-project/erda-infra/providers/component-protocol/examples/components"
    31  )
    32  
    33  //go:embed scenarios
    34  var scenarioFS embed.FS
    35  
    36  type config struct {
    37  }
    38  
    39  // +provider
    40  type provider struct {
    41  	Cfg *config
    42  	Log logs.Logger
    43  
    44  	Protocol componentprotocol.Interface
    45  	Tran     i18n.I18n
    46  }
    47  
    48  // Init .
    49  func (p *provider) Init(ctx servicehub.Context) error {
    50  	p.Log.Info("init demo")
    51  	p.Protocol.SetI18nTran(p.Tran)          // use custom i18n translator
    52  	p.Protocol.WithContextValue("k1", "v1") // test custom context kv
    53  
    54  	// register protocols
    55  	protocol.MustRegisterProtocolsFromFS(scenarioFS)
    56  
    57  	return nil
    58  }
    59  
    60  func init() {
    61  	servicehub.Register("demo", &servicehub.Spec{
    62  		Services: []string{
    63  			"demo-service",
    64  		},
    65  		Description: "here is description of demo",
    66  		ConfigFunc: func() interface{} {
    67  			return &config{}
    68  		},
    69  		Creator: func() servicehub.Provider {
    70  			return &provider{}
    71  		},
    72  	})
    73  }
    74  
    75  func main() {
    76  	hub := servicehub.New(servicehub.WithListener(cpregister.NewHubListener()))
    77  	hub.Run("demo", "", os.Args...)
    78  }
    79  
    80  // COMMAND: curl -s -XPOST localhost:8080/api/component-protocol/actions/render -H "Content-Type: application/json" \
    81  //               -H "lang: zh" -d '{"scenario":{"scenarioType":"demo","scenarioKey":"demo"}}' | jq
    82  // HTTP RESPONSE OUTPUT:
    83  // {
    84  //  "scenario": {
    85  //    "scenarioKey": "demo"
    86  //  },
    87  //  "protocol": {
    88  //    "scenario": "demo",
    89  //    "hierarchy": {
    90  //      "root": "demoTable",
    91  //      "structure": {
    92  //        "demoTable": []
    93  //      }
    94  //    },
    95  //    "components": {
    96  //      "demoTable": {
    97  //        "type": "Table",
    98  //        "name": "demoTable",
    99  //        "props": {
   100  //          "columns": [
   101  //            {
   102  //              "dataIndex": "sn",
   103  //              "title": "编号"
   104  //            },
   105  //            {
   106  //              "dataIndex": "name",
   107  //              "title": "名字"
   108  //            },
   109  //            {
   110  //              "dataIndex": "helloMsg",
   111  //              "title": "欢迎消息"
   112  //            }
   113  //          ],
   114  //          "pageSizeOptions": [
   115  //            "10",
   116  //            "20",
   117  //            "1000"
   118  //          ],
   119  //          "rowKey": "sn"
   120  //        },
   121  //        "data": {
   122  //          "list": [
   123  //            {
   124  //              "helloMsg": "你好: 张三 (666)",
   125  //              "name": "张三",
   126  //              "sn": "1"
   127  //            },
   128  //            {
   129  //              "helloMsg": "你好 克里托斯",
   130  //              "name": "克里托斯",
   131  //              "sn": "2"
   132  //            }
   133  //          ]
   134  //        }
   135  //      }
   136  //    },
   137  //    "rendering": {
   138  //      "__DefaultRendering__": [
   139  //        {
   140  //          "name": "demoTable",
   141  //          "state": []
   142  //        }
   143  //      ]
   144  //    }
   145  //  }
   146  //}