add model

This commit is contained in:
SmileRex
2025-01-23 05:37:47 +03:00
parent fa97251aa9
commit 1970f09678
3 changed files with 13 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"bot/src/controllers"
"bot/src/handlers"
"bot/src/models"
"log"
"os"
"time"
@@ -11,7 +12,7 @@ import (
"gopkg.in/yaml.v3"
)
func config() controllers.Config {
func config() models.Config {
file, err := os.Open("cfg.yml")
if err != nil {
log.Fatal(err)
@@ -19,7 +20,7 @@ func config() controllers.Config {
defer file.Close()
// unmarshal the YAML data into a struct
var config controllers.Config
var config models.Config
err = yaml.NewDecoder(file).Decode(&config)
if err != nil {
log.Fatal(err)

View File

@@ -11,17 +11,9 @@ import (
_ "github.com/go-sql-driver/mysql"
)
type Config struct {
Token string `yaml:"token"`
UserName string `yaml:"user_name"`
Password string `yaml:"password"`
HostName string `yaml:"host_name"`
DbName string `yaml:"db_name"`
}
type DataController struct{ DB *sql.DB }
func NewDB(config Config) *DataController {
func NewDB(config models.Config) *DataController {
dbaddr := fmt.Sprintf("%s:%s@tcp(%s)/%s", config.UserName, config.Password, config.HostName, config.DbName)
db, err := sql.Open("mysql", dbaddr)

9
src/models/config.go Normal file
View File

@@ -0,0 +1,9 @@
package models
type Config struct {
Token string `yaml:"token"`
UserName string `yaml:"user_name"`
Password string `yaml:"password"`
HostName string `yaml:"host_name"`
DbName string `yaml:"db_name"`
}