2024-11-13 21:10:13 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-24 14:04:52 +00:00
|
|
|
"encoding/json"
|
|
|
|
"mic-wallet/common"
|
|
|
|
"mic-wallet/server/core"
|
2024-11-13 21:10:13 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2024-11-24 14:04:52 +00:00
|
|
|
type ApiDependencies struct {
|
|
|
|
*core.BlocksCreator
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
/ -- node info
|
|
|
|
/txs -- add/get txs
|
|
|
|
/blocks -- get/blocks
|
|
|
|
/blocks/current -- get current block info
|
|
|
|
*/
|
|
|
|
|
|
|
|
func NewApi(ctx context.Context, d ApiDependencies, baseRoute string) *http.ServeMux {
|
2024-11-13 21:10:13 +00:00
|
|
|
if baseRoute == "" {
|
|
|
|
baseRoute = "/"
|
|
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
2024-11-24 14:04:52 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
mux.HandleFunc(baseRoute, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/" {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, _ := json.Marshal(common.NodeInfo{
|
|
|
|
NodeVersion: common.VersionNumberStr,
|
|
|
|
})
|
|
|
|
w.Write(data)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
})
|
2024-11-13 21:10:13 +00:00
|
|
|
return mux
|
|
|
|
}
|