add globale config
This commit is contained in:
parent
8be8077d94
commit
6962d40d78
7 changed files with 92 additions and 14 deletions
|
@ -7,3 +7,9 @@
|
||||||
|------|-------------|---------|
|
|------|-------------|---------|
|
||||||
| USERSTORE_SQLITE_PATH | Path to the Sqlite Database | `none` |
|
| USERSTORE_SQLITE_PATH | Path to the Sqlite Database | `none` |
|
||||||
| PORT | Port for the Webserver | 8080 |
|
| PORT | Port for the Webserver | 8080 |
|
||||||
|
| SMTP_SERVER | SMTP Server to send mails | `none` |
|
||||||
|
| SMTP_PORT | SMTP Port | 587 |
|
||||||
|
| SMTP_USERNAME | SMTP Username | `none` |
|
||||||
|
| SMTP_PASSWORD | SMTP Password | `none` |
|
||||||
|
| SMTP_FROM | Mail address used as from for all mails | `none` |
|
||||||
|
| WEB_PUBLIC_REGISTRATION | Enabled the registration for anyone on the webgui | False |
|
||||||
|
|
13
main.go
13
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
|
||||||
|
"git.keks.cloud/kekskurse/miniauth/pkg/smtpclient"
|
||||||
"git.keks.cloud/kekskurse/miniauth/pkg/userstore"
|
"git.keks.cloud/kekskurse/miniauth/pkg/userstore"
|
||||||
"git.keks.cloud/kekskurse/miniauth/pkg/web"
|
"git.keks.cloud/kekskurse/miniauth/pkg/web"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -18,13 +19,15 @@ var templatesFS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cnf := config()
|
cnf := config()
|
||||||
router := setupRouter()
|
router := setupRouter(cnf)
|
||||||
router.Run(fmt.Sprintf(":%v", cnf.Port))
|
router.Run(fmt.Sprintf(":%v", cnf.Port))
|
||||||
}
|
}
|
||||||
|
|
||||||
type gloableConfig struct {
|
type gloableConfig struct {
|
||||||
UserStoreConfig userstore.Config `env:", prefix=USERSTORE_"`
|
UserStoreConfig userstore.Config `env:", prefix=USERSTORE_"`
|
||||||
Port int `env:"PORT, default=8080"`
|
Port int `env:"PORT, default=8080"`
|
||||||
|
SMTPConfig smtpclient.SMTPConfig `env:", prefix=SMTP_"`
|
||||||
|
WebConfig web.WebConfig `env:", prefix=WEB_"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func config() gloableConfig {
|
func config() gloableConfig {
|
||||||
|
@ -44,12 +47,12 @@ func loadTemplates() *template.Template {
|
||||||
return tmpl
|
return tmpl
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupRouter() *gin.Engine {
|
func setupRouter(cfg gloableConfig) *gin.Engine {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.SetHTMLTemplate(loadTemplates())
|
router.SetHTMLTemplate(loadTemplates())
|
||||||
routesWeb := router.Group("/web")
|
routesWeb := router.Group("/web")
|
||||||
|
|
||||||
webServer := web.NewWeb()
|
webServer := web.NewWeb(cfg.WebConfig)
|
||||||
webServer.RegisterRoutes(routesWeb)
|
webServer.RegisterRoutes(routesWeb)
|
||||||
|
|
||||||
router.GET("/", func(c *gin.Context) {})
|
router.GET("/", func(c *gin.Context) {})
|
||||||
|
|
|
@ -9,7 +9,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStartpageEmpty(t *testing.T) {
|
func TestStartpageEmpty(t *testing.T) {
|
||||||
router := setupRouter()
|
gc := gloableConfig{}
|
||||||
|
router := setupRouter(gc)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
req, _ := http.NewRequest("GET", "/", nil)
|
req, _ := http.NewRequest("GET", "/", nil)
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
@ -19,7 +20,8 @@ func TestStartpageEmpty(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPingRoute(t *testing.T) {
|
func TestPingRoute(t *testing.T) {
|
||||||
router := setupRouter()
|
gc := gloableConfig{}
|
||||||
|
router := setupRouter(gc)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
req, _ := http.NewRequest("GET", "/ping", nil)
|
req, _ := http.NewRequest("GET", "/ping", nil)
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
|
@ -13,11 +13,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type SMTPConfig struct {
|
type SMTPConfig struct {
|
||||||
Server string
|
Server string `env:"SERVER"`
|
||||||
Port int
|
Port int `env:"PORT, default=587"`
|
||||||
Username string
|
Username string `env:"USERNAME"`
|
||||||
Password string
|
Password string `env:"PASSWORD"`
|
||||||
From string
|
From string `env:"FROM"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SMTPClient struct {
|
type SMTPClient struct {
|
||||||
|
|
|
@ -6,10 +6,17 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Web struct{}
|
type WebConfig struct {
|
||||||
|
PublicRegistration bool `env:"PUBLIC_REGISTRATION, default=0"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewWeb() Web {
|
type Web struct {
|
||||||
|
config WebConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWeb(config WebConfig) Web {
|
||||||
w := Web{}
|
w := Web{}
|
||||||
|
w.config = config
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,5 +26,9 @@ func (w Web) RegisterRoutes(routing *gin.RouterGroup) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w Web) GetRegisterPage(c *gin.Context) {
|
func (w Web) GetRegisterPage(c *gin.Context) {
|
||||||
|
if !w.config.PublicRegistration {
|
||||||
|
c.HTML(403, "msg.html", gin.H{"msg": "Public registration disabled"})
|
||||||
|
return
|
||||||
|
}
|
||||||
c.HTML(http.StatusOK, "register.html", nil)
|
c.HTML(http.StatusOK, "register.html", nil)
|
||||||
}
|
}
|
||||||
|
|
39
pkg/web/web_test.go
Normal file
39
pkg/web/web_test.go
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRegistrationDisabled(t *testing.T) {
|
||||||
|
webConfig := WebConfig{}
|
||||||
|
webConfig.PublicRegistration = false
|
||||||
|
|
||||||
|
web := NewWeb(webConfig)
|
||||||
|
|
||||||
|
router := gin.New()
|
||||||
|
loadTemplates(router)
|
||||||
|
grpup := router.Group("/web")
|
||||||
|
web.RegisterRoutes(grpup)
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("GET", "/web/register", nil)
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
assert.Equal(t, 403, w.Code)
|
||||||
|
content := string(w.Body.Bytes())
|
||||||
|
assert.Contains(t, content, "Public registration disabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadTemplates(r *gin.Engine) {
|
||||||
|
tmpl, err := template.ParseGlob("../../templates/*.html")
|
||||||
|
if err != nil {
|
||||||
|
panic(err) // Handle Fehler entsprechend
|
||||||
|
}
|
||||||
|
r.SetHTMLTemplate(tmpl)
|
||||||
|
}
|
17
templates/msg.html
Normal file
17
templates/msg.html
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Message</title>
|
||||||
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
|
</head>
|
||||||
|
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
|
||||||
|
<div class="bg-white p-8 shadow-xl w-full max-w-md">
|
||||||
|
<h2 class="text-2xl font-semibold text-gray-900 text-center mb-6">Message</h2>
|
||||||
|
<p>{{ .msg }}</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
Loading…
Add table
Reference in a new issue