golang为LigerUI编写简易版本web服务器
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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步