certichain

Blockchain built from scratch for verifying document's legitimacy.
Log | Files | Refs | README | LICENSE

commit 7aeff6d636c45878eb6af86733b12e883b8a8bcf
parent 44dbe539047889aa4fde9c1e657257d56ee212bc
Author: Shivakumar <[email protected]>
Date:   Sun,  2 Aug 2026 00:41:40 +0530

feat(api): add health endpoint and build a simple http server

Signed-off-by: Shivakumar <[email protected]>

Diffstat:
Aapi/handlers/health.go | 32++++++++++++++++++++++++++++++++
Aapi/router.go | 30++++++++++++++++++++++++++++++
Aapi/server.go | 44++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/api/handlers/health.go b/api/handlers/health.go @@ -0,0 +1,32 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package handlers + +import ( + "encoding/json" + "net/http" +) + +type healthResponse struct { + Status string `json:"status"` +} + +func Health() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(healthResponse{Status: "ok"}) + } +} diff --git a/api/router.go b/api/router.go @@ -0,0 +1,30 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package api + +import ( + "net/http" + + "certichain/api/handlers" +) + +// NewRouter registers every route and returns a composed handlers. +func NewRouter(deps Deps) http.Handler { + mux := http.NewServeMux() + + mux.HandleFunc("GET /healthz", handlers.Health()) + + return mux +} diff --git a/api/server.go b/api/server.go @@ -0,0 +1,44 @@ +// Copyright 2026 Shivakumar (Certichain) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package api + +import ( + "net/http" + "time" + + "certichain/internal/blockchain" + "certichain/internal/mempool" + "certichain/internal/registry" +) + +// Deps holds all internal dependencies the API layer needs to construct all its handlers. +type Deps struct { + Blockchain *blockchain.Blockchain + Registry *registry.Registry + Mempool *mempool.Mempool +} + +// NewServer builds a http server with all the routes and the middleware. +func NewServer(addr string, deps Deps) *http.Server { + mux := NewRouter(deps) + + return &http.Server{ + Addr: addr, + Handler: mux, + ReadTimeout: 5 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 60 * time.Second, + } +}