You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Victorique/vtqe/http.go

94 lines
2.8 KiB
Go

4 years ago
package main
import (
4 years ago
"Victorique/vtqe/httpserver"
"os"
"os/signal"
"syscall"
"b612.me/starlog"
"b612.me/staros"
4 years ago
"github.com/spf13/cobra"
)
4 years ago
var httpPort, httpIP, httpPath, httpBasicAuth, httpCertKey, logPath, httpIndexFile string
var doUpload, daemon, httpStopMime bool
4 years ago
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, "是否开启文件上传")
4 years ago
httpcmd.Flags().BoolVarP(&daemon, "daemon", "d", false, "以后台进程运行")
4 years ago
httpcmd.Flags().StringVarP(&httpBasicAuth, "auth", "a", "", "HTTP BASIC AUTH认证(用户名:密码)")
4 years ago
httpcmd.Flags().StringVarP(&httpIndexFile, "index", "n", "", "Index文件名如index.html")
httpcmd.Flags().StringVarP(&logPath, "log", "l", "", "log地址")
4 years ago
httpcmd.Flags().StringVarP(&httpCertKey, "cert", "c", "", "TLS证书路径用:分割证书与密钥")
4 years ago
httpcmd.Flags().BoolVarP(&httpStopMime, "disablemime", "m", false, "停止解析MIME全部按下载文件处理")
httpcmd.Flags().Bool("daeapplied", false, "")
httpcmd.Flags().MarkHidden("daeapplied")
4 years ago
}
// httpCmd represents the http command
var httpcmd = &cobra.Command{
Use: "http",
Short: "HTTP文件服务器",
Long: `HTTP文件服务器`,
Run: func(cmd *cobra.Command, args []string) {
4 years ago
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.BgGreen}, "Success,PID=%v\n", pid)
return
4 years ago
}
4 years ago
err := run()
if err != nil {
starlog.Errorln("Http Server Closed by Errors")
os.Exit(4)
}
starlog.Infoln("Http Server Closed Normally")
return
4 years ago
},
}
4 years ago
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_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
}
}