zoukankan      html  css  js  c++  java
  • 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
    }

  • 相关阅读:
    [LintCode] Read Characters From File
    [LintCode] Insert Node in a Binary Search Tree
    [LintCode] Validate Binary Search Tree
    [LintCode] Merge Intervals
    [LintCode] Valid Parentheses
    [LintCode] First Position Unique Character
    三十七.MySQL视图 MySQL存储过程
    三十六.MHA集群概述 、 部署MHA集群 测试配置
    三十五.MySQL读写分离 MySQL多实例 、MySQL性能调优
    三十四.MySQL主从同步 、主从同步模式
  • 原文地址:https://www.cnblogs.com/coolyylu/p/5152202.html
Copyright © 2011-2022 走看看