分享
golang为LigerUI编写简易版本web服务器
coolyylu · · 2340 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
package main import ( "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.Handler func 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.1 req.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 }
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信2340 次点击
上一篇:golang环境搭建
下一篇:搭建go语言idea开发环境
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
package main import ( "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.Handler func 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.1 req.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 }