/
main.go
165 lines (139 loc) · 4.64 KB
/
main.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
159
160
161
162
163
164
165
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-pg/pg/v10"
"github.com/google/uuid"
)
type User struct {
ID uuid.UUID `pg:"type:uuid,default:gen_random_uuid()" json:"id"`
Name string `json:"name"`
Purchases []Purchase `pg:"-" json:"purchases,omitempty"`
}
type Event struct {
ID uuid.UUID `pg:"type:uuid,default:gen_random_uuid()" json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Status string `json:"status"`
Purchases []Purchase `pg:"-" json:"purchases,omitempty"`
}
type Purchase struct {
ID uuid.UUID `pg:"type:uuid,default:gen_random_uuid()" json:"id"`
UserID uuid.UUID `pg:"type:uuid" json:"userId"`
EventID uuid.UUID `pg:"type:uuid" json:"eventId"`
Status string `json:"status"`
User *User `pg:"rel:has-one" json:"user"`
Event *Event `pg:"rel:has-one" json:"event"`
}
var db *pg.DB
func main() {
var homeCertDir = os.Getenv("TICKETS_CERTS")
caCert, err := ioutil.ReadFile(homeCertDir + "/jlevi-ca.crt")
if err != nil {
log.Fatalf("Unable to read CA cert: %v", err)
}
// Create a certificate pool and add the CA certificate
caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
log.Fatalf("Failed to append CA certificate to pool")
}
// Set up TLS configuration
tlsConfig := &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: true, // Do not skip server certificate verification
}
db = pg.Connect(&pg.Options{
Addr: "host:26257",
User: "",
Password: "",
Database: "tickets",
PoolSize: 20,
ApplicationName: "gopg-app",
// TLS settings
TLSConfig: tlsConfig,
})
defer db.Close()
// Set application name using Exec
q, err := db.Exec("SET application_name = 'gopg-app'")
if err != nil {
panic(err)
fmt.Println(q)
}
r := gin.Default()
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://192.168.86.202:3000"} // Replace with your React frontend's address
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE"}
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type"}
r.Use(cors.New(config))
r.GET("/user/:userID/purchases", getUserPurchases)
r.GET("/user/:userID/purchases/cancellations", getUserCancelledPurchases)
r.GET("/search/user/:userID", searchUsers)
r.Run(":3001") // Listen and serve on 0.0.0.0:3001
}
func getUserPurchases(c *gin.Context) {
userID := c.Param("userID")
uuidUserID, err := uuid.Parse(userID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
return
}
var purchases []Purchase
query := db.Model(&purchases).
ColumnExpr("purchase.id, purchase.user_id, purchase.event_id, purchase.status, users.id AS user__id, users.name AS user__name, events.id AS event__id, events.name AS event__name, events.type AS event__type, events.status AS event__status").
Join("LEFT JOIN users AS users ON users.id = purchase.user_id").
Join("LEFT JOIN events AS events ON events.id = purchase.event_id").
Where("purchase.user_id = ?", uuidUserID)
err = query.Select(&purchases)
if err != nil {
fmt.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
return
}
c.JSON(http.StatusOK, purchases)
}
func getUserCancelledPurchases(c *gin.Context) {
userID := c.Param("userID")
uuidUserID, err := uuid.Parse(userID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
return
}
var purchases []Purchase
err = db.Model(&purchases).
ColumnExpr("purchase.id, purchase.user_id, purchase.event_id, purchase.status, users.id AS user__id, users.name AS user__name, events.id AS event__id, events.name AS event__name, events.type AS event__type, events.status AS event__status").
Join("LEFT JOIN users AS users ON users.id = purchase.user_id").
Join("LEFT JOIN events AS events ON events.id = purchase.event_id").
Where("purchase.user_id = ? AND purchase.status = 'cancelled'", uuidUserID).
Select(&purchases)
if err != nil {
fmt.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
return
}
c.JSON(http.StatusOK, purchases)
}
func searchUsers(c *gin.Context) {
// userid := c.Query("id")
userID := c.Param("userID")
uuidUserID, err := uuid.Parse(userID)
var users []User
err = db.Model(&users).
Where("id = ?", uuidUserID).
Select(&users)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
// fmt.Println(u)
return
}
if users == nil {
users = []User{}
}
c.JSON(http.StatusOK, users)
}