美文网首页
2018-05-18 第七周

2018-05-18 第七周

作者: 朝着他们笑 | 来源:发表于2018-06-14 23:57 被阅读0次

    使用Go写后端架构(二)

    上一周的学习中,已经将一个简易的后端demo写完,现在需要进一步完善。

    网页分为1个主页和5个子页面

    通过编写不同的页面跳转函数来实现后端的内部结构:

    func HomePage(w http.ResponseWriter, r *http.Request) {

    if r.Method == "GET" {

    t, _ := template.ParseFiles("./h5/homepage.html")

    t.Execute(w, nil)

    }

    }

    func SubPage1(w http.ResponseWriter, r *http.Request) {

    if r.Method == "GET" {

    t, _ := template.ParseFiles("./h5/subpage_1.html")

    t.Execute(w, nil)

    }

    }

    func SubPage2(w http.ResponseWriter, r *http.Request) {

    if r.Method == "GET" {

    t, _ := template.ParseFiles("./h5/subpage_2.html")

    t.Execute(w, nil)

    }

    }

    func SubPage3(w http.ResponseWriter, r *http.Request) {

    if r.Method == "GET" {

    t, _ := template.ParseFiles("./h5/subpage_3.html")

    t.Execute(w, nil)

    }

    }

    func SubPage4(w http.ResponseWriter, r *http.Request) {

    if r.Method == "GET" {

    t, _ := template.ParseFiles("./h5/subpage_4.html")

    t.Execute(w, nil)

    }

    }

    func SubPage5(w http.ResponseWriter, r *http.Request) {

    if r.Method == "GET" {

    t, _ := template.ParseFiles("./h5/subpage_5.html")

    t.Execute(w, nil)

    }

    }

    再通过get函数获取url信息进行跳转:

    func API(w http.ResponseWriter, r *http.Request) {

    if r.Method == "GET" {

    r.ParseForm()

    content := r.Form["content"][0]

    resp1, _ := http.Get(strings.Join([]string{"http://127.0.0.1:1024/?content=", content}, ""))

    defer resp1.Body.Close()

    body, _ := ioutil.ReadAll(resp1.Body)

    resp2, _ := http.Get(strings.Join([]string{"http://127.0.0.1:1025/?vec=", string(body)}, ""))

    defer resp2.Body.Close()

    ret, _ := ioutil.ReadAll(resp2.Body)

    fmt.Fprintf(w, string(ret))

    }

    }

    同时,在go编译器需要现运行的main中绑定相关函数与后缀名:

    func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

    if r.URL.Path != "/" {

    t, _ := template.ParseFiles("h5/404.html")

    t.Execute(w, nil)

    }

    })

    http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css/")))) //定义静态资源的路由

    http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./js/"))))    //定义静态资源的路由

    http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("./img/")))) //定义静态资源的路由

    http.Handle("/h5/", http.StripPrefix("/h5/", http.FileServer(http.Dir("./h5/"))))    //定义静态资源的路由

    http.HandleFunc("/review", server.HomePage)

    http.HandleFunc("/cnn", server.SubPage1)

    http.HandleFunc("/rnn", server.SubPage2)

    http.HandleFunc("/svm", server.SubPage3)

    http.HandleFunc("/nb", server.SubPage4)

    http.HandleFunc("/lycoris", server.SubPage5)

    http.HandleFunc("/api", server.API)

    err := http.ListenAndServe(":1023", nil)

    if err != nil {

    log.Fatal("ListenAndServe: ", err)

    }

    }

    相关文章

      网友评论

          本文标题:2018-05-18 第七周

          本文链接:https://www.haomeiwen.com/subject/ciiueftx.html