-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_firebase.go
158 lines (135 loc) · 3.8 KB
/
main_firebase.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
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
)
type ProcessExcecution struct {
StartedAt time.Time
EndendAt time.Time
}
type UserStruct struct {
FirstName string `bson:"first_name" json:"first_name"`
LastName string `bson:"last_name" json:"last_name"`
Process map[string][]ProcessExcecution `bson:"process" json:"process"`
LastProcess []string `bson:"last_process" json:"last_process"`
LastRequest time.Time
}
type DataStruct struct {
ID string `json:"id"`
Process [][]string `json:"process"`
}
type Database struct {
Collection *firestore.CollectionRef
}
type Response struct {
ID string `json:"id"`
Status string `json:"status"`
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (database *Database) handler(w http.ResponseWriter, r *http.Request) {
var data DataStruct
var user UserStruct
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "invalid_http_method")
return
}
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
panic(err)
}
doc, err := database.Collection.Doc(data.ID).Get(context.Background())
if err != nil {
panic(err)
} else {
err = doc.DataTo(&user)
if err != nil {
panic(err)
}
log.Println(user.FirstName)
if user.Process == nil {
user.Process = make(map[string][]ProcessExcecution)
}
timing := time.Now()
for i, dataToProcess := range data.Process {
now := timing.Add(time.Duration(-(len(data.Process) - 1 - i)) * time.Minute)
for _, dataProcess := range dataToProcess {
if user.Process[dataProcess] != nil && contains(user.LastProcess, dataProcess) && now.Unix()-user.LastRequest.Unix() < 120 {
log.Println("process exist")
user.Process[dataProcess][len(user.Process[dataProcess])-1].EndendAt = now
} else {
var newProcess ProcessExcecution
newProcess.StartedAt = now
newProcess.EndendAt = now
user.Process[dataProcess] = append(user.Process[dataProcess], newProcess)
}
}
user.LastProcess = dataToProcess
user.LastRequest = now
}
_, err = database.Collection.Doc(data.ID).Set(context.Background(), user)
response, _ := json.Marshal(Response{Status: "OK"})
if err != nil {
response, _ = json.Marshal(Response{Status: "Error"})
panic(err)
}
fmt.Fprintf(w, string(response))
}
}
func (database *Database) userHandler(w http.ResponseWriter, r *http.Request) {
var user UserStruct
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "invalid_http_method")
return
}
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
panic(err)
}
if user.FirstName != "" && user.LastName != "" {
ref, _, err := database.Collection.Add(context.Background(), user)
if err != nil {
panic(err)
}
response, err := json.Marshal(Response{ID: ref.ID, Status: "OK"})
fmt.Fprintf(w, string(response))
}
}
func main() {
var database Database
opt := option.WithCredentialsFile("./firebase_key.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatal(err)
}
client, err := app.Firestore(context.Background())
if err != nil {
log.Fatal(err)
}
defer client.Close()
database.Collection = client.Collection(os.Getenv("ENTERPRISE"))
log.Println("Setting database for : " + os.Getenv("ENTERPRISE"))
http.HandleFunc("/", database.handler)
http.HandleFunc("/user", database.userHandler)
if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil {
log.Fatal(err)
} else {
log.Println("server up and running on port :" + os.Getenv("ENTERPRISE"))
}
}