golang builds a static web server implementation method

  • 2020-06-15 09:16:05
  • OfStack

I'm hu Han 3 back again. It has been a long time since I published this article. In order to maintain the active degree of the platform, I will share the next knowledge I just learned today. I will use golang to build a static web server, which is available for pro test and attached with code!

Any programmer who has used golang should know that we do not need server support such as iis,apache,nginx,kangle for golang development.

Why is that?

The reason is that the client and server implementations of HTTP are already provided in the net/http package of golang.

According to online comments, golang is not suitable for web development. Compared with php, java,.net, nodejs and other back-end languages, using golang to develop web is indeed a big project.

Last night, I happened to see an article about using golang to build web server. My heart was itching, so I did a lot of work to practice my hands.

I am a novice on the road, copy the content of the article, always bump, every run is to find the path. The code looks like this:


func main() {
 http.Handle("/css/", http.FileServer(http.Dir("template")))
 http.Handle("/js/", http.FileServer(http.Dir("template")))

 http.ListenAndServe(":8080", nil)
}

Directory structure:


src
|--main
| |-main.go
|--template
| |-css
|  |--admin.css
| |-js
|  |--admin.js
| |-html
|  |--404.html

The result is: the path to template cannot be found.

In fact, I am very puzzled, the author of the article can successfully run the demo, how to come to me, but can not start it?

So here's the question:

1. What causes the program not to start?
What path does http () point to?

So I trace the log, as follows

[

2018/01/07 11:09:28 open template/html/404.html: The system cannot find the path specified.

]

The problem is that you can't find the path. With the first problem solved, the next step is to figure out which path http.Dir () is pointing to.

I looked at the official examples:


log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))

As you can see from the above example http. Dir("/usr/share/doc"), the path points to the absolute path in the linux system. Then the problem is solved: I just need to change the path of http.Dir () to a relative path at runtime, or use an absolute path.

Another example, using the http. StripPrefix() method:


// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

As you can see, tmpfiles is a subdirectory of the tmp directory.

Now that the problem is resolved, change the 1 code and run again


func Template_dir() string {
 template_dir := "E:\\project\\gotest\\src\\template"
 return template_dir
}

func main() {
 http.Handle("/css/", http.FileServer(http.Dir(Template_dir())))
 http.Handle("/js/", http.FileServer(http.Dir(Template_dir())))

 http.ListenAndServe(":8080", nil)
}

After compiling and running, enter localhost:8080/css/ into the browser, and you can see the admin.css file in the directory template/css/ successfully.


Related articles: