add globale config

This commit is contained in:
kekskurse 2025-03-14 01:49:49 +01:00
parent 8be8077d94
commit 6962d40d78
7 changed files with 92 additions and 14 deletions

View file

@ -7,3 +7,9 @@
|------|-------------|---------|
| USERSTORE_SQLITE_PATH | Path to the Sqlite Database | `none` |
| 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
View file

@ -6,6 +6,7 @@ import (
"fmt"
"html/template"
"git.keks.cloud/kekskurse/miniauth/pkg/smtpclient"
"git.keks.cloud/kekskurse/miniauth/pkg/userstore"
"git.keks.cloud/kekskurse/miniauth/pkg/web"
"github.com/gin-gonic/gin"
@ -18,13 +19,15 @@ var templatesFS embed.FS
func main() {
cnf := config()
router := setupRouter()
router := setupRouter(cnf)
router.Run(fmt.Sprintf(":%v", cnf.Port))
}
type gloableConfig struct {
UserStoreConfig userstore.Config `env:", prefix=USERSTORE_"`
Port int `env:"PORT, default=8080"`
UserStoreConfig userstore.Config `env:", prefix=USERSTORE_"`
Port int `env:"PORT, default=8080"`
SMTPConfig smtpclient.SMTPConfig `env:", prefix=SMTP_"`
WebConfig web.WebConfig `env:", prefix=WEB_"`
}
func config() gloableConfig {
@ -44,12 +47,12 @@ func loadTemplates() *template.Template {
return tmpl
}
func setupRouter() *gin.Engine {
func setupRouter(cfg gloableConfig) *gin.Engine {
router := gin.Default()
router.SetHTMLTemplate(loadTemplates())
routesWeb := router.Group("/web")
webServer := web.NewWeb()
webServer := web.NewWeb(cfg.WebConfig)
webServer.RegisterRoutes(routesWeb)
router.GET("/", func(c *gin.Context) {})

View file

@ -9,7 +9,8 @@ import (
)
func TestStartpageEmpty(t *testing.T) {
router := setupRouter()
gc := gloableConfig{}
router := setupRouter(gc)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.ServeHTTP(w, req)
@ -19,7 +20,8 @@ func TestStartpageEmpty(t *testing.T) {
}
func TestPingRoute(t *testing.T) {
router := setupRouter()
gc := gloableConfig{}
router := setupRouter(gc)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping", nil)
router.ServeHTTP(w, req)

View file

@ -13,11 +13,11 @@ import (
)
type SMTPConfig struct {
Server string
Port int
Username string
Password string
From string
Server string `env:"SERVER"`
Port int `env:"PORT, default=587"`
Username string `env:"USERNAME"`
Password string `env:"PASSWORD"`
From string `env:"FROM"`
}
type SMTPClient struct {

View file

@ -6,10 +6,17 @@ import (
"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.config = config
return w
}
@ -19,5 +26,9 @@ func (w Web) RegisterRoutes(routing *gin.RouterGroup) error {
}
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)
}

39
pkg/web/web_test.go Normal file
View 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
View 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>