代码语言:javascript复制
/*
Create on: 2018-10-05 下午6:50
author: sato
mail: ysudqfs@163.com
life is short, you need go go go
*/
package main
import (
"fmt"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
)
//October 05, 2018 - 22:27:57
//Django version 2.1.1, using settings 'eoms.settings'
//Starting development server at http://127.0.0.1:8000/
//Quit the server with CONTROL-C.
// fmt.Println("Usage: ./myweb -d=/yourpath [-p=yourport]")
func main() { // 简单静态服务器
defer func() {
if x := recover(); x != nil {
fmt.Println("Usage: ./myweb -d=[/yourpath default current path] -p=[yourport, default 9870] ")
fmt.Println("Ensure the path include index.html")
fmt.Println("Try to use -help for more information")
}
}()
Logic()
}
func Logic() {
if len(os.Args) == 1 {
fmt.Println(time.Now())
fmt.Println("Generated by", runtime.Version())
fmt.Println("Starting development server at http://127.0.0.1:9870")
fmt.Println("Enjoy that!")
fmt.Println("Quit the server with CONTROL-C.")
StaticWeb("./", 9870)
} else if os.Args[1] == "-help" {
fmt.Println("This is a easy StaticWebservernyou can run it by: ./filname -d=path -p=port!")
fmt.Println("It is fantastic, you can run anywhere you want, just you like, you will like it! ")
fmt.Println("Last! make sure your port in 1024 - 65535")
return
} else if len(os.Args) == 2 {
path := strings.Split(os.Args[1], "=")[1]
_, err := os.Stat(path "/index.html")
if err != nil {
panic("error")
}
fmt.Println(time.Now())
fmt.Println("Generated by", runtime.Version())
fmt.Println("Starting development server at http://127.0.0.1:9870")
fmt.Println("Enjoy that!")
fmt.Println("Quit the server with CONTROL-C.")
StaticWeb(path, 9870)
} else if len(os.Args) == 3 {
port, err := strconv.Atoi(strings.Split(os.Args[2], "=")[1])
if err != nil {
panic("error")
}
if port <= 1023 || port > 65535 {
panic("inlegal")
}
fmt.Println(time.Now())
fmt.Println("Generated by", runtime.Version())
fmt.Printf("Starting development server at http://127.0.0.1:%dn", port)
fmt.Println("Enjoy that!")
fmt.Println("Quit the server with CONTROL-C.")
StaticWeb(strings.Split(os.Args[1], "=")[1], port)
} else {
panic("error")
}
}
//"/home/kali/Desktop/dreamwork/front_end_pc"
func StaticWeb(dir string, port int) {
http.Handle("/", http.FileServer(http.Dir(dir)))
http.ListenAndServe(":" strconv.Itoa(port), nil)
}