|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gin-gonic/gin" |
| 5 | + "gopkg.in/yaml.v3" |
| 6 | + "gorm.io/gorm" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing_system/clients/common/clientconfig" |
| 10 | + "testing_system/common/connectors/masterconn" |
| 11 | + "testing_system/common/connectors/storageconn" |
| 12 | + "testing_system/common/db" |
| 13 | + "testing_system/lib/logger" |
| 14 | +) |
| 15 | + |
| 16 | +type ClientBase struct { |
| 17 | + Config clientconfig.Config |
| 18 | + |
| 19 | + Router *gin.Engine |
| 20 | + |
| 21 | + StorageConnection *storageconn.Connector |
| 22 | + MasterConnection *masterconn.Connector |
| 23 | + DB *gorm.DB |
| 24 | +} |
| 25 | + |
| 26 | +func NewClientBase(configPath string) *ClientBase { |
| 27 | + var config clientconfig.Config |
| 28 | + configData, err := os.ReadFile(configPath) |
| 29 | + if err != nil { |
| 30 | + panic(err) |
| 31 | + } |
| 32 | + err = yaml.Unmarshal(configData, &config) |
| 33 | + if err != nil { |
| 34 | + panic(err) |
| 35 | + } |
| 36 | + |
| 37 | + logger.InitLogger(config.Logger) |
| 38 | + |
| 39 | + base := &ClientBase{ |
| 40 | + Config: config, |
| 41 | + StorageConnection: storageconn.NewConnector(config.StorageConnection), |
| 42 | + MasterConnection: masterconn.NewConnector(config.MasterConnection), |
| 43 | + } |
| 44 | + |
| 45 | + base.DB, err = db.NewDB(config.DB) |
| 46 | + if err != nil { |
| 47 | + logger.Panic("Can not set up testing system db, error: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + base.Router = gin.Default() |
| 51 | + |
| 52 | + base.Router.Static("/static", filepath.Join(base.Config.ResourcesPath, "static")) |
| 53 | + base.Router.LoadHTMLGlob(filepath.Join(base.Config.ResourcesPath, "templates/*")) |
| 54 | + return base |
| 55 | +} |
| 56 | + |
| 57 | +func (b *ClientBase) Run() { |
| 58 | + err := b.Router.Run(b.Config.Address) |
| 59 | + if err != nil { |
| 60 | + logger.Panic("Can not start client handler, error: %v", err) |
| 61 | + } |
| 62 | +} |
0 commit comments