souliyappps commited on
Commit
ca52144
·
1 Parent(s): 18d64e4

feat: implement multi-stage docker build, auth middleware and order status enhancements

Browse files
Files changed (3) hide show
  1. Dockerfile +21 -4
  2. cmd/exec.go +1 -1
  3. cmd/orders.go +20 -5
Dockerfile CHANGED
@@ -1,15 +1,32 @@
1
- FROM golang:1.23-alpine
 
2
 
3
  WORKDIR /app
4
 
 
5
  COPY go.mod go.sum ./
6
-
7
  RUN go mod download
8
 
 
9
  COPY . .
10
 
11
- RUN go build -o api
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  EXPOSE 8080
14
 
15
- CMD ["./api", "serve", "--http=0.0.0.0:8080"]
 
 
1
+ # --- Builder Stage ---
2
+ FROM golang:1.23-alpine AS builder
3
 
4
  WORKDIR /app
5
 
6
+ # Copy dependencies first for better caching
7
  COPY go.mod go.sum ./
 
8
  RUN go mod download
9
 
10
+ # Copy the rest of the application source code
11
  COPY . .
12
 
13
+ # Build the application
14
+ # Using CGO_ENABLED=0 to create a static binary
15
+ # Using -ldflags "-s -w" to strip debug information and make the binary smaller
16
+ RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o /api .
17
+
18
+ # --- Final Stage ---
19
+ FROM alpine:latest
20
+
21
+ WORKDIR /app
22
+
23
+ # Copy the static binary from the builder stage
24
+ COPY --from=builder /api /app/api
25
+
26
+ # Copy the PocketBase data directory
27
+ COPY pb_data ./pb_data
28
 
29
  EXPOSE 8080
30
 
31
+ # Set the entrypoint
32
+ CMD ["/app/api", "serve", "--http=0.0.0.0:8080"]
cmd/exec.go CHANGED
@@ -137,7 +137,7 @@ func Execute(app *pocketbase.PocketBase) (*pocketbase.PocketBase, error) {
137
 
138
  return apis.RecordAuthResponse(e, record, "phone", map[string]any{"token_duration": 315360000})
139
  })
140
- customer := se.Router.Group("/cust")
141
  customer.POST("/checkout", Checkout)
142
  customer.GET("/order-list", ListOrdersByCustID)
143
 
 
137
 
138
  return apis.RecordAuthResponse(e, record, "phone", map[string]any{"token_duration": 315360000})
139
  })
140
+ customer := se.Router.Group("/cust").Bind(apis.RequireAuth("customers"))
141
  customer.POST("/checkout", Checkout)
142
  customer.GET("/order-list", ListOrdersByCustID)
143
 
cmd/orders.go CHANGED
@@ -11,14 +11,16 @@ import (
11
  type OrderStatus string
12
 
13
  var (
14
- OrderStatusPending OrderStatus = "pending"
15
- OrderStatusPurchased OrderStatus = "purchased"
16
- OrderStatusCancel OrderStatus = "cancel"
 
 
17
  )
18
 
19
  func (s *OrderStatus) UnmarshalText(text []byte) error {
20
  switch status := string(text); status {
21
- case string(OrderStatusPending), string(OrderStatusPurchased), string(OrderStatusCancel):
22
  *s = OrderStatus(status)
23
  return nil
24
  default:
@@ -44,6 +46,7 @@ type Order struct {
44
 
45
  func ListOrders(e *core.RequestEvent) error {
46
  statusParam := e.Request.URL.Query().Get("status")
 
47
 
48
  query := app.DB().
49
  Select(
@@ -81,6 +84,11 @@ func ListOrders(e *core.RequestEvent) error {
81
  subquery = subquery.Where(dbx.NewExp("status = {:status}", dbx.Params{"status": string(status)}))
82
  }
83
 
 
 
 
 
 
84
  searchProvider := NewProvider().Query(query).SubQuery(subquery)
85
 
86
  records := make([]Order, 0)
@@ -94,12 +102,14 @@ func ListOrders(e *core.RequestEvent) error {
94
  }
95
 
96
  func ListOrdersByCustID(e *core.RequestEvent) error {
97
- custId := e.Request.URL.Query().Get("customer_id")
98
  if custId == "" {
 
99
  return e.BadRequestError("Missing or invalid customer ID", nil)
100
  }
101
 
102
  statusParam := e.Request.URL.Query().Get("status")
 
103
 
104
  query := app.DB().
105
  Select(
@@ -136,6 +146,11 @@ func ListOrdersByCustID(e *core.RequestEvent) error {
136
  subquery = subquery.Where(dbx.NewExp("status = {:status}", dbx.Params{"status": string(status)}))
137
  }
138
 
 
 
 
 
 
139
  // Add ordering for consistent results
140
  query = query.OrderBy("o.created DESC")
141
 
 
11
  type OrderStatus string
12
 
13
  var (
14
+ OrderStatusPending OrderStatus = "pending"
15
+ OrderStatusPurchased OrderStatus = "purchased"
16
+ OrderStatusCancel OrderStatus = "cancel"
17
+ OrderStatusDelivering OrderStatus = "delivering"
18
+ OrderStatusCompleted OrderStatus = "completed"
19
  )
20
 
21
  func (s *OrderStatus) UnmarshalText(text []byte) error {
22
  switch status := string(text); status {
23
+ case string(OrderStatusPending), string(OrderStatusPurchased), string(OrderStatusCancel), string(OrderStatusDelivering), string(OrderStatusCompleted):
24
  *s = OrderStatus(status)
25
  return nil
26
  default:
 
46
 
47
  func ListOrders(e *core.RequestEvent) error {
48
  statusParam := e.Request.URL.Query().Get("status")
49
+ createdAtParam := e.Request.URL.Query().Get("createdAt")
50
 
51
  query := app.DB().
52
  Select(
 
84
  subquery = subquery.Where(dbx.NewExp("status = {:status}", dbx.Params{"status": string(status)}))
85
  }
86
 
87
+ if createdAtParam != "" {
88
+ query = query.Where(dbx.NewExp("DATE(o.created) = {:created}", dbx.Params{"created": createdAtParam}))
89
+ subquery = subquery.Where(dbx.NewExp("DATE(created) = {:created}", dbx.Params{"created": createdAtParam}))
90
+ }
91
+
92
  searchProvider := NewProvider().Query(query).SubQuery(subquery)
93
 
94
  records := make([]Order, 0)
 
102
  }
103
 
104
  func ListOrdersByCustID(e *core.RequestEvent) error {
105
+ custId := e.Auth.Id
106
  if custId == "" {
107
+ e.App.Logger().Error("Missing or invalid customer ID", "auth_id", custId, "request_event", e)
108
  return e.BadRequestError("Missing or invalid customer ID", nil)
109
  }
110
 
111
  statusParam := e.Request.URL.Query().Get("status")
112
+ createdAtParam := e.Request.URL.Query().Get("createdAt")
113
 
114
  query := app.DB().
115
  Select(
 
146
  subquery = subquery.Where(dbx.NewExp("status = {:status}", dbx.Params{"status": string(status)}))
147
  }
148
 
149
+ if createdAtParam != "" {
150
+ query = query.Where(dbx.NewExp("DATE(o.created) = {:created}", dbx.Params{"created": createdAtParam}))
151
+ subquery = subquery.Where(dbx.NewExp("DATE(created) = {:created}", dbx.Params{"created": createdAtParam}))
152
+ }
153
+
154
  // Add ordering for consistent results
155
  query = query.OrderBy("o.created DESC")
156