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 logFile string
dbPath string dbPath string
addr string addr string
registration bool
traceProfile = os.Getenv("BH_SERVER_DEBUG_TRACE") traceProfile = os.Getenv("BH_SERVER_DEBUG_TRACE")
cpuProfile = os.Getenv("BH_SERVER_DEBUG_CPU") cpuProfile = os.Getenv("BH_SERVER_DEBUG_CPU")
memProfile = os.Getenv("BH_SERVER_DEBUG_MEM") memProfile = os.Getenv("BH_SERVER_DEBUG_MEM")
@ -51,7 +52,7 @@ var (
if cpuProfile != "" || memProfile != "" || traceProfile != "" { if cpuProfile != "" || memProfile != "" || traceProfile != "" {
profileInit() 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(&logFile, "log", "", `Set filepath for HTTP log. "" logs to stderr`)
rootCmd.PersistentFlags().StringVar(&dbPath, "db", sqlitePath(), "db location (sqlite or postgres)") 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().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 | | | | | | | | version: %v
| |__ __ _ ___| |__ | |__ _ _| | address: %v | |__ __ _ ___| |__ | |__ _ _| | address: %v
| '_ \ / _' / __| '_ \| '_ \| | | | '_ \ | '_ \ / _' / __| '_ \| '_ \| | | | '_ \ registration: %v
| |_) | (_| \__ \ | | | | | | |_| | |_) | | |_) | (_| \__ \ | | | | | | |_| | |_) |
|_.__/ \__,_|___/_| |_|_| |_|\__,_|_.__/ |_.__/ \__,_|___/_| |_|_| |_|\__,_|_.__/
___ ___ _ ____ _____ _ __ ___ ___ _ ____ _____ _ __
@ -86,7 +88,7 @@ func startupMessage() {
\__ \ __/ | \ V / __/ | \__ \ __/ | \ V / __/ |
|___/\___|_| \_/ \___|_| |___/\___|_| \_/ \___|_|
`, Version, addr) `, Version, addr, registration)
color.HiGreen(banner) color.HiGreen(banner)
log.Printf("\nListening and serving HTTP on %v\n", addr) 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 // configure routes and middleware
func setupRouter(dbPath string, logFile string) *gin.Engine { func setupRouter(dbPath string, logFile string, registration bool) *gin.Engine {
dbInit(dbPath) dbInit(dbPath)
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
r := gin.New() r := gin.New()
@ -233,6 +233,10 @@ func setupRouter(dbPath string, logFile string) *gin.Engine {
r.POST("/api/v1/user", func(c *gin.Context) { r.POST("/api/v1/user", func(c *gin.Context) {
var user User var user User
if !registration {
c.String(403, "Registration of new users is not allowed.")
return
}
if err := c.ShouldBindJSON(&user); err != nil { if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
@ -470,8 +474,8 @@ func setupRouter(dbPath string, logFile string) *gin.Engine {
} }
// Run starts server // Run starts server
func Run(dbFile string, logFile string, addr string) { func Run(dbFile string, logFile string, addr string, registration bool) {
r := setupRouter(dbFile, logFile) r := setupRouter(dbFile, logFile, registration)
addr = strings.ReplaceAll(addr, "http://", "") addr = strings.ReplaceAll(addr, "http://", "")
err := r.Run(addr) err := r.Run(addr)