github.com/erda-project/erda-infra@v1.0.9/providers/mysql/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 "os" 21 22 "github.com/jinzhu/gorm" 23 24 "github.com/erda-project/erda-infra/base/servicehub" 25 "github.com/erda-project/erda-infra/providers/mysql" 26 ) 27 28 type provider struct { 29 DB *gorm.DB // autowired 30 MySQL mysql.Interface // autowired 31 } 32 33 func (p *provider) Init(ctx servicehub.Context) error { 34 fmt.Println(p.DB) 35 fmt.Println(p.MySQL) 36 // do something 37 return nil 38 } 39 40 func (p *provider) Run(ctx context.Context) error { 41 if err := p.DB.Exec("SELECT 1").Error; err != nil { 42 return err 43 } 44 for { 45 select { 46 case <-ctx.Done(): 47 return nil 48 } 49 } 50 } 51 52 func init() { 53 servicehub.Register("example", &servicehub.Spec{ 54 Services: []string{"example"}, 55 Dependencies: []string{"mysql"}, 56 Description: "example", 57 Creator: func() servicehub.Provider { 58 return &provider{} 59 }, 60 }) 61 } 62 63 func main() { 64 hub := servicehub.New() 65 hub.Run("examples", "", os.Args...) 66 }