Skip to content

Commit

Permalink
added localization and one click validation
Browse files Browse the repository at this point in the history
  • Loading branch information
LobbyLobster committed Oct 23, 2023
1 parent f294d65 commit ac20b9f
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 82 deletions.
2 changes: 2 additions & 0 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func InitServer() {
log.Error(err)
}

controller.InitLocalizer()

gin.SetMode(gin.ReleaseMode)
router := gin.Default()

Expand Down
2 changes: 1 addition & 1 deletion controller/admin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func HandleAdmin(ctx *gin.Context) {
return
}

ctx.HTML(200, "admin.html", nil)
ctx.HTML(200, "admin.html", Data["admin"])
}
88 changes: 88 additions & 0 deletions controller/localization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package controller

import "flag"

var (
lang = flag.String("lang", "en", "Language")
Data map[string]map[string]string
)

func InitLocalizer() {

switch *lang {
case "en":
Data = map[string]map[string]string{
"index": {
"source_details": "Source Details",
"destination_details": "Destination Details",
"server": "Server",
"account": "Account",
"password": "Password",
"validate": "Validate Credentials",
"sync": "Start Synchronization",
"user_queue": "User Table",
},
"login": {
"sign_in": "Sign In",
"description": "Sign in to access admin panel",
"username": "Username",
"password": "Password",
},
"admin": {
"index": "Index",
"source_server": "Source Server",
"source_account": "Source Account",
"dest_server": "Destination Server",
"dest_account": "Destination Account",
"status": "Status",
"actions": "Actions",
},
"table": {
"index": "Index",
"source_server": "Source Server",
"source_account": "Source Account",
"dest_server": "Destination Server",
"dest_account": "Destination Account",
"status": "Status",
"actions": "Actions",
},
}
case "tr":
Data = map[string]map[string]string{
"index": {
"source_details": "Kaynak Bilgileri",
"destination_details": "Hedef Bilgileri",
"server": "Sunucu",
"account": "Hesap",
"password": "Parola",
"validate": "Bilgileri Doğrula",
"sync": "Senkronizasyonu Başlat",
"user_queue": "Kullanıcı İşlem Kuyruğu",
},
"login": {
"sign_in": "Giriş Yap",
"description": "Admin paneline erişebilmek için giriş yapın.",
"username": "Kullanıcı Adı",
"password": "Parola",
},
"admin": {
"index": "Sıra",
"source_server": "Kaynak Sunucu",
"source_account": "Kaynak Hesap",
"dest_server": "Hedef Sunucu",
"dest_account": "Hedef Hesap",
"status": "Durum",
"actions": "Eylemler",
},
"table": {
"index": "Sıra",
"source_server": "Kaynak Sunucu",
"source_account": "Kaynak Hesap",
"dest_server": "Hedef Sunucu",
"dest_account": "Hedef Hesap",
"status": "Durum",
"actions": "Eylemler",
},
}
}
}
3 changes: 2 additions & 1 deletion controller/login_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func HandleLogin(ctx *gin.Context) {
ctx.Redirect(http.StatusTemporaryRedirect, "/")
return
}
ctx.HTML(200, "login.html", nil)

ctx.HTML(200, "login.html", Data["login"])
}

