Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkamalesh committed Jan 26, 2025
1 parent 5c71181 commit 6596c71
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
12 changes: 0 additions & 12 deletions cmd/server/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ func (h *Handlers) routes() []*webgo.Route {
}
}

// Health is the HTTP handler to return the status of the app including the version, and other details
// This handler uses webgo to respond to the http request
func (h *Handlers) Health(w http.ResponseWriter, r *http.Request) error {
out, err := h.apis.ServerHealth()
if err != nil {
return err
}
webgo.R200(w, out)
return nil
}

// HelloWorld is a helloworld HTTP handler
func (h *Handlers) HelloWorld(w http.ResponseWriter, r *http.Request) error {
contentType := r.Header.Get("Content-Type")
switch contentType {
Expand Down
38 changes: 36 additions & 2 deletions inits.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package main

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/naughtygopher/errors"
"github.com/naughtygopher/proberesponder"
proberespHTTP "github.com/naughtygopher/proberesponder/extensions/http"
"github.com/naughtygopher/webgo/v7"

"github.com/naughtygopher/goapp/cmd/server/grpc"
xhttp "github.com/naughtygopher/goapp/cmd/server/http"
Expand All @@ -19,6 +22,8 @@ import (
"github.com/naughtygopher/goapp/internal/users"
)

var now = time.Now()

func startAPM(ctx context.Context, cfg *configs.Configs) *apm.APM {
ap, err := apm.New(ctx, &apm.Options{
Debug: cfg.Environment == configs.EnvLocal,
Expand Down Expand Up @@ -51,14 +56,43 @@ func startServers(svr api.Server, cfgs *configs.Configs, fatalErr chan<- error)
return hserver, nil
}

func startHealthResponder(ctx context.Context, ps *proberesponder.ProbeResponder, api *api.API, fatalErr chan<- error) (*http.Server, error) {
func healthResponseHandler(ps *proberesponder.ProbeResponder) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
payload := map[string]any{
"env": "testing",
"version": "v0.1.0",
"commit": "<git commit hash>",
"status": "all systems up and running",
"startedAt": now.String(),
"releasedOn": now.String(),
}

for key, value := range ps.HealthResponse() {
payload[key] = value
}
b, _ := json.Marshal(payload)
w.Header().Add(webgo.HeaderContentType, webgo.JSONContentType)
_, _ = w.Write(b)
}
}

func startHealthResponder(ctx context.Context, ps *proberesponder.ProbeResponder, fatalErr chan<- error) (*http.Server, error) {
port := uint32(2000)
srv := proberespHTTP.Server(ps, "", uint16(port), proberespHTTP.Handlers{http.MethodGet, "/-/health", func(w http.ResponseWriter, r *http.Request) {}})
srv := proberespHTTP.Server(
ps, "", uint16(port),
proberespHTTP.Handler{
Method: http.MethodGet,
Path: "/-/health",
Handler: healthResponseHandler(ps),
},
)

go func() {
defer logger.Info(ctx, fmt.Sprintf("[http/healthresponder] :%d shutdown complete", port))
logger.Info(ctx, fmt.Sprintf("[http/healthresponder] listening on :%d", port))
fatalErr <- srv.ListenAndServe()
}()

return srv, nil
}

Expand Down
25 changes: 0 additions & 25 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@ package api

import (
"context"
"time"

"github.com/naughtygopher/goapp/internal/usernotes"
"github.com/naughtygopher/goapp/internal/users"
"github.com/naughtygopher/proberesponder"
)

var (
now = time.Now()
)

// Server has all the methods required to run the server
type Server interface {
CreateUser(ctx context.Context, user *users.User) (*users.User, error)
ReadUserByEmail(ctx context.Context, email string) (*users.User, error)
CreateUserNote(ctx context.Context, un *usernotes.Note) (*usernotes.Note, error)
ReadUserNote(ctx context.Context, userID string, noteID string) (*usernotes.Note, error)
ServerHealth() (map[string]any, error)
}

// Subscriber has all the methods required to run the subscriber
Expand All @@ -33,25 +27,6 @@ type API struct {
probestatus *proberesponder.ProbeResponder

Check failure on line 27 in internal/api/api.go

View workflow job for this annotation

GitHub Actions / Static analysis

field `probestatus` is unused (unused)
}

// ServerHealth returns the health of the serever app along with other info like version
func (a *API) ServerHealth() (map[string]any, error) {
payload := map[string]any{
"env": "testing",
"version": "v0.1.0",
"commit": "<git commit hash>",
"status": "all systems up and running",
"startedAt": now.String(),
"releasedOn": now.String(),
}

for key, value := range a.probestatus.HealthResponse() {
payload[key] = value
}

return payload, nil

}

func New(us *users.Users, un *usernotes.UserNotes) *API {
return &API{
users: us,
Expand Down

0 comments on commit 6596c71

Please sign in to comment.