-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
88 lines (71 loc) · 1.82 KB
/
database.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
package main
import (
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/kataras/iris/v12"
"io/ioutil"
"os"
)
var MYSQLDB *gorm.DB
type dbConfiguration struct {
DBHost string `json:"dbHost"`
DBPort string `json:"dbPort"`
DBName string `json:"dbName"`
DBUser string `json:"dbUser"`
DBPass string `json:"dbPass"`
}
type User struct {
Username string `json:"user" gorm:"primary_key"`
Password string `json:"pass"`
}
//type CreateBookInput struct {
// Title string `json:"title" binding:"required"`
// Author string `json:"author" binding:"required"`
//}
//
//type UpdateBookInput struct {
// Title string `json:"title"`
// Author string `json:"author"`
//}
func ConnectMySQL() {
file, _ := os.Open("conf.json")
defer file.Close()
decoder := json.NewDecoder(file)
var dbConf dbConfiguration
_ = decoder.Decode(&dbConf)
dst := dbConf.DBUser+":"+dbConf.DBPass+"@tcp("+dbConf.DBHost+":"+dbConf.DBPort+")/"+dbConf.DBName
database, err := gorm.Open("mysql", dst+"?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic("Failed to connect to database!")
}
database.AutoMigrate(&User{})
// CREATE Table
MYSQLDB = database
admin := User{
Username: "admin",
Password: "password",
}
MYSQLDB.Create(&admin)
}
func createJSON()iris.Map{
// Function Arguments need to contain db access infos
// store data
DBUsername := "test1"
DBProfilepic := "/user/test1/pic/1.jpg"
DBLastLogin := "2021-3-21 10:34"
data, err := ioutil.ReadFile("userdata/task/task.md")
DBTaskList := []byte("Default")
if err != nil{
DBTaskList = []byte("# No task")
} else {
DBTaskList = []byte(string(data))
}
// Return iris Map type
return iris.Map{
"username" : DBUsername,
"profile_pic" : DBProfilepic,
"last_login" : DBLastLogin,
"task_list" : DBTaskList,
}
}