-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
静态文件、host、port 均可通过命令行传入,不再硬编码。
- Loading branch information
Showing
5 changed files
with
451 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github-proxy/middleware/logger" | ||
"github-proxy/server" | ||
) | ||
|
||
var ( | ||
log = logger.GetLogger() | ||
|
||
host string | ||
port uint | ||
) | ||
|
||
func isPathExists(path string) bool { | ||
_, err := os.Stat(path) | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func checkStatic(staticDir string) error { | ||
if !isPathExists(staticDir) { | ||
return errors.New("静态文件目录不存在") | ||
} | ||
|
||
index := path.Join(staticDir, "index.html") | ||
if !isPathExists(index) { | ||
return errors.New("index.html 不存在") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "gp [STATIC_DIR]", | ||
Short: "使用指定的静态文件启动 github proxy 代理服务", | ||
Args: func(_ *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return errors.New("缺少静态文件目录") | ||
} | ||
|
||
return checkStatic(args[0]) | ||
}, | ||
Run: func(_ *cobra.Command, args []string) { | ||
log.Info().Str("static-dir", args[0]).Str("host", host).Uint("port", port).Msg("命令行参数") | ||
|
||
server.Run(args[0], host, port) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.Flags().StringVar(&host, "host", "localhost", "本服务使用的主机") | ||
rootCmd.Flags().UintVar(&port, "port", 3000, "本服务使用的端口") | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatal().Err(err).Send() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.