Skip to content

Commit

Permalink
Refactor to use clean architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
xeome committed Oct 10, 2023
1 parent 05930e0 commit 3c53999
Show file tree
Hide file tree
Showing 25 changed files with 383 additions and 259 deletions.
52 changes: 52 additions & 0 deletions api/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package api

import (
"flag"
"imap-sync/controller"
"imap-sync/internal"

"github.com/gin-gonic/gin"
ginsession "github.com/go-session/gin-session"
)

var log = internal.Log
var port = flag.String("port", "8080", "Port to listen on")

func InitServer() {

err := controller.InitDb()
if err != nil {
log.Error(err)
}

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

router.Use(ginsession.New())

router.LoadHTMLGlob("templates/*")

router.Static("/static", "./static/")

router.GET("/", controller.HandleRoot)
router.GET("/admin", controller.HandleAdmin)
router.GET("/favicon.ico", func(ctx *gin.Context) {
ctx.File("favicon.ico")
})
router.GET("/login", controller.HandleLogin)

go internal.InitQueue()
// API endpoints
router.GET("/api/queue", controller.HandleQueue)
router.GET("/api/queuepoll", controller.HandleQueuePolling)
router.GET("/api/pagination", controller.HandlePagination)
router.POST("/api/validate", controller.HandleValidate)
router.POST("/api/search", controller.HandleSearch)
router.POST("/auth/login", controller.Login)

log.Info("Server starting on http://localhost:" + *port)

if err := router.Run(":" + *port); err != nil {
log.Fatal(err)
}
}
4 changes: 2 additions & 2 deletions internal/admin.go → controller/admin_controller.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package controller

