go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/kana-server/pkg/controller/index.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  	"path/filepath"
    13  
    14  	"go.charczuk.com/sdk/apputil"
    15  	"go.charczuk.com/sdk/db"
    16  	"go.charczuk.com/sdk/web"
    17  
    18  	"go.charczuk.com/projects/kana-server/pkg/config"
    19  	"go.charczuk.com/projects/kana-server/pkg/model"
    20  	"go.charczuk.com/projects/kana-server/pkg/static"
    21  )
    22  
    23  // Index is the root controller.
    24  type Index struct {
    25  	apputil.BaseController
    26  
    27  	Model  model.Manager
    28  	Config config.Config
    29  }
    30  
    31  // Register regisers the controller.
    32  func (i Index) Register(app *web.App) {
    33  	app.Views.AddFS(web.ViewFS{FS: static.Views, Patterns: []string{"_views/*.html"}})
    34  	sfs := &web.StaticFileServer{
    35  		SearchPaths: []http.FileSystem{
    36  			http.FS(static.Static),
    37  		},
    38  		RewriteRules: []web.RewriteRule{
    39  			web.RewriteRuleFunc(func(path string) (string, bool) {
    40  				return filepath.Join("_static", path), true
    41  			}),
    42  		},
    43  	}
    44  	app.HandleAction(http.MethodGet, "/static/*filepath", sfs.Action)
    45  
    46  	app.Get("/", web.SessionAware(i.index))
    47  	app.Get("/status", i.status)
    48  	app.Get("/status/postgres", i.statusPostgres)
    49  }
    50  
    51  func (i Index) index(ctx web.Context) web.Result {
    52  	if ctx.Session() != nil {
    53  		return web.Redirect("/home")
    54  	}
    55  	return ctx.Views().ViewStatus(http.StatusOK, "index", nil)
    56  }
    57  
    58  func (i Index) status(r web.Context) web.Result {
    59  	return &web.JSONResult{
    60  		StatusCode: http.StatusOK,
    61  		Response: map[string]interface{}{
    62  			"serviceEnv":  i.Config.ServiceEnv,
    63  			"serviceName": i.Config.ServiceName,
    64  			"version":     i.Config.Version,
    65  			"gitRef":      i.Config.GitRef,
    66  		},
    67  	}
    68  }
    69  
    70  func (i Index) statusPostgres(ctx web.Context) web.Result {
    71  	any, err := i.Model.Invoke(ctx, db.OptLabel("status")).Query(`select 'ok!'`).Any()
    72  	if err != nil || !any {
    73  		return &web.JSONResult{
    74  			StatusCode: http.StatusInternalServerError,
    75  			Response:   map[string]interface{}{"status": false},
    76  		}
    77  	}
    78  	dbStats := i.Model.Conn.Stats()
    79  	return &web.JSONResult{
    80  		StatusCode: http.StatusOK,
    81  		Response: map[string]interface{}{
    82  			"status":                 true,
    83  			"db.conns_open":          dbStats.OpenConnections,
    84  			"db.conns_idle":          dbStats.Idle,
    85  			"db.max_open_conns":      dbStats.MaxOpenConnections,
    86  			"db.conns_in_use":        dbStats.InUse,
    87  			"db.wait_count":          dbStats.WaitCount,
    88  			"db.wait_duration":       dbStats.WaitDuration,
    89  			"db.max_idle_closed":     dbStats.MaxIdleClosed,
    90  			"db.max_lifetime_closed": dbStats.MaxLifetimeClosed,
    91  		},
    92  	}
    93  }