diff --git a/.air.toml b/.air.toml index a6e0cbb..c2289e9 100644 --- a/.air.toml +++ b/.air.toml @@ -5,7 +5,7 @@ tmp_dir = "tmp" [build] args_bin = [] bin = "./tmp/main" -cmd = "go build -o ./tmp/main ./main.go" +cmd = "go build -o ./tmp/main ./app/main.go" delay = 1000 exclude_dir = ["assets", "tmp", "vendor", "testdata", "build"] exclude_file = [] diff --git a/.gitignore b/.gitignore index fb95978..f083045 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.vscode /__debug* /tmp +/config.yaml diff --git a/app/cfg/cfg.go b/app/cfg/cfg.go new file mode 100644 index 0000000..bf93fb4 --- /dev/null +++ b/app/cfg/cfg.go @@ -0,0 +1,88 @@ +package cfg + +import ( + "fmt" + "os" + + "github.com/kelseyhightower/envconfig" + "github.com/sirupsen/logrus" + "gopkg.in/yaml.v3" +) + +type configStruct struct { + Database struct { + Host string `yaml:"host"` + Port string `yaml:"port"` + Name string `yaml:"name"` + Username string `yaml:"username"` + Password string `yaml:"password" json:"-"` + } `yaml:"database"` + Application struct { + LogLevel string `yaml:"logLevel"` + LogFile string `yaml:"logFile"` + Debug bool `yaml:"debug"` + DebugSQL bool `yaml:"debugSQL"` + DisableSendMail bool `yaml:"disableSendMail"` + IsProduction bool `yaml:"isProduction"` + LoopDelay uint32 `yaml:"loopDelay"` + } `yaml:"application"` + SMTP struct { + Host string `yaml:"host"` + Port int `yaml:"port"` + Username string `yaml:"username"` + Password string `yaml:"password" json:"-"` + StartTLS bool `yaml:"startTLS"` + UseTLS bool `yaml:"useTLS"` + FromEmail string `yaml:"fromEmail"` + } `yaml:"smtp"` +} + +const DEFAULT_CONFIG_FILE = "config.yaml" +const ENV_PREFIX = "MAILSENDER" + +var Config configStruct +var log = logrus.New() + +func processError(err error) { + log.Error("Config file error: " + err.Error()) + os.Exit(2) +} + +func readFile(cfgFile string, cfg *configStruct) { + f, err := os.Open(cfgFile) + if err != nil { + processError(err) + } else { + decoder := yaml.NewDecoder(f) + err = decoder.Decode(cfg) + if err != nil { + processError(err) + } + } + defer f.Close() +} + +func readEnv(cfg *configStruct) { + err := envconfig.Process(ENV_PREFIX, cfg) + if err != nil { + processError(err) + } +} + +func Init() { + cfgFile := os.Getenv("MAILSENDER_CONFIG") + if cfgFile == "" { + cfgFile = DEFAULT_CONFIG_FILE + } + + readFile(cfgFile, &Config) + readEnv(&Config) + + maskedCfg := Config + maskedCfg.Database.Password = "**password hidden**" + maskedCfg.SMTP.Password = "**password hidden**" + + fmt.Println("--- CONFIG -------------------------------") + fmt.Println(maskedCfg) + fmt.Println("------------------------------------------") +} diff --git a/app/db/db.go b/app/db/db.go new file mode 100644 index 0000000..742030a --- /dev/null +++ b/app/db/db.go @@ -0,0 +1,54 @@ +package db + +import ( + "fmt" + "iris-test/app/cfg" + "iris-test/app/logging" + "strconv" + "strings" + "time" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + gormLogger "gorm.io/gorm/logger" +) + +const CONNECTION_MAX_IDLE_TIME = time.Minute * 1 +const DB_CONNECTION_TIMEOUT = 5 + +var DBConn *gorm.DB + +func InitDB() *gorm.DB { + var connectionString = strings.Join([]string{ + "postgres://", + cfg.Config.Database.Username, ":", + cfg.Config.Database.Password, "@", + cfg.Config.Database.Host, ":", + cfg.Config.Database.Port, "/", + cfg.Config.Database.Name, + "?sslmode=disable", + "&TimeZone=UTC", + "&connect_timeout=", strconv.Itoa(DB_CONNECTION_TIMEOUT), + }, "") + + var logLevel = gormLogger.Silent + if cfg.Config.Application.DebugSQL { + logLevel = gormLogger.Info + } + + var err error + DBConn, err = gorm.Open(postgres.Open(connectionString), &gorm.Config{ + Logger: gormLogger.Default.LogMode(logLevel), + }) + if err != nil { + msg := fmt.Sprintf("Error connecting to database: %s. Terminating!", err) + logging.Error(msg) + panic(msg) + } + + // set connection autodisconnect after idle time + db, _ := DBConn.DB() + db.SetConnMaxIdleTime(CONNECTION_MAX_IDLE_TIME) + + return DBConn +} diff --git a/app/logging/logging.go b/app/logging/logging.go new file mode 100644 index 0000000..b09cb43 --- /dev/null +++ b/app/logging/logging.go @@ -0,0 +1,65 @@ +package logging + +import ( + "encoding/json" + "fmt" + "io" + "iris-test/app/cfg" + "os" + + "github.com/sirupsen/logrus" +) + +var Log = logrus.New() + +func Debug(message string) { + Log.Debug(message) +} + +func Info(message string) { + Log.Info(message) +} + +func Error(message string) { + Log.Error(message) +} + +func Warn(message string) { + Log.Warn(message) +} + +func Init() { + logLevel, err := logrus.ParseLevel(cfg.Config.Application.LogLevel) + if err != nil { + panic(fmt.Sprintf("Invalid configured logLevel: %s\n", cfg.Config.Application.LogLevel)) + } + + Log.SetLevel(logLevel) + + Log.SetFormatter(&logrus.TextFormatter{ + FullTimestamp: true, + TimestampFormat: "2006-01-02 15:04:05", + PadLevelText: true, + DisableQuote: true, + }) + + LogFile := cfg.Config.Application.LogFile + file, err := os.OpenFile( + LogFile, + os.O_CREATE|os.O_WRONLY|os.O_APPEND, + 0655, + ) + if err != nil { + msg := fmt.Sprintf("Failed to log to file %s: %s", cfg.Config.Application.LogFile, err) + Log.Warning(msg) + panic(msg) + } + + mw := io.MultiWriter(os.Stdout, file) + Log.SetOutput(mw) + + configJson, err := json.Marshal(cfg.Config) + if err == nil { + Info(fmt.Sprintf("Using config: %s", configJson)) + } +} diff --git a/main.go b/app/main.go similarity index 95% rename from main.go rename to app/main.go index ddf0c22..00bfde9 100644 --- a/main.go +++ b/app/main.go @@ -36,7 +36,7 @@ var users = []User{ func main() { app := iris.New() - app.RegisterView(iris.Jet("./views", ".jet").Reload(true)) + app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true)) // mypathRoute := app.Get("/mypath", writePathHandler) // mypathRoute.Name = "my-page1" @@ -65,7 +65,7 @@ func main() { ctx.ViewData("params1", params1) ctx.ViewData("users", users) - if err := ctx.View("index.jet"); err != nil { + if err := ctx.View("pages/index.jet"); err != nil { ctx.HTML("

%s

", err.Error()) return } diff --git a/views/base.jet b/app/templates/base/base.jet similarity index 100% rename from views/base.jet rename to app/templates/base/base.jet diff --git a/views/table_component.jet b/app/templates/components/table_component.jet similarity index 100% rename from views/table_component.jet rename to app/templates/components/table_component.jet diff --git a/views/index.jet b/app/templates/pages/index.jet similarity index 75% rename from views/index.jet rename to app/templates/pages/index.jet index d325de6..f61217c 100644 --- a/views/index.jet +++ b/app/templates/pages/index.jet @@ -1,5 +1,5 @@ -{{ extends "./base.jet" }} -{{ import "./table_component.jet" }} +{{ extends "/base/base.jet" }} +{{ import "/components/table_component.jet" }} {{ block mainContent() }} diff --git a/go.mod b/go.mod index 265aabe..1bc134d 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,14 @@ module iris-test go 1.21.2 -require github.com/kataras/iris/v12 v12.2.7 +require ( + github.com/kataras/iris/v12 v12.2.7 + github.com/kelseyhightower/envconfig v1.4.0 + github.com/sirupsen/logrus v1.9.3 + gopkg.in/yaml.v3 v3.0.1 + gorm.io/driver/postgres v1.5.3 + gorm.io/gorm v1.25.5 +) require ( github.com/BurntSushi/toml v1.3.2 // indirect @@ -19,6 +26,11 @@ require ( github.com/google/uuid v1.3.1 // indirect github.com/gorilla/css v1.0.0 // indirect github.com/iris-contrib/schema v0.0.6 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgx/v5 v5.4.3 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/kataras/blocks v0.0.8 // indirect github.com/kataras/golog v0.1.9 // indirect @@ -29,10 +41,10 @@ require ( github.com/mailgun/raymond/v2 v2.0.48 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/microcosm-cc/bluemonday v1.0.26 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/schollz/closestmatch v2.1.0+incompatible // indirect github.com/sergi/go-diff v1.1.0 // indirect - github.com/sirupsen/logrus v1.9.3 // indirect github.com/tdewolff/minify/v2 v2.19.10 // indirect github.com/tdewolff/parse/v2 v2.6.8 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect @@ -47,5 +59,4 @@ require ( golang.org/x/time v0.3.0 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index b1ab7d1..67f5d1e 100644 --- a/go.sum +++ b/go.sum @@ -49,6 +49,16 @@ github.com/iris-contrib/httpexpect/v2 v2.15.2 h1:T9THsdP1woyAqKHwjkEsbCnMefsAFvk github.com/iris-contrib/httpexpect/v2 v2.15.2/go.mod h1:JLDgIqnFy5loDSUv1OA2j0mb6p/rDhiCqigP22Uq9xE= github.com/iris-contrib/schema v0.0.6 h1:CPSBLyx2e91H2yJzPuhGuifVRnZBBJ3pCOMbOvPZaTw= github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm65f3wX8J5iA= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY= +github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/kataras/blocks v0.0.8 h1:MrpVhoFTCR2v1iOOfGng5VJSILKeZZI+7NGfxEh3SUM= @@ -63,9 +73,13 @@ github.com/kataras/sitemap v0.0.6 h1:w71CRMMKYMJh6LR2wTgnk5hSgjVNB9KL60n5e2KHvLY github.com/kataras/sitemap v0.0.6/go.mod h1:dW4dOCNs896OR1HmG+dMLdT7JjDk7mYBzoIRwuj5jA4= github.com/kataras/tunnel v0.0.4 h1:sCAqWuJV7nPzGrlb0os3j49lk2JhILT0rID38NHNLpA= github.com/kataras/tunnel v0.0.4/go.mod h1:9FkU4LaeifdMWqZu7o20ojmW4B7hdhv2CMLwfnHGpYw= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4= github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -81,10 +95,11 @@ github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3r github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo= @@ -98,6 +113,7 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= @@ -173,8 +189,9 @@ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= @@ -184,5 +201,9 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.5.3 h1:qKGY5CPHOuj47K/VxbCXJfFvIUeqMSXXadqdCY+MbBU= +gorm.io/driver/postgres v1.5.3/go.mod h1:F+LtvlFhZT7UBiA81mC9W6Su3D4WUhSboc/36QZU0gk= +gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= +gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= moul.io/http2curl/v2 v2.3.0 h1:9r3JfDzWPcbIklMOs2TnIFzDYvfAZvjeavG6EzP7jYs= moul.io/http2curl/v2 v2.3.0/go.mod h1:RW4hyBjTWSYDOxapodpNEtX0g5Eb16sxklBqmd2RHcE=