add http auth

This commit is contained in:
YouROK
2021-03-03 12:31:23 +03:00
parent f108b7c718
commit 453238abbc

32
server/web/auth/auth.go Normal file
View File

@@ -0,0 +1,32 @@
package auth
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"github.com/gin-gonic/gin"
"server/settings"
)
func SetupAuth(engine *gin.Engine) *gin.RouterGroup {
if !settings.HttpAuth {
return nil
}
accs := getAccounts()
if accs == nil {
return nil
}
return engine.Group("/", gin.BasicAuth(accs))
}
func getAccounts() gin.Accounts {
buf, err := ioutil.ReadFile(filepath.Join(settings.Path, "accs.db"))
if err != nil {
return nil
}
var accs gin.Accounts
json.Unmarshal(buf, &accs)
return accs
}