udemy-go-web-2/cmd/micro/invoice/invoice.go

80 lines
1.6 KiB
Go
Raw Normal View History

2024-08-26 20:49:10 +00:00
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
)
const (
version = "1.0.0"
)
type config struct {
port int
smtp struct {
host string
port int
username string
password string
}
frontend string
}
type application struct {
config config
infoLog *log.Logger
errorLog *log.Logger
version string
}
func (app *application) serve() error {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.config.port),
Handler: app.routes(),
IdleTimeout: 30 * time.Second,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
app.infoLog.Printf(
"Starting invoice microservice on port %d",
app.config.port,
)
return srv.ListenAndServe()
}
func main() {
var cfg config
flag.IntVar(&cfg.port, "port", 5000, "Server port to listen on")
flag.StringVar(&cfg.smtp.host, "smtphost", "0.0.0.0", "smtp host")
flag.IntVar(&cfg.smtp.port, "smtpport", 1025, "smtp host")
flag.StringVar(&cfg.smtp.username, "smtpuser", "user", "smtp user")
flag.StringVar(&cfg.smtp.password, "smtppwd", "password", "smtp password")
flag.StringVar(&cfg.frontend, "frontend", "http://localhost:4000", "frontend address")
flag.Parse()
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
app := &application{
version: version,
config: cfg,
infoLog: infoLog,
errorLog: errorLog,
}
app.CreateDirIfNotExist("./invoices")
err := app.serve()
if err != nil {
log.Fatal(err)
}
}