added db health check
This commit is contained in:
parent
9986109947
commit
4fa2917f26
|
@ -48,48 +48,6 @@ export const Home = () => {
|
|||
routeTarget='xss'
|
||||
/>
|
||||
</Grid2>
|
||||
<Grid2 size={3}>
|
||||
<AttackItem
|
||||
attackName='Command Injection'
|
||||
routeTarget='cmd_injection'
|
||||
/>
|
||||
</Grid2>
|
||||
<Grid2 size={3}>
|
||||
<AttackItem
|
||||
attackName='File Inclusion Attacks'
|
||||
routeTarget='cmd_injection'
|
||||
/>
|
||||
</Grid2>
|
||||
<Grid2 size={3}>
|
||||
<AttackItem
|
||||
attackName='CSRF'
|
||||
routeTarget='cmd_injection'
|
||||
/>
|
||||
</Grid2>
|
||||
<Grid2 size={3}>
|
||||
<AttackItem
|
||||
attackName='Directory Traversal'
|
||||
routeTarget='cmd_injection'
|
||||
/>
|
||||
</Grid2>
|
||||
<Grid2 size={3}>
|
||||
<AttackItem
|
||||
attackName='Insecure Desrialization'
|
||||
routeTarget='cmd_injection'
|
||||
/>
|
||||
</Grid2>
|
||||
<Grid2 size={3}>
|
||||
<AttackItem
|
||||
attackName='Session Hijacking'
|
||||
routeTarget='cmd_injection'
|
||||
/>
|
||||
</Grid2>
|
||||
<Grid2 size={3}>
|
||||
<AttackItem
|
||||
attackName='Testing page'
|
||||
routeTarget='testing'
|
||||
/>
|
||||
</Grid2>
|
||||
</Grid2>
|
||||
</Box>
|
||||
);
|
||||
|
|
|
@ -27,7 +27,7 @@ var allowedUsernames map[string]bool
|
|||
func ConnectToDb() (*pgxpool.Pool, error) {
|
||||
// this server is intended to be ran on the same system as the db
|
||||
dbUrl := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", user, password, host, port, dbname)
|
||||
config, err := pgxpool.ParseConfig((dbUrl))
|
||||
config, err := pgxpool.ParseConfig(dbUrl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse data URL: %w", err)
|
||||
}
|
||||
|
@ -37,10 +37,43 @@ func ConnectToDb() (*pgxpool.Pool, error) {
|
|||
return nil, fmt.Errorf("unable to create connection pool: %w", err)
|
||||
}
|
||||
|
||||
log.Println("Connected to DB :)")
|
||||
// validate connection with a simple query
|
||||
conn, err := pool.Acquire(context.Background())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to acquire a connection from the pool: %w", err)
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
// run a test query
|
||||
err = conn.QueryRow(context.Background(), "SELECT 1").Scan(new(int))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to validate database connection: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("Connected to DB at port %d :)", port)
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
// ping the database to check health
|
||||
func DbHealthCheck(w http.ResponseWriter, r *http.Request) {
|
||||
// define the health check query
|
||||
healthCheckSQL := `SELECT 1;`
|
||||
|
||||
// execute the query
|
||||
var result int
|
||||
err := DbPool.QueryRow(context.Background(), healthCheckSQL).Scan(&result)
|
||||
if err != nil {
|
||||
http.Error(w, "Database is unhealthy", http.StatusServiceUnavailable)
|
||||
log.Printf("Database health check failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// send success response
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Database is healthy"))
|
||||
log.Println("Database health check passed")
|
||||
}
|
||||
|
||||
// setup demo db
|
||||
func SetupDemoDb(w http.ResponseWriter, r *http.Request) {
|
||||
// create table and insert demo data
|
||||
|
|
|
@ -17,6 +17,7 @@ func healthCheck(w http.ResponseWriter, r *http.Request) {
|
|||
// setup the http server
|
||||
func ServeApi() {
|
||||
http.HandleFunc("/health", healthCheck)
|
||||
http.HandleFunc("/health-db", db.DbHealthCheck)
|
||||
http.HandleFunc("/setup-demo-db", db.SetupDemoDb)
|
||||
http.HandleFunc("/nuke-db", db.NukeDb)
|
||||
http.HandleFunc("/fetch-all-users", db.FetchAllUsers)
|
||||
|
|
Loading…
Reference in a new issue