-
Notifications
You must be signed in to change notification settings - Fork 0
/
objStructs.go
137 lines (117 loc) · 2.9 KB
/
objStructs.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
package utils
import (
"fmt"
"os"
"strings"
"time"
"github.com/labstack/echo/v4"
)
type ErrorMessage struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (e *ErrorMessage) Error() string {
return fmt.Sprintf("Error %d: %s", e.Code, e.Message)
}
type SuccessMessage struct {
Message string `json:"message"`
}
type Log struct {
ContentLength string
Error error
Host string
Latency time.Duration
RemoteIP string
ResponseSize int64
Time time.Time
Status int
URI string
Protocol string
}
type S3_File struct {
LocalFilePath string
BucketObjectKey string
}
func S3_DownloadFiles() error {
for _, file := range S3_Files {
fileData, err := DownloadFile(BUCKET_NAME,
file.BucketObjectKey,
BUCKET_REGION)
if err != nil {
return &ServerError{
Err: err,
Code: echo.ErrInternalServerError.Code,
Simple: "Error downloading file From bucket. Err: " + err.Error(),
}
}
// If file exists, update it. Else create the dirs to the file
if FileExists(file.LocalFilePath) {
localFile, err := os.OpenFile(file.LocalFilePath, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return &ServerError{
Err: err,
Code: echo.ErrInternalServerError.Code,
Simple: "Error opening file for writing. path: " + file.LocalFilePath,
}
}
if _, err := localFile.Write(fileData); err != nil {
return &ServerError{
Err: err,
Code: echo.ErrInternalServerError.Code,
Simple: "Error writing to local file. Err: " + err.Error(),
}
}
localFile.Close()
} else {
// Create the dirs leading to the file
path_split := strings.Split(file.LocalFilePath, "/")
mkdirPath := strings.Join(path_split[:len(path_split)-1], "/")
err = os.MkdirAll(mkdirPath, os.ModePerm)
if err != nil {
panic(err)
}
// Writing the file to dir
if err := os.WriteFile(file.LocalFilePath, fileData, 0644); err != nil {
return &ServerError{
Err: err,
Code: echo.ErrInternalServerError.Code,
Simple: "Error writing file data to local storage. path: " + file.LocalFilePath,
}
}
}
}
return nil
}
// Currently only 2 files
// The resume pdf and png
func InitVars() {
S3_Files = []S3_File{
{
LocalFilePath: LOCAL_RESUME_PATH,
BucketObjectKey: OBJ_RESUME_KEY,
},
{
LocalFilePath: LOCAL_RESUME_PNG_PATH,
BucketObjectKey: OBJ_RESUME_PNG_KEY,
},
{
LocalFilePath: LOCAL_RESUME_HTML_PATH,
BucketObjectKey: OBJ_RESUME_HTML_KEY,
},
}
}
var (
S3_Files = []S3_File{}
)
type UserProfile struct {
Id int `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
}
// The Id in UserProfile defaults to 0 which results in a conflict
type UserRegister struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
}