0

Now I have the following code extracted from a book.

package main

import (
    "net/http"
)

func main() {
    h := http.FileServer(http.Dir("."))
    http.ListenAndServeTLS(":8001", "rui.crt", "rui.key", h)
}

I expect it can list all the file in the folder of main.go but when I browse to:
https://localhost:8001
I can only see:

This site can’t be reached.  
localhost refused to connect.

I use LiteIDE to build and run the program. After BuildAndRun clicked, the following messages are shown.

F:/Go/bin/go.exe build -i [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
Success: process exited with code 0.
E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server/server.exe  [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
Success: process exited with code 0.

Why and how can I fix it?

Casper
  • 4,435
  • 10
  • 41
  • 72
  • 3
    first, check the error from ListenAndServeTLS, then try using the same IP address in both the server and client. – JimB Jul 19 '16 at 17:20
  • How to check the error from `ListenAndServeTLS`? The messages in the console of LideIDE seems no error. – Casper Jul 19 '16 at 17:24
  • 2
    There's no error, because you're not checking for an error. The output shows the server process exiting, which means it's not serving anything. You check the error from ListenAndServeTLS like any other error, or just print it out regardless with something like `log.Fatal(http.ListenAndServeTLS(...` – JimB Jul 19 '16 at 17:49
  • `2016/07/20 02:29:02 open rui.crt: The system cannot find the file specified.` – Casper Jul 19 '16 at 18:33
  • Use full path to specify the key and crt file. – ain Jul 19 '16 at 19:24

1 Answers1

1

your system cannot find the certificate file.:
this error means you need "rui.crt" file alongside with your main binary file.
if you do not have certificate see: How to create a self-signed certificate with openssl?

then copy "server.pem", "server.key" files to your binary(.exe) file directory

and run this sample code (for test):

package main

import (
    "fmt"
    "log"
    "net/http"
)

type server struct {
}

func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "*Hello World\n*")
}

func main() {
    if err := http.ListenAndServeTLS(":443", "server.pem", "server.key", server{}); err != nil {
        log.Fatal(err)
    }
}

then open web page: https://127.0.0.1/
if firewall poped up say yes,
if you see There is a problem with this website’s security certificate. say continue (advance).

output:

*Hello World
*
Community
  • 1
  • 1