Friday, 14 February 2025

Quick Docker commands

 # list of containers

docker ps

# stop container

docker stop <container id>

# list of images

docker image ls 

# remove images

docker rm <image id>


# add to docker images 

docker load < image_latest.tar.gz

Thursday, 13 February 2025

GO LANG Pointer, derefernece, nil

https://forum.golangbridge.org/t/dereferencing-pointer/21201/4

//Manual derefenrece only need for map

func myProcess(a *map[string]string) { t := (*a)["whatever"] ... }

// GO auto deference if struct is pointer :

Yes, but don’t write it in that complicated way. In Go, we value readability and simplicity. Simply write:

func happyBirthday(p *person) { p.age++ }


--------------------------------------------------------------------------------------------------------

receiver , receiver with pointer


https://go.dev/tour/methods/4


package main


import (

"fmt"

"math"

)


type Vertex struct {

X, Y float64

}


func (v Vertex) Abs() float64 {

return math.Sqrt(v.X*v.X + v.Y*v.Y)

}


func (v *Vertex) Scale(f float64) {

v.X = v.X * f

v.Y = v.Y * f

}


func main() {

v := Vertex{3, 4}

      //result is 5

fmt.Println(v.Abs())

v.Scale(10)

//result is 50

fmt.Println(v.Abs())

}


# reciever with pointer changes the address of original value due to auto dereferncing




-------------------------------







https://medium.com/@jamal.kaksouri/a-comprehensive-guide-to-pointers-in-go-4acc58eb1f4d

var x int = 10
var ptr *int = &x

fmt.Println(x) // output: 10
fmt.Println(ptr) // output: 0xc0000140a8
fmt.Println(*ptr) // output: 10


---------------------


var ptr *int = new(int)

fmt.Println(ptr) // output: 0xc0000160c0
fmt.Println(*ptr) // output: 0

*ptr = 10
fmt.Println(*ptr) // output: 10

ptr = nil


In this example,

we declare a pointer variable ptr

of type *int and use the new function to allocate memory for an integer value.

We then print the memory address stored in ptr,

which is the address of the newly allocated memory block.

We also print the value of *ptr, which is the value stored at the memory address,

which is initially set to 0.

We then assign the value 10 to the memory location pointed to

by ptr using the * operator. Finally, we set the pointer variable to nil,

which frees the memory block allocated by new.


-----------------------------------------------------------------------

error will occur when derefencing nil pointer


The error occurs if you try to dereference (*ptr) a nil pointer:


https://stackoverflow.com/questions/59964619/difference-using-pointer-in-struct-fields
use pointer in struct to allow nil
allow pointer type *string to allow nil value

*namePtr derference the pointet

var namePtr *string // namePtr is nil fmt.Println(*namePtr) // ❌ ERROR: nil pointer dereference

Check nil

