This is a creation in Article, where the information may have evolved or changed.
Title:siren-iot Server build 1:golang web Getting Started
Date:2017-08-27 14:55:55
Category: Default category
This article describes Sensor-iot Server build: Golang web Getting Started
Siren-iot Server build 1:golang web Getting Started
This article is original written by Jin Tian, welcome re-post, and please keep the copyright info, thanks, any question Could be asked via WeChat:jintianiloveu
Golang 15 lines of code to build a Web server
First of all, welcome to the 21st century, if 21st century you are still using Apache,tomcat,java .... It has to be said that you are going out of date, if you are still using nginx etc. You have to say, you're getting out. In the coming 21st century, with the C-language no longer supported, there is no doubt that rust and go will be their followers and even later, and in between, I will use Golang, not because of the strong support of Google behind Golang, but because, Golang, although not Rust's speed, has Python's fast and easy-to-understand language features. Let's build a Web server with 15 lines of code!
Normally, if you use Java, you will need at least a day to configure your environment. If you use Python, you need to install a variety of packages and dependencies, or even a variety of database migration. More trouble. With Golang, your binaries are compiled and run directly on the server. Simple, lightweight, efficient, fast and non-polluting.
More important is the!!!!! do you still need Nginx???? The answer is, no need!!! Golang 15 Line Code implementation of the service-side program, do not need nginx, do not need!!! Listen to the local port directly!
package mainimport ( "log" "fmt" "net/http")func sayHello(w http.ResponseWriter, r *http.Request) { // parse request r.ParseForm() fmt.Println("request form: ", r.Form) fmt.Print("request url: ", r.URL.Path) fmt.Println("request scheme: ", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key: ", k) fmt.Println("val: ", v) } fmt.Fprintf(w, "helllllo!!! this is from Siren!!!")}func main() { fmt.Println("hello, Go lang.") http.HandleFunc("/", sayHello) err := http.ListenAndServe(":9000", nil) if err != nil { log.Fatal("Listen and serve: ", err) }}
Compile and run, this program has been listening to the 9000 port.
Think about what siren needs.
OK, next we may need to stand on the product manager's point of view to think about, I want to siren to achieve what function:
- The name must be very siren. This depends on the function, my ideal siren is a keen sniffer, I will be in its internal integration of a lot of reptiles, crawler countless reptiles daily task is to sniff a variety of Web site updates, such as the GitHub leaderboard Ah, Kickstar interesting project Ah, Hackday on the project update Ah, And so on, all the information will be subject to the same structure, I can arbitrarily adjust the information, of course, the kernel is based on the internal natural language processing mechanism Jarvis;
- Siren must be able to send information to the client, and to be able to receive the sensor data, such as I want to let siren access my temperature sensor, Arduino found that the temperature sensor is higher than 30 degrees, this is not normal, so send a message to Siren,siren must promptly notify me;
- I can control all my devices through siren, such as my UAV is suitable for siren link, then I can control the drone through siren, even say later my refrigerator, my air conditioning all need through a mastermind and siren to connect, siren all the sensor information distribution center, The mastermind of all information processing is that Jarvis,jarvis can only operate on Python, and siren and Jarvis can only communicate through sockets;
Able to achieve the above functions, basically my siren is also the initial embryonic. Finally, Siren is the messenger of the IoT message that we're going to build, and she's an image of a siren in ancient Greek mythology:
Test
What, cool not cool, sex not sexy?
Discussion on Golang of MTPP protocol
OK, since to let Siren realize the interconnection of all things, then the push of the message must be done. The client is very good, the question is whether the server can do so? Let's not think about high concurrency for a while, and say, can I have device A and device B do instant messaging? White, is to build an instant chat software.