前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >static web server by go

static web server by go

作者头像
用户2458545
发布2022-09-07 11:32:57
2040
发布2022-09-07 11:32:57
举报
文章被收录于专栏:阿牛的牙
代码语言: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 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)

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年5月25日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档