小编Bla*_*Cat的帖子

当处理程序在主包外部定义时 chi.URLParam 不起作用

所以我是新手,目前我尝试使用 chi 构建一个小 REST-API(我喜欢它)。昨天我遇到了一个问题,我不太明白。

在我的小测试项目中,我有一个main.go文件,其中包含路由器实例化、添加中间件和启动服务器的主要功能:

func main() {
    router := chi.NewRouter()
    // Middleware
    router.Use(middleware.RequestID)
    router.Use(middleware.RealIP)
    router.Use(middleware.Logger)
    router.Use(middleware.Recoverer)
    // Routes
    router.Post("/login", users.Login)
    router.Post("/register", users.Register)
    router.With(users.LoginRequired).Route("/users", func(r chi.Router) {
        r.Get("/{user_id}", users.GetUser)
    })
    // Start Server
    port := ":8080"
    log.Printf("Server starting at port %v\n", port)
    log.Fatal(http.ListenAndServe(port, router))
}
Run Code Online (Sandbox Code Playgroud)

首先,问题不存在,因为我在main.go文件中定义了所有处理程序函数,并且 GetUser 函数按预期工作并从我的“数据库”(包含 3 个用户的数组)返回一个用户:

func GetUser(w http.ResponseWriter, r *http.Request) {
    uID := chi.URLParam(r, "user_id") // Problem when not in main -> uID = ""
    id, err := strconv.Atoi(uID)
    if err …
Run Code Online (Sandbox Code Playgroud)

go go-chi

13
推荐指数
1
解决办法
4878
查看次数

在 VS Code 和 virtualenv 中使用 Azure Functions 时找不到 Numpy 模块

我是使用 azure 函数的新手,并尝试使用 VS Code 和 Azure Functions 扩展在本地编写一个小示例。

例子:

# First party libraries
import logging

# Third party libraries
import numpy as np
from azure.functions import HttpResponse, HttpRequest


def main(req: HttpRequest) -> HttpResponse:
    seed = req.params.get('seed')

    if not seed:
        try:
            body = req.get_json()
        except ValueError:
            pass
        else:
            seed = body.get('seed')

    if seed:
        np.random.seed(seed=int(seed))
        r_int = np.random.randint(0, 100)
        logging.info(r_int)
        return HttpResponse(
            "Random Number: " f"{str(r_int)}", status_code=200
            )
    else:
        return HttpResponse(
            "Insert seed to generate a number",
            status_code=200
            )
Run Code Online (Sandbox Code Playgroud)

当全局安装 numpy …

python numpy virtualenv azure-functions

5
推荐指数
1
解决办法
2999
查看次数

标签 统计

azure-functions ×1

go ×1

go-chi ×1

numpy ×1

python ×1

virtualenv ×1