Merge pull request #18 from phejl/allow-disable-registrations

Allow disabling user registrations
This commit is contained in:
Nick Sherron 2021-01-07 13:52:42 -05:00 committed by GitHub
commit 1a38bc37d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 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)

View file

@ -75,7 +75,7 @@ func TestMain(m *testing.M) {
dbPath := filepath.Join(testDir, "test.db")
logFile := filepath.Join(testDir, "server.log")
log.Print("sqlite tests")
router = setupRouter(dbPath, logFile)
router = setupRouter(dbPath, logFile, true)
system = sysStruct{
user: "tester",