package main import "fmt" func main() { var namePtr *string // initialized pointer (nil) if namePtr == nil { fmt.Println("Pointer is nil, cannot dereference") } else { fmt.Println(*namePtr) // This won't execute// dereferencing pointer } }



// Make a pointer
namePtr := new(string)

*namePtr = "Bob" // Assign value to the dereferenced pointer



Using a Pointer for Nested Structs

type User struct { Name string `json:"name"` Email string `json:"email"` Age int `json:"age"` Address *Address `json:"address,omitempty"` // Pointer to Address struct }
  • Address *Address means the Address field is a pointer to an Address struct, not the actual struct itself.
  • The * in the struct definition does not dereference the pointer—it just defines the type as a pointer.

If Address is optional, use a pointer (*Address):

go
type User struct { Name string `json:"name"` Email string `json:"email"` Age int `json:"age"` Address *Address `json:"address,omitempty"` // Omits field if nil }

📌 Response when Address is nil

json
{ "name": "John Doe", "email": "john@example.com", "age": 30 }

Friday, 7 February 2025

Angular deploy a secondary project

 permanet:

In your seondaryproject :

angular.json

"architect": { "build": { "options": { "outputPath": "dist/your-app-name", "baseHref": "/demo/", "deployUrl": "/demo/" }


  • --base-href=/demo/: Specifies the base URL for the application’s routing (i.e., how Angular handles navigation).
  • --deploy-url=/demo/: Specifies the URL path for static assets (JavaScript, CSS, images, fonts, etc.).

Temporary(for ng build only)
ng build --base-href=/demo/ --deploy-url=/demo/


The configure web server to map a dir for this project

Wednesday, 5 February 2025

SQL With Recursive All, Union ALL


https://www.techonthenet.com/mysql/union_all.php

UNION ALL



The MySQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It returns all rows from the query and it does not remove duplicate rows between the various SELECT statements.

Each SELECT statement within the MySQL UNION ALL operator must have the same number of fields in the result sets with similar data types.


 https://stackoverflow.com/questions/18840998/recursive-in-sql

The syntax that you are using looks like Postgres. "Recursion" in SQL is not really recursion, it is iteration. Your statement is:


WITH RECURSIVE t(n) AS (

    SELECT 1

    UNION ALL

    SELECT n+1 FROM t WHERE n < 100

)

SELECT sum(n) FROM t;

The statement for t is evaluated as:


Evaluate the non-self-referring part (select  1).

Then evaluate the self-referring part. (Initially this gives 2.)

Then evaluation the self-referring part again. (3).

And so on while the condition is still valid (n < 100).

When this is done the t subquery is finished, and the final statement can be evaluated.



The RECURSIVE from the query doesn't mean anything: it's just another name like n or t. What makes things recursive is that the CTE named t references itself inside the expression. To produce the result of the expression, the query engine must therefore recursively build the result, where each evaluation triggers the next. It reaches this point: SELECT n+1 FROM t... and has to stop and evaluate t. To do that, it has to call itself again, and so on, until the condition (n < 100) no longer holds. The SELECT 1 provides a starting point, and the WHERE n < 100 makes it so that the query does not recur forever.

Tuesday, 4 February 2025

Certificate Revocation List, 403, self signed CRL

 https://www.techtarget.com/searchsecurity/definition/Certificate-Revocation-List#:~:text=A%20certificate%20revocation%20list%20(CRL)%20is%20a%20list%20of%20digital,actual%20or%20assigned%20expiration%20date.


  • A user (client) submits their digital certificate through the access point.
  • The access point sends the certificate to the authentication server for authentication.
  • The server checks to see if the certificate is expired.
  • If the certificate is valid (i.e., not expired), the server checks the directory containing the details of approved users.
  • If the user is found in the directory, the server next checks the CRL to confirm if the certificate is revoked (identified by the certificate serial number).
  • If the certificate is not revoked, i.e., the serial number is not in the CRL, the user is allowed to access the network.

If CA has CRL, it is contained in cert:


CRL Distribution Point
     Distribution Point Name:
          Full Name:
               URL=xxxxxxxxxxmyca.crl.com/checkme


if this end point is not accessible, 403 will be returned :

https://serverfault.com/questions/450676/how-often-is-crl-refreshed-and-how-to-force-it-to-be

If the CRL cannot be reached, IIS returns a 403.13 by default.


Python, django offer to host crl for ur own self signed ca:
https://django-ca.readthedocs.io/en/latest/crl.html

Tuesday, 28 January 2025

vim global search replace

 https://www.baeldung.com/linux/vim-search-replace

To search and replace all occurrences on every line of the file, we need to slightly modify the previous command:

:%s/article/tutorial/g

The symbol lets us access all the content in the file, and we can replace all occurrences in each line.

The sample.txt file should match this after replacing all occurrences:

MYSQL how to get ERD

 

https://dev.to/alumassy/how-to-access-the-erd-entity-relationship-diagram-of-your-database-schema-in-mysql-workbench-5813


Below are steps you can follow to view the ERD (Entity-Relationship Diagram) of your database schema in MySQL Workbench:

  1. Open MySQL Workbench and open your database connection.
  2. In the top navigation bar, click on “Database” to expand the list of options.
  3. Select “Reverse Engineer” from the database context menu.