Allow disabling user registrations

This commit is contained in:
Petr Hejl 2021-01-04 14:43:19 +01:00
parent 02931595c5
commit 4abbd5978e
2 changed files with 12 additions and 6 deletions

View file

@ -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(&registration, "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)
}

View file

@ -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)