import (
"net/http"
Expand All @@ -7,7 +7,7 @@ import (
ginsession "github.com/go-session/gin-session"
)

func handleAdmin(ctx *gin.Context) {
func HandleAdmin(ctx *gin.Context) {
store := ginsession.FromContext(ctx)
_, ok := store.Get("user")
if !ok {
Expand Down
11 changes: 10 additions & 1 deletion internal/db.go → controller/db.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package internal
package controller

import (
"database/sql"
"errors"
"flag"
"fmt"
"imap-sync/internal"
"reflect"

_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/bcrypt"
)

var log = internal.Log

var (
admin_name = flag.String("admin_name", "admin", "Admin username")
admin_pass = flag.String("admin_pass", "admin", "Admin password")
)

var db *sql.DB

func InitDb() error {
Expand Down
16 changes: 8 additions & 8 deletions internal/auth.go → controller/login_controller.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package controller

import (
"net/http"
Expand All @@ -8,12 +8,7 @@ import (
"golang.org/x/crypto/bcrypt"
)

type user struct {
Name string `json:"username"`
Password string `json:"password"`
}

func handleLogin(ctx *gin.Context) {
func HandleLogin(ctx *gin.Context) {
store := ginsession.FromContext(ctx)
_, ok := store.Get("user")
if ok {
Expand All @@ -24,7 +19,12 @@ func handleLogin(ctx *gin.Context) {
ctx.HTML(200, "login.html", nil)
}

func login(ctx *gin.Context) {
type user struct {
Name string `json:"username"`
Password string `json:"password"`
}

func Login(ctx *gin.Context) {
var data user
data.Name = ctx.PostForm("username")
data.Password = ctx.PostForm("password")
Expand Down
15 changes: 15 additions & 0 deletions controller/pagination_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package controller

import (
"imap-sync/internal"
"strconv"

"github.com/gin-gonic/gin"
)

func HandlePagination(ctx *gin.Context) {
index, _ := strconv.Atoi(ctx.Request.FormValue("page"))
pages := internal.GetPagination(index)

ctx.HTML(200, "pagination.html", pages)
}
16 changes: 16 additions & 0 deletions controller/queue_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package controller

import (
"imap-sync/internal"
"strconv"

"github.com/gin-gonic/gin"
)

func HandleQueue(ctx *gin.Context) {
index, _ := strconv.Atoi(ctx.Request.FormValue("page"))

data := internal.GetQueueData(index)

ctx.HTML(200, "tbody.html", data)
}
16 changes: 16 additions & 0 deletions controller/queue_polling_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package controller

import (
"imap-sync/internal"
"strconv"

"github.com/gin-gonic/gin"
)

func HandleQueuePolling(ctx *gin.Context) {
index, _ := strconv.Atoi(ctx.Request.FormValue("page"))

data := internal.GetPollingData(index)

ctx.HTML(200, "table.html", data)
}
40 changes: 40 additions & 0 deletions controller/root_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package controller

import (
"flag"
"imap-sync/internal"

"github.com/gin-gonic/gin"
)

var (
source_server = flag.String("source_server", "", "Source server")
source_account = flag.String("source_account", "", "Source account")
source_password = flag.String("source_password", "", "Source password")
destination_server = flag.String("destination_server", "", "Destination server")
destination_account = flag.String("destination_account", "", "Destination account")
destination_password = flag.String("destination_password", "", "Destination password")
)

func HandleRoot(ctx *gin.Context) {
sourceDetails := internal.Credentials{
Server: *source_server,
Account: *source_account,
Password: *source_password,
}

destinationDetails := internal.Credentials{
Server: *destination_server,
Account: *destination_account,
Password: *destination_password,
}

data := struct {
SourceDetails internal.Credentials
DestinationDetails internal.Credentials
}{
SourceDetails: sourceDetails,
DestinationDetails: destinationDetails,
}
ctx.HTML(200, "index.html", data)
}
20 changes: 20 additions & 0 deletions controller/search_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controller

import (
"imap-sync/internal"

"github.com/gin-gonic/gin"
)

func HandleSearch(ctx *gin.Context) {
searchQuery := ctx.PostForm("search-input")

if searchQuery == "" {
HandleQueue(ctx)
return
}

data := internal.GetSearchData(searchQuery)

ctx.HTML(200, "tbody.html", data)
}
41 changes: 41 additions & 0 deletions controller/sync_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package controller

import (
"imap-sync/internal"

"github.com/gin-gonic/gin"
)

func handleSync(ctx *gin.Context) {
sourceServer := ctx.PostForm("source_server")
sourceAccount := ctx.PostForm("source_account")
sourcePassword := ctx.PostForm("source_password")
destinationServer := ctx.PostForm("destination_server")
destinationAccount := ctx.PostForm("destination_account")
destinationPassword := ctx.PostForm("destination_password")

sourceDetails := internal.Credentials{
Server: sourceServer,
Account: sourceAccount,
Password: sourcePassword,
}

destinationDetails := internal.Credentials{
Server: destinationServer,
Account: destinationAccount,
Password: destinationPassword,
}

// Add to queue
log.Infof("Adding %s to queue", sourceDetails.Account)
internal.AddTask(sourceDetails, destinationDetails)

// log.Infof("Syncing %s to %s", sourceDetails.Account, destinationDetails.Account)

// err := syncIMAP(sourceDetails, destinationDetails)
// if err != nil {
// ctx.HTML(200, "error.html", err.Error())
// return
// }
// ctx.HTML(200, "success.html", "Synced "+sourceDetails.Account+" to "+destinationDetails.Account)
}
48 changes: 48 additions & 0 deletions controller/validation_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package controller

import (
"imap-sync/internal"

"github.com/gin-gonic/gin"
)

func HandleValidate(ctx *gin.Context) {

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

var Server, Account, Password string

if sourceCreds != "" {
Server = ctx.PostForm("source_server")
Account = ctx.PostForm("source_account")
Password = ctx.PostForm("source_password")
}

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

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

creds := internal.Credentials{
Server: Server,
Account: Account,
Password: Password,
}

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

err := internal.ValidateCredentials(creds)
if err != nil {
ctx.HTML(200, "error.html", err.Error())
return
}
ctx.HTML(200, "success.html", creds)
}
21 changes: 0 additions & 21 deletions internal/flags.go

This file was deleted.

11 changes: 6 additions & 5 deletions internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"github.com/sirupsen/logrus"
)

var log = logrus.New()
var Log = logrus.New()
var log = Log

func SetupLogger(logger *logrus.Logger) {
func SetupLogger() {

logger.SetFormatter(&logrus.TextFormatter{
log.SetFormatter(&logrus.TextFormatter{
ForceColors: true, // Enable colors in the console output
FullTimestamp: true, // Show full timestamp with date and time
TimestampFormat: "2006-01-02 15:04:05",
Expand All @@ -24,6 +25,6 @@ func SetupLogger(logger *logrus.Logger) {
},
})

logger.SetReportCaller(true)
logger.SetLevel(logrus.TraceLevel)
log.SetReportCaller(true)
log.SetLevel(logrus.TraceLevel)
}
Loading

0 comments on commit 3c53999

Please sign in to comment.