github.com/erda-project/erda-infra@v1.0.9/providers/legacy/httpendpoints/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  	"context"
    19  	"fmt"
    20  	"net/http"
    21  	"os"
    22  
    23  	"github.com/erda-project/erda-infra/base/logs"
    24  	"github.com/erda-project/erda-infra/base/servicehub"
    25  	"github.com/erda-project/erda-infra/providers/legacy/httpendpoints"
    26  	_ "github.com/erda-project/erda-infra/providers/legacy/httpendpoints"
    27  	"github.com/erda-project/erda-infra/providers/legacy/httpendpoints/errorresp"
    28  )
    29  
    30  type config struct{}
    31  
    32  type provider struct {
    33  	C *config     // auto inject this field
    34  	L logs.Logger // auto inject this field
    35  }
    36  
    37  func (p *provider) Init(ctx servicehub.Context) error {
    38  	// register some apis
    39  	server := ctx.Service("http-endpoints").(httpendpoints.Interface)
    40  	server.RegisterEndpoints([]httpendpoints.Endpoint{
    41  		httpendpoints.Endpoint{
    42  			Path:    "/hello",
    43  			Method:  http.MethodGet,
    44  			Handler: p.Hello,
    45  		},
    46  		httpendpoints.Endpoint{
    47  			Path:    "/error",
    48  			Method:  http.MethodGet,
    49  			Handler: p.Error,
    50  		},
    51  	})
    52  	return nil
    53  }
    54  
    55  func (p *provider) Hello(ctx context.Context, r *http.Request, vars map[string]string) (
    56  	httpendpoints.Responser, error) {
    57  	return httpendpoints.OkResp(map[string]interface{}{
    58  		"message": "ok",
    59  	})
    60  }
    61  
    62  func (p *provider) Error(ctx context.Context, r *http.Request, vars map[string]string) (
    63  	httpendpoints.Responser, error) {
    64  	return errorresp.ErrResp(fmt.Errorf("example error"))
    65  }
    66  
    67  func init() {
    68  	servicehub.Register("example", &servicehub.Spec{
    69  		Services:     []string{"example"},
    70  		Dependencies: []string{"http-endpoints"},
    71  		Description:  "example",
    72  		ConfigFunc:   func() interface{} { return &config{} },
    73  		Creator:      func() servicehub.Provider { return &provider{} },
    74  	})
    75  }
    76  
    77  func main() {
    78  	hub := servicehub.New()
    79  	hub.Run("examples", "", os.Args...)
    80  }