http:恐慌服务127.0.0.1:48286:EOF

时间:2018-04-04 09:27:31

标签: go

这是我的代码(它是golang中的简单Web服务器):

package main

import (
    "fmt"
    "net/http"
    "log"
    "io"
    "github.com/gorilla/mux"
    "encoding/json"
    "strconv"

    "io/ioutil"
    "os"
)

var (
    Trace   *log.Logger
    Info    *log.Logger
    Warning *log.Logger
    Error   *log.Logger
    Response string
)

func Init(
    traceHandle io.Writer,
    infoHandle io.Writer,
    warningHandle io.Writer,
    errorHandle io.Writer) {

    Trace = log.New(traceHandle,
        "TRACE: ",
        log.Ldate|log.Ltime|log.Lshortfile)

    Info = log.New(infoHandle,
        "INFO: ",
        log.Ldate|log.Ltime|log.Lshortfile)

    Warning = log.New(warningHandle,
        "WARNING: ",
        log.Ldate|log.Ltime|log.Lshortfile)

    Error = log.New(errorHandle,
        "ERROR: ",
        log.Ldate|log.Ltime|log.Lshortfile)
}

type check_geo_send struct {
    Status int `json:"status"`
}

func checkGeo(w http.ResponseWriter, r *http.Request) {
    var ipdata ip
    decoder := json.NewDecoder(r.Body)

    err := decoder.Decode(&ipdata)
    fmt.Println(ipdata.Ip)
    checkErr(err)
    var data_send check_geo_send
    data_send.Status = 1
    data_json, _ := json.Marshal(data_send)
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Content-Type", "application/json")
    w.Header().Set("Content-Length", strconv.Itoa(len(data_json))) //len(dec)
    w.Write(data_json)
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

func main() {
    Init(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)
    r := mux.NewRouter()
    Info.Println("1")
    r.HandleFunc("/", checkGeo)
    http.Handle("/", r)
    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        fmt.Println(err)
    }
}

在我做了一个本地隧道之后:

lt --port 8000

给我一​​些类似的东西:

https://proud-chipmunk-52.localtunnel.me

但是当我尝试使用浏览器打开URL时出现此错误:

INFO: 2018/04/04 11:19:03 Orchestra.go:85: 1
2018/04/04 11:19:08 http: panic serving 127.0.0.1:48286: EOF
goroutine 22 [running]:
net/http.(*conn).serve.func1(0xc420128280)
    /usr/lib/go-1.10/src/net/http/server.go:1726 +0xd0
panic(0x6884a0, 0xc420010070)
    /usr/lib/go-1.10/src/runtime/panic.go:505 +0x229
main.checkErr(0x719940, 0xc420010070)
    /home/bussiere/Workspace/Alexa/Orchestra.go:79 +0x4a
main.checkGeo(0x71c7a0, 0xc420188000, 0xc42013c600)
    /home/bussiere/Workspace/Alexa/Orchestra.go:65 +0x175
net/http.HandlerFunc.ServeHTTP(0x6f8458, 0x71c7a0, 0xc420188000, 0xc42013c600)
    /usr/lib/go-1.10/src/net/http/server.go:1947 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc42010e000, 0x71c7a0, 0xc420188000, 0xc42013c600)
    /home/bussiere/go/packages/src/github.com/gorilla/mux/mux.go:159 +0xed
net/http.(*ServeMux).ServeHTTP(0x86d720, 0x71c7a0, 0xc420188000, 0xc42013c400)
    /usr/lib/go-1.10/src/net/http/server.go:2337 +0x130
net/http.serverHandler.ServeHTTP(0xc420082ea0, 0x71c7a0, 0xc420188000, 0xc42013c400)
    /usr/lib/go-1.10/src/net/http/server.go:2694 +0xbc
net/http.(*conn).serve(0xc420128280, 0x71ca20, 0xc42012e540)
    /usr/lib/go-1.10/src/net/http/server.go:1830 +0x651
created by net/http.(*Server).Serve
    /usr/lib/go-1.10/src/net/http/server.go:2795 +0x27b

也许是因为它使用的是https,但我不知道是什么导致了这个错误。

1 个答案:

答案 0 :(得分:2)

正如Volker的评论中所提到的那样,当你根据自己的规范收到json时,你现在很恐慌 - 可以安全地认为这不应该是这种情况,所以你也应该这样做正确的错误处理。

尽管如此,假设您有类似于此结构的内容,您的代码目前正在运行:

type ip struct {
    Ip string `json:"ip"`
}

您可以发布与此结构匹配的内容:

curl -d '{"ip":"128.0.0.1"}' -H "Content-Type: application/json" -X POST http://localhost:8000/

这将返回:

{"status":1}