In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use the GE language to write a Http Server related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you will gain something after reading this article on how to write a Http Server in GE language. Let's take a look.
Http Server code
Go.mod:
Module goStudy1go 1.17
Main.go:
Package mainimport ("fmt", "os", "strconv" / / "github.com/thinkeridea/go-extend/exnet", "io", "log", "net/http", "strings") / * write a HTTP server with four functions: 1, receive client request and write header in request to response header 2 Read the VERSION configuration in the environment variables of the current system and write to response header 3 the server-side record access log including the client-side IP,HTTP return code, output to the standard output 4 of the server side, when accessing localhost/healthz Should return 200 http.HandleFunc / Main method entry func main () {println ("normal environment") / / function 1 http.HandleFunc ("/ requestAndResponse", requestAndResponse) / / function 2 http.HandleFunc ("/ getVersion", getVersion) / / function 3 http.HandleFunc ("/ ipAndStatus") IpAndStatus) / / register interface handle / / function 4 http.HandleFunc ("/ healthz", healthz) / / register interface handle err: = http.ListenAndServe (": 81", nil) / / listen for empty handle Port 80 is occupied, use port 81 if nil! = err {log.Fatal (err) / / display error log}} / / function 1 Receive request and response func requestAndResponse (response http.ResponseWriter Request * http.Request) {println ("call requestAndResponse API") headers: = request.Header / / header is data of type Map println ("incoming hander:") for header: = range headers {/ / value is [] string / / println ("key of header:" + header) values: = headers [header] for index _: = range values {values [index] = strings.TrimSpace (values) / / println ("index=" + strconv.Itoa (index)) / / println ("value of header:" + values [index])} / / valueString: = strings.Join (values) ") / / println (" value of header: "+ valueString) println (header +" = "+ strings.Join (values,")) / / print the header response.Header () of request. Set (header, strings.Join (values, ") ")) / / traverses Header / / println ()} fmt.Fprintln written to response (response," Header all data: ", headers) io.WriteString (response," succeed ")} / / function 2 Get the versionfunc getVersion (response http.ResponseWriter) of the environment variable Request * http.Request) {println ("call getVersion API") envStr: = os.Getenv ("VERSION") / / envStr: = os.Getenv ("HADOOP_HOME") / / println ("system environment variable:" + envStr) / / you can see C:\ soft\ hadoop-3.3.1 Win10 needs to restart the computer to take effect response.Header () .Set ("VERSION"). EnvStr) io.WriteString (response, "succeed")} / / function 3 The output IP and the return code func ipAndStatus (response http.ResponseWriter, request * http.Request) {println ("call ipAndStatus API") form: = request.RemoteAddr println ("Client- > ip:port=" + form) / / the virtual machine is in bridging mode. All returned by postman is 127.0.0.1 Open the website 192.168.1.139:81/ipAndStatus with your mobile phone and you can see the new IP ipStr: = strings.Split (form, ":") println ("Client- > ip=" + ipStr [0]) / / print ip / / get the http response code / / response.WriteHeader (301) / / manually set the response code Default 200 / / response.WriteHeader (http.StatusOK) / / since this is called by default, the ∴ return code is this 200 [server.go active code] println ("Client- > response code=" + strconv.Itoa (http.StatusOK)) / / println ("response code- >:" + code) io.WriteString (response, "succeed")} / / function 4 Connectivity test API func healthz (response http.ResponseWriter, request * http.Request) {println ("call healthz API") response.WriteHeader (200) / / set the error code 200 / / response.WriteHeader (http.StatusOK) / / this method is called by default. The default is 200 [server.go source code] io.WriteString (response, "succeed")}
Because port 80 is occupied, port 81 is used.
Debug
Because the Linux virtual machine does not install the go environment, only windows has the go environment. After developing with goland, debug with postman.
Function 1
Additional k2=v1 is configured in the request of POST. After Send, you can see:
Manually added request headers and other default request headers appear in Response. [the original response has only 3 pairs of kv results, which has been traversed and added successfully]. Indicates that the write was successful.
Function 2
Because Windows needs to restart to refresh environment variables, so:
/ / envStr: = os.Getenv ("VERSION") envStr: = os.Getenv ("HADOOP_HOME")
When testing, the existing environment variables are read here, and the principle is consistent.
Website: http://127.0.0.1:81/getVersion
Indicates that Go can read the value of the environment variable and write to the headers of response.
Function 3
Website: http://127.0.0.1:81/ipAndStatus
Request the website with postman and mobile phone respectively (the mobile request needs to be on the same route as PC, and change the website IP to PC's IP to access it), which is shown in goland:
Environment calls getVersion API normally calls ipAndStatus API Client- > ip:port=127.0.0.1:59595Client- > ip=127.0.0.1Client- > response code=200 calls ipAndStatus Interface Client- > ip:port=192.168.1.138:37548Client- > ip=192.168.1.138Client- > response code=200
Obviously read the IP of client. Because it is written in server.go:
/ / WriteHeader sends an HTTP response header with the provided / / status code. / If WriteHeader is not called explicitly, the first call to Write / / will trigger an implicit WriteHeader (http.StatusOK) / / Thus explicit calls to WriteHeader are mainly used to / / send error codes. / The provided code must be a valid HTTP 1xx-5xx status code. / / Only one header may be written. Go does not currently / / support sending user-defined 1xx informational headers, / / with the exception of 100-continue response header that the / / Server sends automatically when the Request.Body is read. WriteHeader (statusCode int)
The default response header is to take the return value of 200, if not set, it will be returned according to the default value of 200, so the response code here is 200.
Since the request body referenced by the response body does not contain a return code, if the return code [request.Response.StatusCode] is taken directly from the response body's request, a memory error and a null pointer panic will be reported.
Function 4
Web site: http://127.0.0.1:81/healthz
Using postman to call the interface, you can see:
The default return code for the response header of the response body is 200. And return 3 values.
As you can see, Go is very concise compared to Java.
This is the end of the article on "how to write a Http Server in GE language". Thank you for reading! I believe you all have a certain understanding of "how to write a Http Server in GE language". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.