Skip to content

Commit

Permalink
yaml config file
Browse files Browse the repository at this point in the history
  • Loading branch information
LobbyLobster committed Nov 9, 2023
1 parent 73c74c0 commit 5d488a2
Show file tree
Hide file tree
Showing 13 changed files with 606 additions and 50 deletions.
14 changes: 8 additions & 6 deletions api/router.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package api

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

"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")
var log = logger.Log
var port string

func InitServer() {
internal.SetupLogger()
port = config.Conf.Port
logger.SetupLogger()
err := internal.InitDb()
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -47,9 +49,9 @@ func InitServer() {
router.POST("/api/search", controller.HandleSearch)
router.POST("/auth/login", controller.Login)

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

if err := router.Run(":" + *port); err != nil {
if err := router.Run(":" + port); err != nil {
log.Fatal(err)
}
}
17 changes: 17 additions & 0 deletions config/cconfig.sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: en
port: 8000
dataabaseInfo:
adminName: admin
adminPass: admin
databasePath: ./db.db
sourceAndDestination:
SourceServer: imap.example.com
SourceMail: "@example.com"
DestinationServer: imap.example.com
DestinationMail: "@example.com"
email:
SMTPHost: smtp.example.com
SMTPPort: 587
SMTPFrom: example
SMTPUser: [email protected]
SMTPPassword: password
60 changes: 60 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package config

import (
"flag"
"imap-sync/logger"
"os"

"github.com/spf13/viper"
)

var log = logger.Log

type Config struct {
Language string
Port string

DatabaseInfo struct {
AdminName string
AdminPass string
DatabasePath string
}

SourceAndDestination struct {
SourceServer string
SourceMail string
DestinationServer string
DestinationMail string
}

Email struct {
SMTPHost string
SMTPPort string
SMTPFrom string
SMTPUser string
SMTPPassword string
}
}

var Conf Config

func ParseConfig() {
filePath := flag.String("config", "/etc/monomail-sync.yml", "Path of the configuration file in YAML format")
flag.Parse()

if _, err := os.Stat(*filePath); os.IsNotExist(err) {
log.Fatalf("Configuration file: %s does not exist, %v\n", *filePath, err)
}

viper.SetConfigFile(*filePath)
viper.SetConfigType("yaml")

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s\n", err)
}

err := viper.Unmarshal(&Conf)
if err != nil {
log.Fatalf("Unable to decode into struct, %v\n", err)
}
}
30 changes: 16 additions & 14 deletions controller/root_controller.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
package controller

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

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

var log = internal.Log
var log = logger.Log

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")
source_server string
source_account string
destination_server string
destination_account string
)

func HandleRoot(ctx *gin.Context) {
source_server = config.Conf.SourceAndDestination.SourceServer
source_account = config.Conf.SourceAndDestination.SourceMail
destination_server = config.Conf.SourceAndDestination.DestinationServer
destination_account = config.Conf.SourceAndDestination.DestinationMail

sourceDetails := internal.Credentials{
Server: *source_server,
Account: *source_account,
Password: *source_password,
Server: source_server,
Account: source_account,
}

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

data := struct {
Expand Down
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,24 @@ require (
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-session/session v3.1.2+incompatible // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/smartystreets/goconvey v1.8.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

require (
Expand All @@ -39,6 +54,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/spf13/viper v1.17.0
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.5.0 // indirect
Expand Down
Loading

0 comments on commit 5d488a2

Please sign in to comment.