github.com/erda-project/erda-infra@v1.0.9/providers/httpserver/examples/reloadable/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  	"net/http"
    20  	"os"
    21  	"time"
    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/health"
    26  	"github.com/erda-project/erda-infra/providers/httpserver"
    27  )
    28  
    29  type provider struct {
    30  	Log logs.Logger
    31  	rm  httpserver.RouterManager
    32  }
    33  
    34  func (p *provider) Init(ctx servicehub.Context) error {
    35  	p.rm = ctx.Service("http-router-manager",
    36  		func(handler func(ctx httpserver.Context) error) func(ctx httpserver.Context) error {
    37  			return func(ctx httpserver.Context) error {
    38  				p.Log.Info("intercept request", ctx.Request().URL.String())
    39  				return handler(ctx)
    40  			}
    41  		},
    42  	).(httpserver.RouterManager)
    43  
    44  	routes := p.rm.NewRouter()
    45  	routes.GET("/hello",
    46  		func(resp http.ResponseWriter, req *http.Request) {
    47  			resp.Write([]byte("hello"))
    48  		},
    49  	)
    50  
    51  	return routes.Commit()
    52  }
    53  
    54  func (p *provider) Run(ctx context.Context) error {
    55  	select {
    56  	case <-time.After(5 * time.Second):
    57  		p.Log.Infof("routes reload")
    58  		r := p.rm.NewRouter(func(handler func(ctx httpserver.Context) error) func(ctx httpserver.Context) error {
    59  			return func(ctx httpserver.Context) error {
    60  				p.Log.Info("new common intercept request", ctx.Request().URL.String())
    61  				return handler(ctx)
    62  			}
    63  		})
    64  		r.GET("/hello3",
    65  			func(resp http.ResponseWriter, req *http.Request) {
    66  				resp.Write([]byte("hello3"))
    67  			},
    68  			func(handler func(ctx httpserver.Context) error) func(ctx httpserver.Context) error {
    69  				return func(ctx httpserver.Context) error {
    70  					p.Log.Info("new api intercept request", ctx.Request().URL.String())
    71  					return handler(ctx)
    72  				}
    73  			},
    74  		)
    75  		return r.Commit()
    76  	case <-ctx.Done():
    77  	}
    78  	return nil
    79  }
    80  
    81  type provider2 struct {
    82  	Log logs.Logger
    83  }
    84  
    85  func (p *provider2) Init(ctx servicehub.Context) error {
    86  	rm := ctx.Service("http-router-manager",
    87  		func(handler func(ctx httpserver.Context) error) func(ctx httpserver.Context) error {
    88  			return func(ctx httpserver.Context) error {
    89  				p.Log.Info("intercept request", ctx.Request().URL.String())
    90  				return handler(ctx)
    91  			}
    92  		},
    93  	).(httpserver.RouterManager)
    94  
    95  	routes := rm.NewRouter()
    96  	routes.GET("/hello2",
    97  		func(resp http.ResponseWriter, req *http.Request) {
    98  			resp.Write([]byte("hello2"))
    99  		},
   100  	)
   101  	return routes.Commit()
   102  }
   103  
   104  func init() {
   105  	servicehub.Register("hello", &servicehub.Spec{
   106  		Dependencies: []string{"http-server"},
   107  		Creator: func() servicehub.Provider {
   108  			return &provider{}
   109  		},
   110  	})
   111  	servicehub.Register("hello2", &servicehub.Spec{
   112  		Dependencies: []string{"http-server"},
   113  		Creator: func() servicehub.Provider {
   114  			return &provider2{}
   115  		},
   116  	})
   117  }
   118  
   119  func main() {
   120  	hub := servicehub.New()
   121  	hub.Run("examples", "", os.Args...)
   122  }