dqybeh2884
dqybeh2884
2018-04-04 09:27

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 douchui3933 3年前

    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}
    
    点赞 评论 复制链接分享

相关推荐