Create an HTTP server
The Go language creates a simple HTTP server program, much like a Java Web servlet. For example, let’s create a program that simply prints “Hello World.” The code is as follows:
package main
import (
"fmt"
"net/http"
)
func helloworld(w http.ResponseWriter,r *http.Request) {
fmt.Fprintf(w,"Hello World")}func main(a) {
server :=&http.Server{
Addr: "127.0.0.1:80",
}
http.HandleFunc("/",helloworld)
server.ListenAndServe()
}
Copy the code
After running, enter 127.0.0.1 in the web page input box of the browser and a simple web page with Hello World will appear.
Here, we can see that if you want to create a server using the Go language, you need to understand the internal logic of the NET/HTTP package. Looking only at the code in the main() function, we see that it calls the HandleFunc() function and ListenAndServe() function.
ListenAndServe()
In the Go language, ListenAndServe() takes two arguments, the first being the port number to listen on and the second being the event handler. The handler interface is defined as follows:
type Handler interface{
ServerHTTP(ResponseWriter,*Request)
}
Copy the code
Once you implement this interface, you can implement your own handler handler. Its specific implementation method is as follows:
type HandlerFunc func(ResponseWriter,*Request)
func (f HandlerFunc)ServerHTTP(w ResponseWriter,r *Request){
f(w,r)
}
Copy the code
The Handler handler is an Interceptor similar to the Interceptor in the Java SpringMVC framework. It happens before the http.handlerfunc () function processes the logic.
Example use of the ServeHTTP() method
If we need to implement HTTP request, only with the specified parameter refer, the request can be successfully called, otherwise return 403 status. The following is an example:
Define the Refer structure
type Refer struct {
handler http.Handler
refer string
}
Copy the code
As you can see here, the author defines two objects, handler and a custom refer.
Implement the ServeHTTP() method
The complete code is as follows:
package main
import (
"fmt"
"net/http"
)
type Refer struct {
handler http.Handler
refer string
}
func (this *Refer) ServeHTTP(w http.ResponseWriter,r *http.Request) {
if r.Referer()==this.refer{
this.handler.ServeHTTP(w,r)
}else{
w.WriteHeader(403)}}func myHandler(w http.ResponseWriter,r *http.Request) {
w.Write([]byte("It's handler"))}func helloworld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")}func main(a) {
referer :=&Refer{
handler: http.HandlerFunc(myHandler),
refer: "www.liyuanjing.com",
}
http.HandleFunc("/hello", helloworld)
http.ListenAndServe(": 80",referer)
}
Copy the code
At this point, if you type 127.0.0.1 directly, the page will display a denial of access.
Example Create an HTTPS server
In the Go language, the NET/HTTP package provides methods to create HTTPS services, which are defined as follows:
func (srv *Server)ListenAndServeTLS(certFile,keyFile string) error
Copy the code
From the above method, we can see that the creation of HTTPS service requires only two parameters. The first parameter is the path of the certificate file, and the second parameter is the path of the private key file.
We need to create these two files before we can create the HTTPS service. However, the certificates that generally circulate over the Internet are paid, and some are free, but troublesome. So, here the blogger only do an example, also use.
Create a certificate and private key
First, you need to make sure your computer has downloaded and configured OpenSSL properly. If the configuration file has been downloaded, run the following command to create the certificate and private key:
openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 365 -out server.crt
Copy the code
After the command is executed, the following information is displayed. Here, the reader is free to type.
After these commands are executed, two files server. CRT and server.key are automatically generated in the directory. Now you can use these two files to create your own HTTPS server.
Example Create an HTTPS server
Without further ado, here is the code directly, as follows:
package main
import (
"net/http"
)
func handle(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World HTTPS"))}func main(a) {
server := &http.Server{Addr: ": 80", Handler: http.HandlerFunc(handle)}
server.ListenAndServeTLS("server.crt"."server.key")}Copy the code
When run, the page will not print Hello World HTTPS, because the HTTPS certificate created in this way is not recognized by the Internet, but that’s how it was created.