mongona

mongona
-- --
正在获取天气

static web server by go

/*
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 StaticWebserver\nyou 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:%d\n", 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)

}

 

1
0
Tags
富强,民主,文明,和谐,自由,平等,公正,法治,爱国,敬业,诚信,友善。
打赏二维码
About
Sato
有一个人养了几条鱼死了,悲伤不已,他不想土葬,他说想给他火葬,把鱼灰撒回海洋让它回到母亲的怀抱……谁知道越烤越香,后来他就买了瓶啤酒…… 很多事情,走着走着就忘了初衷…… 几乎每个人都听过:“不忘初心,方得始终”,却少有人知道下一句:“初心易得,始终难守”。 人生是一场修行,很少有人能忠于自己内心的目标和生命的使命,因此善始者多,善终者少。
Category
Tags
Site statistics

本站现有文章16篇,共被浏览7909

本次响应耗时: 0.468s

当前来路IP: 54.147.29.160  美国

您是本站第: 46177 位访客!

本站已苟活: 

All hots
Article archiving