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)

}

 

Tags
请我喝咖啡

感谢支持,我会继续更新更有用的技术内容。

打赏二维码
请我喝咖啡 如果内容帮到了你,可以赞赏支持继续更新。
Category
Tags
Site statistics

本站现有文章218篇,共被浏览140573

本次响应耗时: 0.188s

当前来路IP: 18.97.14.83  

您是本站第: 255828 位访客!

本站已苟活: 

Commercial
开发者产品赞助位开放

适合 AI 工具、云服务、课程、开源项目和招聘团队。

查看合作方案
All hots
Article archiving
Mongona Radio
等待播放