micw/server/api/http/api.go
2024-11-24 15:04:52 +01:00

49 lines
874 B
Go

package http
import (
"context"
"encoding/json"
"mic-wallet/common"
"mic-wallet/server/core"
"net/http"
)
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 {
if baseRoute == "" {
baseRoute = "/"
}
mux := http.NewServeMux()
/*
*/
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)
})
return mux
}