From 4abbd5978e5a85d0d970f9128a546c97b9f9bee4 Mon Sep 17 00:00:00 2001 From: Petr Hejl Date: Mon, 4 Jan 2021 14:43:19 +0100 Subject: [PATCH] Allow disabling user registrations --- cmd/root.go | 8 +++++--- internal/server.go | 10 +++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 43a0866..8b02fd5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) } diff --git a/internal/server.go b/internal/server.go index 8b126a2..221e45e 100644 --- a/internal/server.go +++ b/internal/server.go @@ -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)