mirror of
https://github.com/foolin/goview.git
synced 2025-09-26 19:01:15 +08:00
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
/*
|
|
* Copyright 2018 Foolin. All rights reserved.
|
|
*
|
|
* Use of this source code is governed by a MIT style
|
|
* license that can be found in the LICENSE file.
|
|
*
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/GeertJohan/go.rice"
|
|
"github.com/foolin/goview"
|
|
"github.com/foolin/goview/supports/gorice"
|
|
"net/http"
|
|
)
|
|
|
|
func main() {
|
|
|
|
//static
|
|
staticBox := rice.MustFindBox("static")
|
|
staticFileServer := http.StripPrefix("/static/", http.FileServer(staticBox.HTTPBox()))
|
|
http.Handle("/static/", staticFileServer)
|
|
|
|
//new view engine
|
|
gv := gorice.New(rice.MustFindBox("views"))
|
|
//set engine for default instance
|
|
goview.Use(gv)
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
err := goview.Render(w, http.StatusOK, "index", goview.M{
|
|
"title": "Index title!",
|
|
"add": func(a int, b int) int {
|
|
return a + b
|
|
},
|
|
})
|
|
if err != nil {
|
|
fmt.Fprintf(w, "Render index error: %v!", err)
|
|
}
|
|
|
|
})
|
|
|
|
http.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
|
|
err := goview.Render(w, http.StatusOK, "page.html", goview.M{"title": "Page file title!!"})
|
|
if err != nil {
|
|
fmt.Fprintf(w, "Render page.html error: %v!", err)
|
|
}
|
|
})
|
|
|
|
fmt.Println("Listening and serving HTTP on :9090")
|
|
http.ListenAndServe(":9090", nil)
|
|
}
|