dqybeh2884 2018-04-04 09:27
浏览 390
已采纳

http:紧急服务127.0.0.1:48286:EOF [关闭]

Here is my code (it's a simple web server in golang) :

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)
    }
}

and after I made a local tunnel:

lt --port 8000

that give me something like:

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

But when I try to open the URL with my browser I have this error:

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

Maybe because it's using https but I don't have any clue of what is causing this bug.

  • 写回答

1条回答 默认 最新

  • douchui3933 2018-04-04 11:18
    关注

    As mentioned in the comments by Volker you are currently panic'ing when you don't receive json according to your spec - it's safe to assume this should not be the case, so do some proper error handling.


    Nonetheless you're code is currently working, assuming you have something similar to this struct:

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

    You can post content matching this struct:

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

    This will return:

    {"status":1}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能