94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package main
|
||
|
||
import (
|
||
"Victorique/vtqe/httpserver"
|
||
"os"
|
||
"os/signal"
|
||
"syscall"
|
||
|
||
"b612.me/starlog"
|
||
|
||
"b612.me/staros"
|
||
|
||
"github.com/spf13/cobra"
|
||
)
|
||
|
||
var httpPort, httpIP, httpPath, httpBasicAuth, httpCertKey, logPath, httpIndexFile string
|
||
var doUpload, daemon, httpStopMime bool
|
||
|
||
func init() {
|
||
httpcmd.Flags().StringVarP(&httpPort, "port", "p", "80", "监听端口")
|
||
httpcmd.Flags().StringVarP(&httpIP, "ip", "i", "0.0.0.0", "监听ip")
|
||
httpcmd.Flags().StringVarP(&httpPath, "folder", "f", "./", "本地文件地址")
|
||
httpcmd.Flags().BoolVarP(&doUpload, "upload", "u", false, "是否开启文件上传")
|
||
httpcmd.Flags().BoolVarP(&daemon, "daemon", "d", false, "以后台进程运行")
|
||
httpcmd.Flags().StringVarP(&httpBasicAuth, "auth", "a", "", "HTTP BASIC AUTH认证(用户名:密码)")
|
||
httpcmd.Flags().StringVarP(&httpIndexFile, "index", "n", "", "Index文件名,如index.html")
|
||
httpcmd.Flags().StringVarP(&logPath, "log", "l", "", "log地址")
|
||
httpcmd.Flags().StringVarP(&httpCertKey, "cert", "c", "", "TLS证书路径,用:分割证书与密钥")
|
||
httpcmd.Flags().BoolVarP(&httpStopMime, "disablemime", "m", false, "停止解析MIME,全部按下载文件处理")
|
||
httpcmd.Flags().Bool("daeapplied", false, "")
|
||
httpcmd.Flags().MarkHidden("daeapplied")
|
||
}
|
||
|
||
// httpCmd represents the http command
|
||
var httpcmd = &cobra.Command{
|
||
Use: "http",
|
||
Short: "HTTP文件服务器",
|
||
Long: `HTTP文件服务器`,
|
||
Run: func(cmd *cobra.Command, args []string) {
|
||
apply, _ := cmd.Flags().GetBool("daeapplied")
|
||
if daemon && !apply {
|
||
nArgs := append(os.Args[1:], "--daeapplied")
|
||
pid, err := staros.Daemon(os.Args[0], nArgs...)
|
||
if err != nil {
|
||
starlog.Criticalln("Daemon Error:", err)
|
||
os.Exit(1)
|
||
}
|
||
starlog.StdPrintf([]starlog.Attr{starlog.FgGreen}, "Success,PID=%v\n", pid)
|
||
return
|
||
}
|
||
err := run()
|
||
if err != nil {
|
||
starlog.Errorln("Http Server Closed by Errors")
|
||
os.Exit(4)
|
||
}
|
||
starlog.Infoln("Http Server Closed Normally")
|
||
return
|
||
},
|
||
}
|
||
|
||
func run() error {
|
||
if logPath != "" {
|
||
if !staros.Exists(logPath) {
|
||
err := starlog.SetLogFile(logPath)
|
||
if err != nil {
|
||
starlog.Errorln("Create LogFile Failed:", err)
|
||
os.Exit(2)
|
||
}
|
||
defer starlog.Close()
|
||
} else {
|
||
logFp, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND, 0755)
|
||
if err != nil {
|
||
starlog.Errorln("Create LogFile Failed:", err)
|
||
os.Exit(2)
|
||
}
|
||
defer logFp.Close()
|
||
starlog.Std.SwitchOut(logFp)
|
||
}
|
||
}
|
||
stopChan := make(chan os.Signal, 1)
|
||
overChan := make(chan error)
|
||
signal.Notify(stopChan, syscall.SIGINT, syscall.SIGKILL)
|
||
go func(stop chan<- error) {
|
||
err := httpserver.RunHttpServer(httpIP, httpPort, httpBasicAuth, httpPath, httpCertKey, vtqe_version, httpIndexFile, doUpload, httpStopMime)
|
||
stop <- err
|
||
}(overChan)
|
||
select {
|
||
case <-stopChan:
|
||
return nil
|
||
case err := <-overChan:
|
||
return err
|
||
}
|
||
}
|