/
config.go
79 lines (74 loc) · 2.06 KB
/
config.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
package sql
import (
"encoding/json"
"os"
)
const COMMIT_HASH = ""
type Config struct {
DatabaseName string `json:"database_name"`
DatabaseConfig string `json:"database_config"`
Debug bool `json:"debug"`
Host string `json:"host"`
CommitHash string `json:"commit_hash"`
RemoteRepository string `json:"remote_repository"`
SchoolName string `json:"school_name"`
SchoolAddress string `json:"school_address"`
SchoolCity string `json:"school_city"`
SchoolCountry string `json:"school_country"`
SchoolPostCode int `json:"school_post_code"`
ParentViewGrades bool `json:"parent_view_grades"`
ParentViewAbsences bool `json:"parent_view_absences"`
ParentViewHomework bool `json:"parent_view_homework"`
ParentViewGradings bool `json:"parent_view_gradings"`
BlockRegistrations bool `json:"block_registrations"`
BlockMeals bool `json:"block_meals"`
SchoolFreeDays []string `json:"school_free_days"`
}
func GetConfig() (Config, error) {
var config Config
file, err := os.ReadFile("config.json")
if err != nil {
marshal, err := json.Marshal(Config{
DatabaseName: "sqlite3",
DatabaseConfig: "MeetPlanDB/meetplan.db",
Debug: true,
Host: "127.0.0.1:8000",
CommitHash: COMMIT_HASH,
RemoteRepository: "https://github.com/MeetPlan/MeetPlanBackend",
})
if err != nil {
return config, err
}
err = os.WriteFile("config.json", marshal, 0600)
if err != nil {
return config, err
}
file, err = os.ReadFile("config.json")
if err != nil {
return config, err
}
}
err = json.Unmarshal(file, &config)
if err != nil {
return config, err
}
return config, err
}
func SaveConfig(config Config) error {
marshal, err := json.Marshal(config)
if err != nil {
return err
}
f, err := os.Create("config.json")
if err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
err = os.WriteFile("config.json", marshal, 0600)
if err != nil {
return err
}
return nil
}