Tidbits, tips, things I read recently etc.
2025/01/07 - Find a process attached to a port
More often than not, I come across = EADDRINUSE: address already in use = while starting a local API. I used to google it and now use LLMs to find a quick solution. This time it felt weird. I felt like I should know this by heart, and here it goes.
Find the process associated with the port:
lsof -i :<port_number>
Stop the process:
kill <PID> # or kill -9 <PID> for force quit
2024/12/06 - Debugging Hugo pages
One of the easiest ways to debug metadata in Hugo is by simply printing all of them on the page.
<textarea>{{ . | jsonify }}</textarea>
To view the raw content of a page:
<textarea>{{ .Content | safeHTML }}</textarea>
I prefer <textarea>
because it provides an easy way to view and
expand the output using the browser's resize handle.
2024/12/03 - Trace in Haskell
trace
is a debugging function in Haskell, similar to a log
or
print
statement in other languages. It allows you to log messages
while evaluating expressions.
Here’s an example:
import System.IO
import Debug.Trace (trace)
main :: IO ()
main = do
let x = 1 + 2
let y = x + 3
trace ("trace x == " ++ show x) $
trace ("trace y == " ++ show y) $ do
let z = x + y
putStrLn ("z == " ++ show z)
putStrLn "Done!"
We can also use trace
when defining variables. Whenever the
variable's value is evaluated, trace
logs the message. For example:
ghci> import Debug.Trace (trace)
ghci> let nu = trace ("OG value of nu == " ++ show 7) 7
ghci> nu
OG value of nu == 7
7
ghci> x = nu + 7
ghci> x
OG value of nu == 7
14
ghci> y = x + 7
ghci> y
OG value of nu == 7
21
2024/12/01 - MongoDB Database Creation
Apparently, you can't create a MongoDB database without creating a collection.
Ideally, the following command should create a database:
database.command(
'createUser',
database_user,
pwd=database_password,
roles=roles
)
However, it doesn't. A MongoDB database is created only when data is
inserted. To work around this, we insert an empty document into the
random
collection:
database["random"].insert_one({})