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.
star/httpserver/cmd.go

99 lines
3.2 KiB
Go

2 years ago
package httpserver
import (
"b612.me/starlog"
"b612.me/staros"
"context"
8 months ago
"encoding/json"
2 years ago
"github.com/spf13/cobra"
"os"
"os/signal"
"strings"
)
var s HttpServer
var daemon bool
8 months ago
var hooks string
2 years ago
func init() {
8 months ago
Cmd.Flags().StringVarP(&hooks, "hook", "H", "", "fileget hook for modify")
2 years ago
Cmd.Flags().StringVarP(&s.port, "port", "p", "80", "监听端口")
Cmd.Flags().StringVarP(&s.addr, "ip", "i", "0.0.0.0", "监听ip")
Cmd.Flags().StringVarP(&s.envPath, "folder", "f", "./", "本地文件地址")
Cmd.Flags().StringVarP(&s.uploadFolder, "upload", "u", "", "文件上传文件夹路径")
Cmd.Flags().BoolVarP(&daemon, "daemon", "d", false, "以后台进程运行")
Cmd.Flags().StringVarP(&s.basicAuthUser, "auth", "a", "", "HTTP BASIC AUTH认证(用户名:密码)")
Cmd.Flags().StringVarP(&s.indexFile, "index", "n", "", "Index文件名如index.html")
Cmd.Flags().StringVarP(&s.logpath, "log", "l", "", "log地址")
Cmd.Flags().StringVarP(&s.cert, "ssl-cert", "c", "", "TLS证书路径")
Cmd.Flags().StringVarP(&s.key, "ssl-key", "k", "", "TLS密钥路径")
Cmd.Flags().BoolVarP(&s.disableMIME, "disablemime", "m", false, "停止解析MIME全部按下载文件处理")
Cmd.Flags().StringSliceVarP(&s.protectAuthPage, "protect-page", "P", []string{}, "Basic Auth 开启白名单")
Cmd.Flags().StringVar(&s.page401, "401", "", "自定义401页面地址")
Cmd.Flags().StringVar(&s.page403, "403", "", "自定义403页面地址")
Cmd.Flags().StringVar(&s.page404, "404", "", "自定义404页面地址")
Cmd.Flags().BoolVarP(&s.httpDebug, "debug", "D", false, "开启调试模式")
2 years ago
Cmd.Flags().Bool("daeapplied", false, "")
Cmd.Flags().MarkHidden("daeapplied")
}
var Cmd = &cobra.Command{
Use: "http",
Short: "HTTP文件服务器(HTTP File Browser Server)",
Long: `HTTP文件服务器(HTTP File Browser Server)`,
Run: func(cmd *cobra.Command, args []string) {
8 months ago
if hooks != "" {
if !staros.Exists(hooks) {
starlog.Criticalln("hook file not exists")
os.Exit(2)
}
data, err := os.ReadFile(hooks)
if err != nil {
starlog.Criticalln("read hook file error", err)
os.Exit(3)
}
var hk []ServerHook
err = json.Unmarshal(data, &hk)
if err != nil {
starlog.Errorln("Unmarshal hook Json Failed", err)
os.Exit(4)
}
s.hooks = hk
}
2 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.FgGreen}, "Success,PID=%v\n", pid)
return
}
sig := make(chan os.Signal)
signal.Notify(sig, os.Kill, os.Interrupt)
ctx, fn := context.WithCancel(context.Background())
go func() {
starlog.Infoln("Recv Signal", <-sig)
fn()
}()
if s.basicAuthUser != "" {
tmp := strings.SplitN(s.basicAuthUser, ":", 2)
if len(tmp) != 2 {
starlog.Errorln("basic Auth should have user and password")
os.Exit(2)
}
s.basicAuthUser = strings.TrimSpace(tmp[0])
s.basicAuthPwd = tmp[1]
}
err := s.Run(ctx)
if err != nil {
starlog.Errorln("Http Server Closed by Errors", err)
os.Exit(1)
}
starlog.Infoln("Http Server Closed Normally")
},
}