This commit is contained in:
2023-07-05 22:07:10 +02:00
parent 0ba69b5496
commit b2e65a9df0
15 changed files with 645 additions and 0 deletions

60
cmd/http/main.go Normal file
View File

@@ -0,0 +1,60 @@
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"gitea.urkob.com/urko/prosody-password/internal/api"
"gitea.urkob.com/urko/prosody-password/internal/services/prosody"
"gitea.urkob.com/urko/prosody-password/kit/config"
)
func main() {
envFile := ""
if os.Getenv("PROSODY_ENV") != "prod" {
envFile = ".env"
}
cfg := config.NewConfig(envFile)
ctx, cancel := context.WithCancel(signalContext(context.Background()))
defer cancel()
restServer := api.NewRestServer(prosody.NewProsody(cfg.Domain))
go func() {
if err := restServer.Start(cfg.ApiPort, cfg.Views); err != nil {
panic(fmt.Errorf("restServer.Start: %w", err))
}
}()
<-ctx.Done()
log.Println("on shutdown")
if restServer != nil {
restServer.Shutdown()
}
log.Println("gracefully shutdown")
}
func signalContext(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
log.Println("listening for shutdown signal")
<-sigs
log.Println("shutdown signal received")
signal.Stop(sigs)
close(sigs)
cancel()
}()
return ctx
}

24
cmd/http/views/error.hbs Normal file
View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
{{> styles}}
</head>
<body>
<div class="container">
<div class="form-container" style="text-align: center;">
<h2>Error</h2>
<p style="color: #D32F2F; font-size: 18px;">Unexpected error: {{message}}</p>
<a href="/"
style="display: inline-block; margin-top: 20px; padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 4px;">Go
Back</a>
</div>
</div>
</body>
</html>

37
cmd/http/views/index.hbs Normal file
View File

@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change XMPP Password</title>
{{> styles}}
</head>
<body>
<div class="container">
<div class="form-container">
<h2>Change Password</h2>
<form action="/changePassword" method="post">
<div class="form-field">
<label for="user">User: (without domain)</label>
<input type="text" id="user" name="user" required>
</div>
<div class="form-field">
<label for="current_password">Current Password:</label>
<input type="password" id="current_password" name="current_password" required>
</div>
<div class="form-field">
<label for="new_password">New Password:</label>
<input type="password" id="new_password" name="new_password" required>
</div>
<div class="form-field">
<input type="submit" value="Change Password">
</div>
</form>
</div>
</div>
</body>
</html>

63
cmd/http/views/styles.hbs Normal file
View File

@@ -0,0 +1,63 @@
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
}
.container {
max-width: 500px;
margin: 0 auto;
padding: 20px;
}
.form-container {
background-color: #ffffff;
border-radius: 8px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.form-container h2 {
margin: 0;
padding-bottom: 20px;
font-size: 24px;
color: #333333;
}
.form-field {
margin-bottom: 20px;
}
.form-field label {
display: block;
margin-bottom: 8px;
font-weight: bold;
color: #555555;
}
.form-field input {
width: 100%;
padding: 10px;
border: 1px solid #cccccc;
border-radius: 4px;
font-size: 16px;
}
.form-field input[type="submit"] {
background-color: #4CAF50;
color: white;
cursor: pointer;
}
.form-field input[type="submit"]:hover {
background-color: #45a049;
}
@media screen and (max-width: 768px) {
.container {
padding: 10px;
}
}
</style>

View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Success</title>
{{> styles}}
</head>
<body>
<div class="container">
<div class="form-container" style="text-align: center;">
<h2>Success</h2>
<p style="color: #388E3C; font-size: 18px;">Password changed successfully!</p>
<a href="/" style="display: inline-block; margin-top: 20px; padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 4px;">Return Home</a>
</div>
</div>
</body>
</html>