Reorganized directories and packages.

This commit is contained in:
mikestefanello 2022-11-02 19:23:26 -04:00
parent 965fb540c7
commit dceb232cb2
61 changed files with 83 additions and 83 deletions

71
cmd/web/main.go Normal file
View file

@ -0,0 +1,71 @@
package main
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/mikestefanello/pagoda/pkg/routes"
"github.com/mikestefanello/pagoda/pkg/services"
)
func main() {
// Start a new container
c := services.NewContainer()
defer func() {
if err := c.Shutdown(); err != nil {
c.Web.Logger.Fatal(err)
}
}()
// Build the router
routes.BuildRouter(c)
// Start the server
go func() {
srv := http.Server{
Addr: fmt.Sprintf("%s:%d", c.Config.HTTP.Hostname, c.Config.HTTP.Port),
Handler: c.Web,
ReadTimeout: c.Config.HTTP.ReadTimeout,
WriteTimeout: c.Config.HTTP.WriteTimeout,
IdleTimeout: c.Config.HTTP.IdleTimeout,
}
if c.Config.HTTP.TLS.Enabled {
certs, err := tls.LoadX509KeyPair(c.Config.HTTP.TLS.Certificate, c.Config.HTTP.TLS.Key)
if err != nil {
c.Web.Logger.Fatalf("cannot load TLS certificate: %v", err)
}
srv.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{certs},
}
}
if err := c.Web.StartServer(&srv); err != http.ErrServerClosed {
c.Web.Logger.Fatalf("shutting down the server: %v", err)
}
}()
// Start the scheduler service to queue periodic tasks
go func() {
if err := c.Tasks.StartScheduler(); err != nil {
c.Web.Logger.Fatalf("scheduler shutdown: %v", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
signal.Notify(quit, os.Kill)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := c.Web.Shutdown(ctx); err != nil {
c.Web.Logger.Fatal(err)
}
}

45
cmd/worker/main.go Normal file
View file

@ -0,0 +1,45 @@
package main
import (
"fmt"
"log"
"github.com/hibiken/asynq"
"github.com/mikestefanello/pagoda/config"
"github.com/mikestefanello/pagoda/pkg/tasks"
)
func main() {
// Load the configuration
cfg, err := config.GetConfig()
if err != nil {
panic(fmt.Sprintf("failed to load config: %v", err))
}
// Build the worker server
srv := asynq.NewServer(
asynq.RedisClientOpt{
Addr: fmt.Sprintf("%s:%d", cfg.Cache.Hostname, cfg.Cache.Port),
DB: cfg.Cache.Database,
Password: cfg.Cache.Password,
},
asynq.Config{
// See asynq.Config for all available options and explanation
Concurrency: 10,
Queues: map[string]int{
"critical": 6,
"default": 3,
"low": 1,
},
},
)
// Map task types to the handlers
mux := asynq.NewServeMux()
mux.Handle(tasks.TypeExample, new(tasks.ExampleProcessor))
// Start the worker server
if err := srv.Run(mux); err != nil {
log.Fatalf("could not run worker server: %v", err)
}
}