go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/chirp/pkg/controller/status.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package controller 9 10 import ( 11 "net/http" 12 13 "go.charczuk.com/sdk/apputil" 14 "go.charczuk.com/sdk/db" 15 "go.charczuk.com/sdk/web" 16 17 "go.charczuk.com/projects/chirp/pkg/config" 18 "go.charczuk.com/projects/chirp/pkg/model" 19 ) 20 21 type Status struct { 22 apputil.BaseController 23 Model model.Manager 24 Config config.Config 25 } 26 27 func (s Status) Register(app *web.App) { 28 app.Get("/status", s.status) 29 app.Get("/status/postgres", s.statusPostgres) 30 } 31 32 func (s Status) status(r web.Context) web.Result { 33 return &web.JSONResult{ 34 StatusCode: http.StatusOK, 35 Response: map[string]interface{}{ 36 "serviceEnv": s.Config.ServiceEnv, 37 "serviceName": s.Config.ServiceName, 38 "version": s.Config.Version, 39 "gitRef": s.Config.GitRef, 40 }, 41 } 42 } 43 44 func (s Status) statusPostgres(ctx web.Context) web.Result { 45 any, err := s.Model.Invoke(ctx, db.OptLabel("status")).Query(`select 'ok!'`).Any() 46 if err != nil || !any { 47 return &web.JSONResult{ 48 StatusCode: http.StatusInternalServerError, 49 Response: map[string]interface{}{"status": false}, 50 } 51 } 52 dbStats := s.Model.Conn.Stats() 53 return &web.JSONResult{ 54 StatusCode: http.StatusOK, 55 Response: map[string]interface{}{ 56 "status": true, 57 "db.conns_open": dbStats.OpenConnections, 58 "db.conns_idle": dbStats.Idle, 59 "db.max_open_conns": dbStats.MaxOpenConnections, 60 "db.conns_in_use": dbStats.InUse, 61 "db.wait_count": dbStats.WaitCount, 62 "db.wait_duration": dbStats.WaitDuration, 63 "db.max_idle_closed": dbStats.MaxIdleClosed, 64 "db.max_lifetime_closed": dbStats.MaxLifetimeClosed, 65 }, 66 } 67 }