【Go】简易到简陋的web服务器
在上篇的读数据库代码上直接修改的。之后应该不会有下文了,感觉成功加入黑历史……还是找个框架,读读代码,快点上手吧。自己摸索有点迷迷糊糊……
Go写静态网页服务器,为浏览器返回网页。网页里的脚本向服务器上的动态接口请求了数据,拿到数据后灌进网页。总结:静态网页+动态内容的简陋服务器……
【Go】本来想把数据库的内容和网页做个交互,但是时间原因,先放过……
package main import ( //"path/filepath" "bufio" "io" "os" "fmt" "log" "net/http" "strconv" "database/sql" _ "github.com/go-sql-driver/mysql" ) func AddHandler(w http.ResponseWriter, r *http.Request) { //dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) // 运行路径罢辽 //fmt.Println(dir) fmt.Println(r.URL.Path) if r.URL.Path == "/test" { f, err := os.Open("path/main.html") // 得填绝对路径…… if err == nil { defer f.Close() bfRd := bufio.NewReader(f) // 读取静态网页内容并返回给浏览器 for { str, err := bfRd.ReadString('\n') fmt.Fprintln(w, str) if err != nil { fmt.Println("Read Err", err) if err == io.EOF { return } } } } fmt.Println("Open err") } vars := r.URL.Query() a := vars.Get("para") b := vars.Get("para2") inta, _ := strconv.Atoi(a) intb, _ := strconv.Atoi(b) fmt.Println(r.Method, r.URL, r.Host, vars) fmt.Println(r.Proto) fmt.Fprintln(w, inta+intb) // vars["para"][0]+vars["para2"][0]) } type Good struct { Name string Count string Price string } func CheckErr(err error, paras ...string) { if err != nil { for _, val := range paras { log.Println(val) } log.Fatal(err) } } func main() { db, _ := sql.Open("mysql", "root:pwd@(127.0.0.1:3306)/mysql") defer db.Close() err := db.Ping() CheckErr(err, "SQLErr") log.Println("Success") query := "select * from goods" rows, _ := db.Query(query) var id, name, count, price string var nameCount map[string] int nameCount = make(map[string]int) for rows.Next() { rows.Scan(&id, &name, &count, &price) _count, err := strconv.Atoi(count) CheckErr(err, id, name, count, price) _, ok := nameCount[name] if (ok) { nameCount[name] += _count } else { nameCount[name] = _count } } for k, v := range nameCount { fmt.Println(k, v) } http.HandleFunc("/", AddHandler) log.Println("Listen : 15233") err = http.ListenAndServe("0.0.0.0:15233", nil) CheckErr(err, "ListenErr") fmt.Println("Start open") }
【HTML】html是拿以前的部件改的……有点丑。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Show plus</title> <link rel="stylesheet" href="http://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <style> .plus .calcu { padding: 40px; background-color: #FF0000; text-align: center; font: 10px; } </style> <script src="http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <script> $(document).ready(function() { [ [11, 15, ".plus .plus-fifteen"], [12, 30, ".plus .plus-thirty"], [13, 90, ".plus .plus-ninety"] ].forEach(function(v) { $.get("/get?para=" + v[0] + "¶2=" + v[1], function(data) { if (data.length == 0) { $(v[2]).text("blank"); } else { $(v[2]).text(data); } }); }); }) </script> </head> <body> <div class="row plus"> <div class="col-md-9"> <div id="recommendation-carousel" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#recommendation-carousel" data-slide-to="0" class="active"></li> <li data-target="#recommendation-carousel" data-slide-to="1"></li> <li data-target="#recommendation-carousel" data-slide-to="2"></li> </ol> <div class="carousel-inner"> <div class="item active"> <div class="calcu"> 11+15= <div class="plus-fifteen"> </div> </div> </div> <div class="item"> <div class="calcu"> 12+30= <div class="plus-thirty"> </div> </div> </div> <div class="item"> <div class="calcu"> 13+90= <div class="plus-ninety"> </div> </div> </div> <a class="left carousel-control" href="#recommendation-carousel" role="button" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> </a> <a class="right carousel-control" href="#recommendation-carousel" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> </a> </div> </div> </div> </body> </html>
Anyway,做个记录。接下来打算跟着走:
https://github.com/geektutu/7days-golang
https://geektutu.com/post/gee.html
-------------------------------------------------------------------------------
5月6日更新
除了最后一章有点看不太懂,前面感觉差不多了。其实看起来还是蛮快的,每天大概用1~2个小时吧。学习方式是先看过一遍,然后像leetcode做题一样自己理清输入输出,然后完善函数。不过到后面几章总忘前面的内容,所以还是要多复习。
五一过得有点混乱,学习动力有所削弱,身体也不太舒服。接下来加强锻炼吧,马上开始work了!
另外找到了新的学习内容,想跟着细捋一遍: