常用软件下载和配置

Download Node.js®

参考文档:Get Node.js v24.15.0 for Linux using fnm with pnpm

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Download and install fnm:
curl -o- https://fnm.vercel.app/install | bash
# Download and install Node.js:
fnm install 24
# Verify the Node.js version:
node -v # Should print "v24.15.0".
# Download and install pnpm:
corepack enable pnpm
# Verify pnpm version:
pnpm -v

Download Golang

安装教程:Download and install

Golang 中 JWT 的签发和校验

JWT 是什么?

JSON Web Token(基于 JSON 的 Web 令牌)

即:JWT 是后端给前端发的一个加密字符串,用来免登录、身份认证、鉴权,代替传统的 Session/Cookie。

golang 中 yaml 配置文件的加载

假如我们有以下配置文件( config.yml ):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
database:
  sqlite:
    path: ./main.db
  mysql:
    host: localhost
    port: 3306
    username: hello
    password: god.pwd
    database: dev
jwt:
  secret: ok-this-is-jst-sec
  expire_time: 24

使用 yaml 库加载

  1. 先使用 os.ReadFile 读取文件内容。
  2. 再使用 yaml.Unmarshal 反序列化文件内容。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"fmt"
	"log"
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	Database DatabaseConfig `yaml:"database"`
	JWT      JWTConfig      `yaml:"jwt"`
}

type DatabaseConfig struct {
	MySQL  MySQLConfig  `yaml:"mysql"`
	Sqlite SqliteConfig `yaml:"sqlite"`
}

type MySQLConfig struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Database string `yaml:"database"`
}

type SqliteConfig struct {
	Path string `yaml:"path"`
}

type JWTConfig struct {
	Secret     string `yaml:"secret"`
	ExpireTime int    `yaml:"expire_time"`
}

func main() {
	cfg := &Config{}
	data, err := os.ReadFile("config.yml")
	if err != nil {
		log.Fatal(err)
	}
	err = yaml.Unmarshal(data, cfg)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(cfg)
	fmt.Println(cfg.Database.MySQL.Username)

}

输出:

Golang 中 .env 文件加载

.env 是一个用于存储环境变量的文件,通常用于存储应用程序的配置信息。

通过使用 .env 文件,我们可以将这些敏感信息从代码中分离出来,从而提高应用程序的安全性和可维护性。