mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
use getenv function
This commit is contained in:
parent
65423c176a
commit
efccace761
3 changed files with 38 additions and 32 deletions
|
|
@ -10,10 +10,10 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/labstack/gommon/log"
|
||||
u "github.com/rjNemo/underscore"
|
||||
|
||||
"github.com/rjNemo/rentease/config"
|
||||
"github.com/rjNemo/rentease/internal/booking"
|
||||
u "github.com/rjNemo/underscore"
|
||||
)
|
||||
|
||||
type PdfService struct {
|
||||
|
|
@ -24,11 +24,7 @@ type PdfService struct {
|
|||
apiKey string
|
||||
}
|
||||
|
||||
func NewPdfService() (*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")
|
||||
func NewPdfService(pid, rid, url, key string) (*PdfService, error) {
|
||||
if pid == "" || rid == "" || url == "" || key == "" {
|
||||
return nil, errors.New("error building Pdf service. Verify your env variables")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ type Server struct {
|
|||
|
||||
type options struct {
|
||||
port *int
|
||||
fs *embed.FS
|
||||
}
|
||||
|
||||
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)
|
||||
for _, opt := range opts {
|
||||
err := opt(option)
|
||||
|
|
@ -56,7 +64,7 @@ func New(fs *embed.FS, bs *booking.Service, ps *pdf.PdfService, hc *config.Host,
|
|||
}
|
||||
|
||||
s := &Server{
|
||||
Router: NewRouter(*fs),
|
||||
Router: NewRouter(*option.fs),
|
||||
bs: bs,
|
||||
ps: ps,
|
||||
hc: hc,
|
||||
|
|
|
|||
50
main.go
50
main.go
|
|
@ -1,10 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"flag"
|
||||
"log"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
|
|
@ -20,53 +21,54 @@ import (
|
|||
|
||||
//go:embed assets
|
||||
var static embed.FS
|
||||
var envFile = flag.String("env", ".env", "env file name")
|
||||
|
||||
func init() {
|
||||
flag.Parse()
|
||||
|
||||
if os.Getenv("ENV") != "PROD" {
|
||||
log.Printf("Loading env file: %q", *envFile)
|
||||
err := godotenv.Load(*envFile)
|
||||
if err != nil {
|
||||
log.Fatalln("Error loading .env file")
|
||||
}
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
if err := run(ctx, os.Getenv); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
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{
|
||||
Dsn: os.Getenv("SENTRY_DSN"),
|
||||
Dsn: getEnv("SENTRY_DSN"),
|
||||
EnableTracing: true,
|
||||
TracesSampleRate: 1.0,
|
||||
ProfilesSampleRate: 1.0,
|
||||
}); 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 {
|
||||
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{})
|
||||
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 {
|
||||
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)
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
return fmt.Errorf("error starting server %s\n", err)
|
||||
}
|
||||
srv.Start()
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue