/
db.go
145 lines (114 loc) · 2.64 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package entities
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"os"
"sync"
"github.com/crossphoton/status-cron/src/config"
"github.com/crossphoton/status-cron/src/utils"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type ServiceData map[string]interface{}
func (a *ServiceData) Value() (driver.Value, error) {
return json.Marshal(a)
}
func (a *ServiceData) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
return json.Unmarshal(b, &a)
}
type Service struct {
Id int64 `mapstructure:"id" json:"id" gorm:"primaryKey"`
Name string `mapstructure:"name" json:"name"`
Url string `mapstructure:"url" json:"url"`
Type ServiceType `mapstructure:"type" json:"type"`
Cron string `mapstructure:"cron" json:"cron"`
Data ServiceData `mapstructure:"data" json:"data"`
}
type DB interface {
Connect()
GetServices() []Service
SaveResult(Result)
Close()
}
var DB_instance DB
type DB_TYPE string
const (
Mongo_DB DB_TYPE = "mongo"
Postgres_DB DB_TYPE = "postgres"
JSON DB_TYPE = "json"
)
type PostgresDB struct {
client *gorm.DB
}
func (db *PostgresDB) Connect() {
var err error
dsn := config.GetPostgresURI()
db.client, err = gorm.Open(postgres.New(postgres.Config{
DSN: dsn,
}))
if err != nil {
panic(err)
}
db.client.AutoMigrate(&Service{}, &Result{})
utils.Logger.Info("connected to database")
}
func (db *PostgresDB) GetServices() []Service {
var services []Service
res := db.client.Find(&services)
if res.Error != nil {
utils.Logger.Error(fmt.Sprint("couldn't connect to database", res.Error.Error()))
return []Service{}
}
return services
}
func (db *PostgresDB) SaveResult(res Result) {
db.client.Create(&res)
}
func (db *PostgresDB) Close() {
}
type JsonFile struct {
Services []Service `json:"services"`
Results []Result `json:"results"`
}
type JsonDB struct {
file JsonFile
filepath string
lock sync.Mutex
}
func (db *JsonDB) Connect() {
db.filepath = config.GetJSONFilePath()
file, err := os.ReadFile(db.filepath)
if err != nil {
panic(err)
}
json.Unmarshal(file, &(db.file))
}
func (db *JsonDB) GetServices() []Service {
return db.file.Services
}
func (db *JsonDB) SaveResult(res Result) {
db.lock.Lock()
db.file.Results = append(db.file.Results, res)
db.lock.Unlock()
}
func (db *JsonDB) Close() {
data, err := json.Marshal(db.file)
if err != nil {
panic(err)
}
os.WriteFile(db.filepath, data, 0777)
}
var db_map = map[DB_TYPE]DB{
Postgres_DB: &PostgresDB{},
JSON: &JsonDB{},
}
func init() {
DB_instance = db_map[DB_TYPE(config.DB_TYPE)]
DB_instance.Connect()
}