2021.12.12

I don’t usually open files from Documents folder in Emacs. But I was trying to open an Arduino files, which by default is created in Documents folder. And I found out I couldn’t dired the Documents dir (Not sure if I was able to open the file). The error was:

Listing directory failed but ‘access-file’ worked

Solution

It’s caused by Mac OS Privacy settings.

  1. Settings -> General Settings -> Security & Privacy -> Privacy -> Full Disk Access
  2. Check Emacs (Require restart)
  3. Click +, add /usr/bin/ruby (I added this after restarting Emacs, did not require other restart. This was the one that let me do the dired)
2021.11.25

It is so hard to find a good reference for Firebase. In particular I wasn’t really able to find a good example on how to authenticate a REST API served by Firebase Google Cloud Function. AFAIK Python Admin SDK cannot call google cloud function. Actually, I’m not too sure if this is actually secure. :confused:

There is one guide on authenticating for REST API of Realtime Database, but it didn’t work for the cloud function.

On Firebase Cloud Function

It can validate the ID Token, as well as get the data from Realtime Database as admin.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const app = express();

let db = admin.database();

app.get('/', (req, res) => {
  const authorization = req.get('Authorization');
  if (!authorization) {
    res.send({"status":"error","error":"Not authorized"});
    return;
  }
  const idToken = req.get('Authorization').slice(6,);
  admin.auth()
    .verifyIdToken(idToken)
    .then((decodedToken) => {
      const uid = decodedToken.uid;
      return db.ref('users').once("value");
    })
    .then((value) => {
      res.send(value.val());
    })
});
exports.test = functions.https.onRequest(app);

On python admin SDK

# Both Firebase and Emulator needs a credential file (Service Account Key from Cloud Console)
cred = credentials.Certificate('credentials.json')
default_app = initialize_app(cred, {
  "projectId": "myproject"
})
custom_token = auth.create_custom_token("myuid")
api_key = "actualKeyForFirebaseOrRandomStringForEmulator"

# For Firebase
response = requests.post("https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key="+api_key, json={"token":custom_token,"returnSecureToken":True})

# For Emulator
response = requests.post("http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key="+api_key, json={"token":custom_token,"returnSecureToken":True})

id_token = response.json()["idToken"]
url = "https://us-central1-myprojectid.cloudfunctions.net/myfunctionname"
url = "http://0:0:0:5001/myprojectid/us-central1/myfunctionname"
requests.get(url, headers={"Authorization":"Token "+id_token})
2021.10.13

The setup was not really clear (I think). References:

  1. M-x package-list-package and install forge
  2. Go to Gitlab and create a personal token with all access, Copy the token key
  3. Open ~/.authinfo
     machine gitlab.com/api/v4 login <your_gitlab_username>^forge password <your_gitlab_access_token>
    
  4. M-x epa-encrypt-file, select OK
  5. Setup a new password
  6. Add in .emacs
     ; Auto run forge
     (with-eval-after-load 'magit
       (require 'forge))
     ; For forge authentication
     (setq auth-sources '("~/.authinfo.gpg"))
    
  7. Do C-x e at the end of each lines above
  8. Open the magit, then type f a
  9. Add the repo and it will ask if you want to download stuff.
2021.10.03

What is Docker

Docker is basically a virtual machine. The special thing is that the minimal image that it has is based on a Linux distro called Alpine, and the image size is really small, just about 5MB! Of course there are images based on Debian and Ubuntu as well, with bunch of different versions.

It’s useful that when you try to test a software, you don’t need to install or create the environment for the software in your actual PC, since you’ll usually you’ll end up with dependencies problem and it takes time to solve, but instead you can just launch this Docker container and interact with the software from your PC using its exposed ports.

First, install Docker for Mac here. It will install a GUI and an icon in the menu bar. You don’t need to sign in to use Docker.

The Basic

Image

Is basically an image. A set of installed OS and softwares that you can either create one or download from Docker Hub (Like git, you can do docker pull). List your images by

docker images 

Run

In order to use the image, you need to run it. For example, let’s run alpine.

docker run alpine

It will actually do nothing since there’s no process set in the image to keep the system running.

Container

When we run an image, it will create a container, an instance of the image. It will be automatically assigned a unique name. The following will list all of the containers that we have, including the exited ones.

docker container ls -a

We can make the container not exit. If we do:

docker run -it alpine

It will create a new container and we directly entered the container’s command line! If we press ctrl-d it will exit. We can re-run the container by referencing the name on the last column of docker container ls -a. -i is used so that we can enter the command line again.

docker container start -i <container_name>

However, the -it is linked to the container. So even if we restarted the container from the first run, it will always exit. (I’m still not sure with this concept)

If we keep on running the image, we will end up with a bunch of containers.

docker rm <container_name>
docker rm $(docker ps --filter status=exited -q) # Remove all stopped containers

Building Your Image Own Image

If we create a Dockerfile we can build our own image. We can base it off other image and install extra stuff in there. Then, there’s also compose where we can launch multiple containers at once.

For example, create a new folder, and inside that folder, create a new file named Dockerfile with the content

FROM alpine

If we then run

docker build . -t test

It will create a new image named test based off the alpine image. If we do docker images you can see it’s listed there. We can then run it by

docker run -it test

Yay! I guess that’s my first docker image =p My next goal is to run php laravel on docker. There are images that provides that, but they seem to be so big (500MB+) I just want to see if it is possible to make a smaller one. But maybe it’s not a good idea, not something that I want to spend my time on… there are alpine based ones. I think I can try the bitnami one or try the official Laravel Sail.

2021.09.18
  • Using __slots__ = () inside a class will prevent its attributes to be changed. [ref]
  • Only for Python3, 3 different types of raising Exception:
    1. Normal Exception (Not expected to have another exception while handling an exception)
        try: raise Exception("Error")
        except Exception as e: raise Exception("Error while handling Exception")
      
    2. Cause of Exception (The second exception is expected to happen while handling an exception)
        try: raise Exception("Error")
        except Exception as e: raise Exception("Different Exception") from e
      
    3. Only show last Exception (Ignoring the first exception)
        try: raise Exception("Error")
        except Exception as e: raise Exception("Different error") from None