Weight.go
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// Mocked utility functions and structures for demonstration purposes
type SignedTransaction struct {
Transaction map[string]interface{} `json:"transaction"`
PublicKey string `json:"public_key"`
Signature string `json:"signature"`
}
func NewSignedTransaction(item map[string]interface{}) *SignedTransaction {
return &SignedTransaction{
Transaction: item["transaction"].(map[string]interface{}),
PublicKey: item["public_key"].(string),
Signature: item["signature"].(string),
}
}
type Machine struct{}
func (m *Machine) Instance() *Machine {
return &Machine{}
}
func (m *Machine) Weight(tx *SignedTransaction, errMsg *string) *string {
// ์ค์ weight ๊ณ์ฐ ๋ก์ง ๊ตฌํ
weight := "mocked_weight"
return &weight
}
func MachineInstance() *Machine {
return &Machine{}
}
// WeightHandler handles the weight calculation.
type WeightHandler struct{}
func (w *WeightHandler) Fail(wr http.ResponseWriter, result, errMsg string) {
http.Error(wr, fmt.Sprintf("%s: %s", result, errMsg), http.StatusBadRequest)
}
func (w *WeightHandler) Main(wr http.ResponseWriter, req *http.Request) {
var item map[string]interface{}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
w.Fail(wr, "FAIL", "Failed to read request body")
return
}
if err := json.Unmarshal(body, &item); err != nil {
w.Fail(wr, "FAIL", "Invalid JSON format")
return
}
tx := NewSignedTransaction(item)
var errMsg string
weight := MachineInstance().Weight(tx, &errMsg)
if weight == nil {
w.Fail(wr, "FAIL", errMsg)
return
}
fmt.Fprintf(wr, *weight)
}
func main() {
http.HandleFunc("/weight", func(w http.ResponseWriter, req *http.Request) {
handler := &WeightHandler{}
handler.Main(w, req)
})
http.ListenAndServe(":8080", nil)
}์ฃผ์ ๊ตฌ์ฑ ์์์ ํจ์
์์ฝ
Last updated