use getenv function

This commit is contained in:
Ruidy 2024-04-13 19:04:57 +02:00
parent 65423c176a
commit efccace761
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 38 additions and 32 deletions

View file

@ -10,10 +10,10 @@ import (
"strconv" "strconv"
"github.com/labstack/gommon/log" "github.com/labstack/gommon/log"
u "github.com/rjNemo/underscore"
"github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/config"
"github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/booking"
u "github.com/rjNemo/underscore"
) )
type PdfService struct { type PdfService struct {
@ -24,11 +24,7 @@ type PdfService struct {
apiKey string apiKey string
} }
func NewPdfService() (*PdfService, error) { func NewPdfService(pid, rid, url, key string) (*PdfService, error) {
pid := os.Getenv("HTMLDOCS_PROJECT_ID")
rid := os.Getenv("HTMLDOCS_REPORT_PROJECT_ID")
url := os.Getenv("HTMLDOCS_URL")
key := os.Getenv("HTMLDOCS_KEY")
if pid == "" || rid == "" || url == "" || key == "" { if pid == "" || rid == "" || url == "" || key == "" {
return nil, errors.New("error building Pdf service. Verify your env variables") return nil, errors.New("error building Pdf service. Verify your env variables")
} }

View file

@ -32,6 +32,7 @@ type Server struct {
type options struct { type options struct {
port *int port *int
fs *embed.FS
} }
type Option func(*options) error type Option func(*options) error
@ -46,7 +47,14 @@ func WithPort(port int) Option {
} }
} }
func New(fs *embed.FS, bs *booking.Service, ps *pdf.PdfService, hc *config.Host, opts ...Option) (*Server, error) { func WithFileSystem(fs embed.FS) Option {
return func(o *options) error {
o.fs = &fs
return nil
}
}
func New(bs *booking.Service, ps *pdf.PdfService, hc *config.Host, opts ...Option) (*Server, error) {
option := new(options) option := new(options)
for _, opt := range opts { for _, opt := range opts {
err := opt(option) err := opt(option)
@ -56,7 +64,7 @@ func New(fs *embed.FS, bs *booking.Service, ps *pdf.PdfService, hc *config.Host,
} }
s := &Server{ s := &Server{
Router: NewRouter(*fs), Router: NewRouter(*option.fs),
bs: bs, bs: bs,
ps: ps, ps: ps,
hc: hc, hc: hc,

50
main.go
View file

@ -1,10 +1,11 @@
package main package main
import ( import (
"context"
"embed" "embed"
"flag" "fmt"
"log"
"os" "os"
"os/signal"
"strconv" "strconv"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
@ -20,53 +21,54 @@ import (
//go:embed assets //go:embed assets
var static embed.FS var static embed.FS
var envFile = flag.String("env", ".env", "env file name")
func init() { func main() {
flag.Parse() ctx := context.Background()
if err := run(ctx, os.Getenv); err != nil {
if os.Getenv("ENV") != "PROD" { fmt.Fprintf(os.Stderr, "%s\n", err)
log.Printf("Loading env file: %q", *envFile) os.Exit(1)
err := godotenv.Load(*envFile)
if err != nil {
log.Fatalln("Error loading .env file")
}
} }
} }
func main() { func run(ctx context.Context, getEnv func(string) string) error {
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
godotenv.Load()
if err := sentry.Init(sentry.ClientOptions{ if err := sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_DSN"), Dsn: getEnv("SENTRY_DSN"),
EnableTracing: true, EnableTracing: true,
TracesSampleRate: 1.0, TracesSampleRate: 1.0,
ProfilesSampleRate: 1.0, ProfilesSampleRate: 1.0,
}); err != nil { }); err != nil {
log.Fatalf("error initializing sentry: %s", err) return fmt.Errorf("error initializing sentry: %s", err)
} }
db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{}) db, err := gorm.Open(postgres.Open(getEnv("DATABASE_URL")), &gorm.Config{})
if err != nil { if err != nil {
log.Fatalf("error connecting to the database %s\n", err) return fmt.Errorf("error connecting to the database %s\n", err)
} }
err = db.AutoMigrate(&booking.Booking{}, &booking.BookingRequest{}, &booking.Item{}) err = db.AutoMigrate(&booking.Booking{}, &booking.BookingRequest{}, &booking.Item{})
if err != nil { if err != nil {
log.Fatalf("error migrating the database %s\n", err) return fmt.Errorf("error migrating the database %s\n", err)
} }
ps, err := pdf.NewPdfService() ps, err := pdf.NewPdfService(getEnv("HTMLDOCS_PROJECT_ID"), getEnv("HTMLDOCS_REPORT_PROJECT_ID"), getEnv("HTMLDOCS_URL"), getEnv("HTMLDOCS_KEY"))
if err != nil { if err != nil {
log.Fatal(err) return fmt.Errorf("error starting pdf service %s\n", err)
} }
p := os.Getenv("PORT") p := getEnv("PORT")
port, err := strconv.Atoi(p) port, err := strconv.Atoi(p)
if err != nil { if err != nil {
log.Fatal(err) return fmt.Errorf("error parsing PORT env %s\n", err)
} }
srv, err := server.New(&static, booking.NewService(db), ps, config.NewHost(), server.WithPort(port)) srv, err := server.New(booking.NewService(db), ps, config.NewHost(), server.WithPort(port), server.WithFileSystem(static))
if err != nil { if err != nil {
log.Fatal(err) return fmt.Errorf("error starting server %s\n", err)
} }
srv.Start() srv.Start()
return nil
} }