type user struct {
Expand Down
4 changes: 4 additions & 0 deletions controller/root_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ func HandleRoot(ctx *gin.Context) {
data := struct {
SourceDetails internal.Credentials
DestinationDetails internal.Credentials
Text map[string]string
Table map[string]string
}{
SourceDetails: sourceDetails,
DestinationDetails: destinationDetails,
Text: Data["index"],
Table: Data["table"],
}
ctx.HTML(200, "index.html", data)
}
56 changes: 33 additions & 23 deletions controller/validation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,55 @@ import (

func HandleValidate(ctx *gin.Context) {

sourceCreds := ctx.PostForm("source_creds")
destCreds := ctx.PostForm("destination_creds")
validate := ctx.PostForm("validate")
submitsync := ctx.PostForm("submit_sync")

var Server, Account, Password string
var Source bool
var SServer, SAccount, SPassword string
var DServer, DAccount, DPassword string

if sourceCreds != "" {
Server = ctx.PostForm("source_server")
Account = ctx.PostForm("source_account")
Password = ctx.PostForm("source_password")
Source = true
if validate != "" {
SServer = ctx.PostForm("source_server")
SAccount = ctx.PostForm("source_account")
SPassword = ctx.PostForm("source_password")
DServer = ctx.PostForm("destination_server")
DAccount = ctx.PostForm("destination_account")
DPassword = ctx.PostForm("destination_password")
}

if destCreds != "" {
Server = ctx.PostForm("destination_server")
Account = ctx.PostForm("destination_account")
Password = ctx.PostForm("destination_password")
Source = false
}

if destCreds == "" && sourceCreds == "" && submitsync != "" {
if validate == "" && submitsync != "" {
HandleSync(ctx)
return
}

creds := internal.Credentials{
Server: Server,
Account: Account,
Password: Password,
Source: Source,
Server: SServer,
Account: SAccount,
Password: SPassword,
Source: true,
}

log.Infof("Validating credentials for: %s", creds.Account)

err := internal.ValidateCredentials(creds)
if err != nil {
ctx.HTML(200, "error.html", err.Error())
ctx.HTML(200, "error.html", "Couldn't verify for user: "+SAccount)
return
}
ctx.HTML(200, "success.html", creds)

creds = internal.Credentials{
Server: DServer,
Account: DAccount,
Password: DPassword,
Source: false,
}

log.Infof("Validating credentials for: %s", creds.Account)

err = internal.ValidateCredentials(creds)
if err != nil {
ctx.HTML(200, "error.html", "Couldn't verify for user: "+DAccount)
return
}

ctx.HTML(200, "success.html", nil)
}
26 changes: 26 additions & 0 deletions internal/notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package internal

import (
"net/smtp"
)

func Email(creds Credentials) error {

smtpHost := ""
smtpPort := ""
from := ""
username := ""
password := ""
to := creds.Account
subject := ""
message := ""

auth := smtp.CRAMMD5Auth(username, password)

msg := []byte("From: " + from + "\r\n" +
"To: " + to + "\r\n" +
"Subject: [" + "" + "] " + subject + "\r\n\r\n" +
message + "\r\n")

return smtp.SendMail(smtpHost+":"+smtpPort, auth, from, []string{to}, msg)
}
18 changes: 16 additions & 2 deletions templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>imapsync-web-admin</title>
<title>Monomail-Sync Admin</title>
<link rel="stylesheet" href="/static/css/styles.css" />
<script src="/static/js/tailwind.js"></script>
<script src="/static/js/htmx.min.js"
Expand Down Expand Up @@ -55,7 +55,21 @@ <h2 class="text-4xl m-2">Queue</h2>

<table class="table-hover table-compact table" id="queue-table" hx-get="/api/queue?page=1" hx-trigger="load"
hx-target="#table-body" hx-swap-oob="innerHTML transition:true">
{{template "table.html" .}}
<thead>
<tr>
<th>{{index . "index"}}</th>
<th>{{index . "source_server"}}</th>
<th>{{index . "source_account"}}</th>
<th>{{index . "dest_server"}}</th>
<th>{{index . "dest_account"}}</th>
<th>{{index . "status"}}</th>
<th>{{index . "actions"}}</th>
</tr>
</thead>
<tbody id="table-body" hx-boost="true" hx-get="/api/queue?page={{if .Index}}{{.Index}}{{else}}1{{end}}"
hx-swap="innerHTML transition:true" hx-trigger="every 60s">
{{template "tbody.html" .}}
</tbody>
</table>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/admin_navbar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="navbar bg-backgroundSecondary">
<div class="navbar-start font-semibold">
<a class="navbar-item" href="/">imapsync-web Admin Panel</a>
<a class="navbar-item" href="/">Monomail-Sync Admin Panel</a>
</div>

<div class="navbar-center" x-data="{ searchInput: '' }">
Expand Down
Loading

0 comments on commit ac20b9f

Please sign in to comment.