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}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器