diff --git a/cmd/config.toml b/cmd/config.toml index 504d344..aeb79af 100644 --- a/cmd/config.toml +++ b/cmd/config.toml @@ -1,3 +1,8 @@ +[server] +address = "127.0.0.1:8080" +read_timeout = 10 +write_timeout = 10 + [database] driver = "sqlite3" url = "gopay.db" diff --git a/cmd/main.go b/cmd/main.go index 73229d5..8098453 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,10 +2,13 @@ package main import ( _ "embed" + "net/http" + "time" "git.supernets.org/perp/gopay/internal/config" "git.supernets.org/perp/gopay/internal/context" "git.supernets.org/perp/gopay/internal/router" + "github.com/rs/zerolog/log" ) //go:embed config.toml @@ -15,9 +18,25 @@ var content []byte const version = "0.0.0" func main() { + // Parse config cfg := config.Parse(content) config.SetupLogger(cfg) + // Create context & router ctx := context.New(cfg) router.New(ctx) + + // Create server + server := &http.Server{ + Handler: ctx.Engine, + Addr: cfg.Server.Address, + ReadTimeout: time.Second * time.Duration(cfg.Server.ReadTimeout), + WriteTimeout: time.Second * time.Duration(cfg.Server.WriteTimeout), + } + + // Start server + err := server.ListenAndServe() + if err != nil { + log.Panic().Msg(err.Error()) + } } diff --git a/internal/config/config.go b/internal/config/config.go index 8365715..29afe4a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,10 +2,18 @@ package config // Configuration type Config struct { + Server *Server Database *Database Log *Log } +// Server configuration +type Server struct { + Address string `toml:"address"` + ReadTimeout int `toml:"read_timeout"` + WriteTimeout int `toml:"write_timeout"` +} + // Database configuration type Database struct { Driver string `toml:"driver"`