diff --git a/code/go-project/base-farmework/internal/pkg/middleware/logging.go b/code/go-project/base-farmework/internal/pkg/middleware/logging.go new file mode 100644 index 00000000..1564ac56 --- /dev/null +++ b/code/go-project/base-farmework/internal/pkg/middleware/logging.go @@ -0,0 +1,11 @@ +package middleware + +import ( + "fmt" + "net/http" +) + +// LoggingMiddleware logs incoming requests +func LoggingMiddleware(w http.ResponseWriter, r *http.Request) { + fmt.Printf("Request received: %s %s\n", r.Method, r.URL.Path) +} diff --git a/code/go-project/base-farmework/internal/pkg/router/router.go b/code/go-project/base-farmework/internal/pkg/router/router.go new file mode 100644 index 00000000..fc2e9ec0 --- /dev/null +++ b/code/go-project/base-farmework/internal/pkg/router/router.go @@ -0,0 +1,98 @@ +package router + +import ( + "net/http" +) + +// HandlerFunc defines the signature of a handler function +type HandlerFunc func(w http.ResponseWriter, r *http.Request) + +// Router struct holds all the routes, middlewares, and groups +type Router struct { + routes map[string]map[string]HandlerFunc + middleware []HandlerFunc + groups map[string]*Group +} + +// NewRouter creates a new Router instance +func NewRouter() *Router { + return &Router{ + routes: make(map[string]map[string]HandlerFunc), + groups: make(map[string]*Group), + } +} + +// Use adds a global middleware to the router +func (r *Router) Use(middleware HandlerFunc) { + r.middleware = append(r.middleware, middleware) +} + +// Handle registers a route with a method and a handler +func (r *Router) Handle(method, path string, handler HandlerFunc) { + if _, exists := r.routes[method]; !exists { + r.routes[method] = make(map[string]HandlerFunc) + } + r.routes[method][path] = handler +} + +// Group creates a new group with a prefix +func (r *Router) Group(prefix string) *Group { + group := &Group{ + router: r, + prefix: prefix, + } + r.groups[prefix] = group + return group +} + +// Group struct represents a group of routes with a common prefix +type Group struct { + router *Router + prefix string + middleware []HandlerFunc +} + +// Use adds a middleware to a group +func (g *Group) Use(middleware HandlerFunc) { + g.middleware = append(g.middleware, middleware) +} + +// Handle registers a route in the group +func (g *Group) Handle(method, path string, handler HandlerFunc) { + fullPath := g.prefix + path + + // Apply group middleware first + for _, m := range g.middleware { + g.router.Use(m) + } + + // Register the route + g.router.Handle(method, fullPath, handler) +} + +// Get registers a GET route in the group +func (g *Group) Get(path string, handler HandlerFunc) { + g.Handle("GET", path, handler) +} + +// Post registers a POST route in the group +func (g *Group) Post(path string, handler HandlerFunc) { + g.Handle("POST", path, handler) +} + +// ServeHTTP is the main entry point for handling HTTP requests +func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { + // Apply global middlewares + for _, m := range r.middleware { + m(w, req) + } + + // Route matching + if handlers, ok := r.routes[req.Method]; ok { + if handler, ok := handlers[req.URL.Path]; ok { + handler(w, req) + return + } + } + http.NotFound(w, req) +} diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815154226238-359528291.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815154226238-359528291.png new file mode 100644 index 00000000..4c0b5d9a Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815154226238-359528291.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815154356500-781734063.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815154356500-781734063.png new file mode 100644 index 00000000..ddaa2cff Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815154356500-781734063.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160406164-241997583.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160406164-241997583.png new file mode 100644 index 00000000..53a8408e Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160406164-241997583.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160700818-1341311032.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160700818-1341311032.png new file mode 100644 index 00000000..24cd097b Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160700818-1341311032.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160850386-1312464558.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160850386-1312464558.png new file mode 100644 index 00000000..4d13a352 Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815160850386-1312464558.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815161016883-1770065586.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815161016883-1770065586.png new file mode 100644 index 00000000..34197c2e Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815161016883-1770065586.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815161656144-1744125380.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815161656144-1744125380.png new file mode 100644 index 00000000..dcaf4a1f Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815161656144-1744125380.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815162127646-1370878516.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815162127646-1370878516.png new file mode 100644 index 00000000..f477715f Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815162127646-1370878516.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815163105131-1420632637.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815163105131-1420632637.png new file mode 100644 index 00000000..81f41f4a Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20230815163105131-1420632637.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410153748815-1410794935.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410153748815-1410794935.png new file mode 100644 index 00000000..436b65a2 Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410153748815-1410794935.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410163414144-427544868.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410163414144-427544868.png new file mode 100644 index 00000000..95d4d848 Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410163414144-427544868.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410163841369-1133262585.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410163841369-1133262585.png new file mode 100644 index 00000000..94b2158b Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410163841369-1133262585.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410164106670-257379519.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410164106670-257379519.png new file mode 100644 index 00000000..db132e7c Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410164106670-257379519.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410164727553-1497806551.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410164727553-1497806551.png new file mode 100644 index 00000000..c5a75fb8 Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20240410164727553-1497806551.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20241018114323936-1844242583.png b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20241018114323936-1844242583.png new file mode 100644 index 00000000..f9e736da Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/81544-20241018114323936-1844242583.png differ diff --git a/notes/前端开发/nvm/assets/windows下nvm安装和使用/copycode.gif b/notes/前端开发/nvm/assets/windows下nvm安装和使用/copycode.gif new file mode 100644 index 00000000..dc146865 Binary files /dev/null and b/notes/前端开发/nvm/assets/windows下nvm安装和使用/copycode.gif differ diff --git a/notes/前端开发/nvm/windows下nvm安装和使用.md b/notes/前端开发/nvm/windows下nvm安装和使用.md new file mode 100644 index 00000000..bdf226a2 --- /dev/null +++ b/notes/前端开发/nvm/windows下nvm安装和使用.md @@ -0,0 +1,143 @@ +# **一、独立下载安装nodejs(若想切换使用多版本nodejs,请直接转至第二节)** + +1、访问https://nodejs.org/en/地址,切换到download目录下的Prebuilt Installer的tab页,选择相应版本进行下载: + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410163414144-427544868.png) + +或者直接去列表里查找想要下载的版本:https://nodejs.org/dist/ + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20241018114323936-1844242583.png) + + + +2、安装完成后,添加环境变量 + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410163841369-1133262585.png) + + Pah中新建nodejs安装路径,如:D:\Program Files\node\nodejs + + 3、使用cmd命令,查看node版本 + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410164106670-257379519.png) + + 4、查看npm默认存放的位置 + +``` +npm get prefix +npm get cache +``` + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410164727553-1497806551.png) + + 如果需要修改指定路径可以进行修改,若进行修改需要相应添加系统变量: + +``` +npm config set prefix "D:\..." +npm config set cache "D:\..." +``` + + + +# **二、安装nvm,并利用nvm安装管理多个nodejs版本(无需独立下载安装nodejs,若已安装需要先卸载)** + +1、安装nvm + +nvm是一个简单的bash脚本,它是用来管理系统中多个已存的Node.js版本 + +注:如果在安装nvm前已经安装了node,建议先将原来的node进行卸载或删除,可通过where node查看node安装路径: + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815163105131-1420632637.png) + +(1)下载并安装nvm,下载地址:https://github.com/coreybutler/nvm-windows/releases,下载.zip后缀的这个文件,下载后解压安装即可 + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815154226238-359528291.png) + +(2)配置镜像源,从安装目录中找到settings.txt文件将一下配置复制进去 + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815154356500-781734063.png) + +``` +node_mirror: http://npmmirror.com/mirrors/node/ +npm_mirror: http://registry.npmmirror.com/mirrors/npm/ +``` + +否则执行nvm list available会报错: + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20240410153748815-1410794935.png) + + + +(3)查看安装的版本号,检查安装是否成功:nvm + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815160406164-241997583.png) + + (4)安装完后,通过环境变量可以查看到相关路径设置: + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815160700818-1341311032.png) + + + +2、使用nvm安装多版本nodejs + +(1)查看nodejs所有版本 + +``` +nvm list available +``` + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815160850386-1312464558.png) + +(2)安装指定版本 + +``` +nvm install 18.16.0 +``` + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815161016883-1770065586.png) + +(3)使用指定版本或切换到指定版本 + +``` +nvm ues 18.16.0 +``` + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815161656144-1744125380.png) + +(4)查看当前版本信息 + +``` +node -v (查看当前版本) +nvm list (查看已安装的所有nodejs版本) +``` + +![img](./assets/windows%E4%B8%8Bnvm%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/81544-20230815162127646-1370878516.png) + +(5)卸载指定版本 + +``` +nvm uninstall 18.16.0 +``` + +(6)其他nvm常用命令 + +``` +nvm off // 禁用node.js版本管理(不卸载任何东西) +nvm on // 启用node.js版本管理 +nvm install // 安装node.js的命名 version是版本号 例如:nvm install 8.12.0 +nvm uninstall // 卸载node.js是的命令,卸载指定版本的nodejs,当安装失败时卸载使用 +nvm ls // 显示所有安装的node.js版本 +nvm list available // 显示可以安装的所有node.js的版本 +nvm use // 切换到使用指定的nodejs版本 +nvm v // 显示nvm版本 +nvm install stable // 安装最新稳定版 +``` + + + +3、遇到问题 + +在新机上安装nvm以及nodejs,安装完后 nvm ues 18.16.0 设置当前版本没有反应, + +通过查看在安装nvm的时候,设置的nodejs路径下(settings.txt文件里的path值)没有相应文件夹,也就是系统变量下NVM_SYMLINK的路径文件夹不存在。 + +解决方案:下载任意版本nodejs安装到系统变量NVM_SYMLINK的路径文件夹下,然后再卸载掉,通过 nvm ues 18.16.0再切换到指定版本,可解决。 \ No newline at end of file