mirror of
https://github.com/nicksherron/bashhub-server.git
synced 2024-11-10 09:02:54 +08:00
Allow disabling user registrations
This commit is contained in:
parent
02931595c5
commit
4abbd5978e
2 changed files with 12 additions and 6 deletions
|
@ -40,6 +40,7 @@ var (
|
|||
logFile string
|
||||
dbPath string
|
||||
addr string
|
||||
registration bool
|
||||
traceProfile = os.Getenv("BH_SERVER_DEBUG_TRACE")
|
||||
cpuProfile = os.Getenv("BH_SERVER_DEBUG_CPU")
|
||||
memProfile = os.Getenv("BH_SERVER_DEBUG_MEM")
|
||||
|
@ -51,7 +52,7 @@ var (
|
|||
if cpuProfile != "" || memProfile != "" || traceProfile != "" {
|
||||
profileInit()
|
||||
}
|
||||
internal.Run(dbPath, logFile, addr)
|
||||
internal.Run(dbPath, logFile, addr, registration)
|
||||
},
|
||||
}
|
||||
)
|
||||
|
@ -69,6 +70,7 @@ func init() {
|
|||
rootCmd.PersistentFlags().StringVar(&logFile, "log", "", `Set filepath for HTTP log. "" logs to stderr`)
|
||||
rootCmd.PersistentFlags().StringVar(&dbPath, "db", sqlitePath(), "db location (sqlite or postgres)")
|
||||
rootCmd.PersistentFlags().StringVarP(&addr, "addr", "a", listenAddr(), "Ip and port to listen and serve on")
|
||||
rootCmd.PersistentFlags().BoolVarP(®istration, "registration", "r", true, "Allow user registration")
|
||||
|
||||
}
|
||||
|
||||
|
@ -78,7 +80,7 @@ func startupMessage() {
|
|||
_ _ _ _
|
||||
| | | | | | | | version: %v
|
||||
| |__ __ _ ___| |__ | |__ _ _| | address: %v
|
||||
| '_ \ / _' / __| '_ \| '_ \| | | | '_ \
|
||||
| '_ \ / _' / __| '_ \| '_ \| | | | '_ \ registration: %v
|
||||
| |_) | (_| \__ \ | | | | | | |_| | |_) |
|
||||
|_.__/ \__,_|___/_| |_|_| |_|\__,_|_.__/
|
||||
___ ___ _ ____ _____ _ __
|
||||
|
@ -86,7 +88,7 @@ func startupMessage() {
|
|||
\__ \ __/ | \ V / __/ |
|
||||
|___/\___|_| \_/ \___|_|
|
||||
|
||||
`, Version, addr)
|
||||
`, Version, addr, registration)
|
||||
color.HiGreen(banner)
|
||||
log.Printf("\nListening and serving HTTP on %v\n", addr)
|
||||
}
|
||||
|
|
|
@ -130,7 +130,7 @@ func loggerWithFormatterWriter(logFile string, f gin.LogFormatter) gin.HandlerFu
|
|||
}
|
||||
|
||||
// configure routes and middleware
|
||||
func setupRouter(dbPath string, logFile string) *gin.Engine {
|
||||
func setupRouter(dbPath string, logFile string, registration bool) *gin.Engine {
|
||||
dbInit(dbPath)
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
|
@ -233,6 +233,10 @@ func setupRouter(dbPath string, logFile string) *gin.Engine {
|
|||
|
||||
r.POST("/api/v1/user", func(c *gin.Context) {
|
||||
var user User
|
||||
if !registration {
|
||||
c.String(403, "Registration of new users is not allowed.")
|
||||
return
|
||||
}
|
||||
if err := c.ShouldBindJSON(&user); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
@ -470,8 +474,8 @@ func setupRouter(dbPath string, logFile string) *gin.Engine {
|
|||
}
|
||||
|
||||
// Run starts server
|
||||
func Run(dbFile string, logFile string, addr string) {
|
||||
r := setupRouter(dbFile, logFile)
|
||||
func Run(dbFile string, logFile string, addr string, registration bool) {
|
||||
r := setupRouter(dbFile, logFile, registration)
|
||||
|
||||
addr = strings.ReplaceAll(addr, "http://", "")
|
||||
err := r.Run(addr)
|
||||
|
|
Loading…
Reference in a new issue