Merge branch 'main' of git-ssh.tornberg.me:mats/go-cart-actor
This commit is contained in:
140
pool-server.go
140
pool-server.go
@@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
messages "git.tornberg.me/go-cart-actor/proto"
|
||||
@@ -59,6 +61,7 @@ func (s *PoolServer) WriteResult(w http.ResponseWriter, result *FrameWithPayload
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("X-Pod-Name", s.pod_name)
|
||||
w.Header().Set("Access-Control-Allow-Origin", "https://tornberg.me")
|
||||
if result.StatusCode != 200 {
|
||||
log.Printf("Call error: %d\n", result.StatusCode)
|
||||
if result.StatusCode >= 200 && result.StatusCode < 600 {
|
||||
@@ -116,6 +119,137 @@ func (s *PoolServer) HandleSetDelivery(w http.ResponseWriter, r *http.Request) e
|
||||
return s.WriteResult(w, data)
|
||||
}
|
||||
|
||||
func (s *PoolServer) HandleSetPickupPoint(w http.ResponseWriter, r *http.Request) error {
|
||||
id := r.PathValue("id")
|
||||
deliveryIdString := r.PathValue("deliveryId")
|
||||
deliveryId, err := strconv.Atoi(deliveryIdString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pickupPoint := messages.PickupPoint{}
|
||||
err = json.NewDecoder(r.Body).Decode(&pickupPoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reply, err := s.pool.Process(ToCartId(id), Message{
|
||||
Type: SetPickupPointType,
|
||||
Content: &messages.SetPickupPoint{
|
||||
DeliveryId: int64(deliveryId),
|
||||
Id: pickupPoint.Id,
|
||||
Name: pickupPoint.Name,
|
||||
Address: pickupPoint.Address,
|
||||
City: pickupPoint.City,
|
||||
Zip: pickupPoint.Zip,
|
||||
Country: pickupPoint.Country,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.WriteResult(w, reply)
|
||||
}
|
||||
|
||||
func (s *PoolServer) HandleRemoveDelivery(w http.ResponseWriter, r *http.Request) error {
|
||||
id := r.PathValue("id")
|
||||
deliveryIdString := r.PathValue("deliveryId")
|
||||
deliveryId, err := strconv.Atoi(deliveryIdString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reply, err := s.pool.Process(ToCartId(id), Message{
|
||||
Type: RemoveDeliveryType,
|
||||
Content: &messages.RemoveDelivery{Id: int64(deliveryId)},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.WriteResult(w, reply)
|
||||
}
|
||||
|
||||
func (s *PoolServer) HandleQuantityChange(w http.ResponseWriter, r *http.Request) error {
|
||||
id := r.PathValue("id")
|
||||
|
||||
changeQuantity := messages.ChangeQuantity{}
|
||||
err := json.NewDecoder(r.Body).Decode(&changeQuantity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reply, err := s.pool.Process(ToCartId(id), Message{
|
||||
Type: ChangeQuantityType,
|
||||
Content: &changeQuantity,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.WriteResult(w, reply)
|
||||
}
|
||||
|
||||
func (s *PoolServer) HandleAddRequest(w http.ResponseWriter, r *http.Request) error {
|
||||
id := r.PathValue("id")
|
||||
|
||||
addRequest := messages.AddRequest{}
|
||||
err := json.NewDecoder(r.Body).Decode(&addRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reply, err := s.pool.Process(ToCartId(id), Message{
|
||||
Type: AddRequestType,
|
||||
Content: &addRequest,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.WriteResult(w, reply)
|
||||
}
|
||||
|
||||
var (
|
||||
APIUsername = os.Getenv("KLARNA_API_USERNAME")
|
||||
APIPassword = os.Getenv("KLARNA_API_PASSWORD")
|
||||
)
|
||||
|
||||
func (s *PoolServer) HandleCheckout(w http.ResponseWriter, r *http.Request) error {
|
||||
id := r.PathValue("id")
|
||||
|
||||
reply, err := s.pool.Process(ToCartId(id), Message{
|
||||
Type: CreateCheckoutOrderType,
|
||||
Content: &messages.CreateCheckoutOrder{
|
||||
Terms: "https://tornberg.me/terms",
|
||||
Checkout: "https://tornberg.me/checkout",
|
||||
Confirmation: "https://tornberg.me/confirmation",
|
||||
Push: "https://cart.tornberg.me/push",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if reply.StatusCode != 200 {
|
||||
return s.WriteResult(w, reply)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", "https://api.playground.klarna.com/checkout/v3/orders", bytes.NewReader(reply.Payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.SetBasicAuth(APIUsername, APIPassword)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(res.Body)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("X-Pod-Name", s.pod_name)
|
||||
w.Header().Set("Access-Control-Allow-Origin", "https://tornberg.me")
|
||||
w.WriteHeader(res.StatusCode)
|
||||
|
||||
w.Write(buf.Bytes())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PoolServer) Serve() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("OPTIONS /{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -126,7 +260,13 @@ func (s *PoolServer) Serve() *http.ServeMux {
|
||||
})
|
||||
mux.HandleFunc("GET /{id}", ErrorHandler(s.HandleGet))
|
||||
mux.HandleFunc("GET /{id}/add/{sku}", ErrorHandler(s.HandleAddSku))
|
||||
mux.HandleFunc("POST /{id}", ErrorHandler(s.HandleAddRequest))
|
||||
mux.HandleFunc("DELETE /{id}/{itemId}", ErrorHandler(s.HandleDeleteItem))
|
||||
mux.HandleFunc("PUT /{id}", ErrorHandler(s.HandleQuantityChange))
|
||||
mux.HandleFunc("POST /{id}/delivery", ErrorHandler(s.HandleSetDelivery))
|
||||
mux.HandleFunc("DELETE /{id}/delivery/{deliveryId}", ErrorHandler(s.HandleRemoveDelivery))
|
||||
mux.HandleFunc("PUT /{id}/delivery/{deliveryId}/pickupPoint", ErrorHandler(s.HandleSetPickupPoint))
|
||||
mux.HandleFunc("GET /{id}/checkout", ErrorHandler(s.HandleCheckout))
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user