小编B-R*_*Ray的帖子

使用Go Language Simple HTTP Server打印到日志

我正在尝试记录请求者的IP地址,他们正在使用什么方法以及他们请求的文件.但由于某种原因它只在终端上输出而不将其保存到logfile.txt ...

package main

import (
  "fmt"
  "net/http"
  "log"
  "encoding/json"
  "io/ioutil"
)

type Options struct {
  Path string
  Port string
}

func Log(handler http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
    handler.ServeHTTP(w, r)
  })
}

func main() {

  op := &Options{Path: "./", Port: "8001"}

  data, _ := ioutil.ReadFile("./config.json")

  json.Unmarshal(data, op)

  http.Handle("/", http.FileServer(http.Dir(op.Path)))
  err := http.ListenAndServe(":" + op.Port, Log(http.DefaultServeMux))
  if err != nil {
    log.Fatal("ListenAndServe: ", err)
  }
}
Run Code Online (Sandbox Code Playgroud)

go

7
推荐指数
1
解决办法
6261
查看次数

JavaScript 数组中对象的分组依据、不同计数、总和

这可能是补救措施,但我无法弄清楚。我试过使用 d3 并使用 lodash 来获得有效的解决方案,但没有得到任何接近的结果。

我在 JavaScript 中有一个对象数组。如果 [Selected] 值为 true,我想创建一个按 [Version Name] 分组的对象,其中包含不同区域的计数以及每个版本的总数。例如与对象...

[ 
     { Selcted: false, Version Name: "aaa", Zone: "11111", Value: 5 },
     { Selcted: false, Version Name: "aaa", Zone: "11111", Value: 10 },
     { Selcted: true, Version Name: "aaa", Zone: "11111", Value: 15 },
     { Selcted: true, Version Name: "aaa", Zone: "11111", Value: 20 },
     { Selcted: true, Version Name: "aaa", Zone: "22222", Value: 25 },
     { Selcted: true, Version Name: "bbb", Zone: "22222", …
Run Code Online (Sandbox Code Playgroud)

javascript

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

标签 统计

go ×1

javascript ×1