Added server to config

This commit is contained in:
perp 2024-06-02 14:07:09 +01:00
parent 949d691ef7
commit 0fbf41968b
3 changed files with 32 additions and 0 deletions

View File

@ -1,3 +1,8 @@
[server]
address = "127.0.0.1:8080"
read_timeout = 10
write_timeout = 10
[database]
driver = "sqlite3"
url = "gopay.db"

View File

@ -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())
}
}

View File

@ -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"`