golang为LigerUI编写简易版本web服务器
生活随笔
收集整理的這篇文章主要介紹了
golang为LigerUI编写简易版本web服务器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package mainimport ("io/ioutil""log""net/http""os"
)var zpath string = "D:/Download/jQuery LigerUI V1.3.2/Source/" //LigerUI安裝路徑
var zport string = "80"
var zsource_file = "source.config"
var zport_file = "port.config"
var staticHandler http.Handlerfunc Init() {if sourceExist(zport_file) {log.Println("Port file is " + zport_file)ztemp_port := filetostr(zport_file)if ztemp_port != "" {zport = ztemp_port}} else {log.Println("Port file not exist ,please set server port in file " + zport_file)log.Println("Server will use default port 80")}if sourceExist(zsource_file) {log.Println("Config file is " + zsource_file)ztemp_path := filetostr(zsource_file)if ztemp_path != "" {zpath = ztemp_path}} else {log.Println("Source file not exist ,please set LigerUI source path in file " + zsource_file)log.Println("Server will use default LigerUI path")}staticHandler = http.FileServer(http.Dir(zpath))
}func indexPage(w http.ResponseWriter, req *http.Request) {log.Println(req.URL.Path)if req.URL.Path != "/" {staticHandler.ServeHTTP(w, req)return}//處理主頁127.0.0.1req.URL.Path = "/index.htm"staticHandler.ServeHTTP(w, req)
}func main() {Init()log.Println("LigerUI Source Path:")log.Println(zpath)zport_str := ""if zport != "80" {zport_str = ":" + zport}log.Println("Start LigerUI Server " + "127.0.0.1" + zport_str)http.HandleFunc("/", indexPage)err := http.ListenAndServe(":"+zport, nil)if err != nil {log.Fatal("ListenAndServe: ", err)}}func sourceExist(filename string) bool {_, err := os.Stat(filename)return err == nil || os.IsExist(err)
}func filetostr(zfilename string) string {zbyte, err := ioutil.ReadFile(zfilename)zstr := ""if err == nil {zstr = string(zbyte)}return zstr
}
?
轉載于:https://www.cnblogs.com/coolyylu/p/5152202.html
總結
以上是生活随笔為你收集整理的golang为LigerUI编写简易版本web服务器的全部內容,希望文章能夠幫你解決所遇到的問題。