jokes-bapak2/api/main.go

162 lines
3.4 KiB
Go
Raw Normal View History

2021-05-02 07:49:13 +00:00
package main
import (
2022-09-03 10:53:46 +00:00
"errors"
2021-05-02 07:58:37 +00:00
"log"
2022-09-03 10:53:46 +00:00
"net/http"
2021-07-09 06:11:11 +00:00
"os"
2021-07-09 12:13:19 +00:00
"os/signal"
2021-07-09 06:11:11 +00:00
2021-10-30 03:24:53 +00:00
"context"
"jokes-bapak2-api/core/joke"
"jokes-bapak2-api/routes"
2022-09-03 10:53:46 +00:00
"github.com/go-redis/redis/v8"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
2021-10-30 03:24:53 +00:00
2022-09-03 10:53:46 +00:00
"time"
2021-10-30 03:24:53 +00:00
"github.com/allegro/bigcache/v3"
"github.com/getsentry/sentry-go"
2022-09-03 10:53:46 +00:00
"github.com/go-chi/chi/v5"
2021-05-02 07:49:13 +00:00
)
func main() {
2022-09-03 10:53:46 +00:00
redisUrl, ok := os.LookupEnv("REDIS_URL")
if !ok {
redisUrl = "redis://@localhost:6379"
2021-10-30 03:24:53 +00:00
}
2022-09-03 10:53:46 +00:00
minioHost, ok := os.LookupEnv("MINIO_HOST")
if !ok {
minioHost = "localhost:9000"
2021-10-30 03:24:53 +00:00
}
2022-09-03 10:53:46 +00:00
minioID, ok := os.LookupEnv("MINIO_ACCESS_ID")
if !ok {
minioID = "minio"
}
minioSecret, ok := os.LookupEnv("MINIO_SECRET_KEY")
if !ok {
minioSecret = "password"
}
minioToken, ok := os.LookupEnv("MINIO_TOKEN")
if !ok {
minioToken = ""
}
sentryDsn, ok := os.LookupEnv("SENTRY_DSN")
if !ok {
sentryDsn = ""
}
port, ok := os.LookupEnv("PORT")
if !ok {
port = "5000"
}
hostname, ok := os.LookupEnv("HOSTNAME")
if !ok {
hostname = "127.0.0.1"
}
environment, ok := os.LookupEnv("ENVIRONMENT")
if !ok {
environment = "development"
2021-10-30 03:24:53 +00:00
}
// Setup In Memory
memory, err := bigcache.NewBigCache(bigcache.DefaultConfig(6 * time.Hour))
if err != nil {
log.Panicln(err)
}
defer memory.Close()
2022-09-03 10:53:46 +00:00
// Setup MinIO
minioClient, err := minio.New(minioHost, &minio.Options{
Creds: credentials.NewStaticV4(minioID, minioSecret, minioToken),
})
if err != nil {
log.Fatalf("setting up minio client: %s", err.Error())
return
}
parsedRedisUrl, err := redis.ParseURL(redisUrl)
if err != nil {
log.Fatalf("parsing redis url: %s", err.Error())
return
}
redisClient := redis.NewClient(parsedRedisUrl)
defer func() {
err := redisClient.Close()
if err != nil {
log.Printf("closing redis client: %s", err.Error())
}
}()
2021-10-30 03:24:53 +00:00
// Setup Sentry
err = sentry.Init(sentry.ClientOptions{
2022-09-03 10:53:46 +00:00
Dsn: sentryDsn,
Environment: environment,
2021-10-30 03:24:53 +00:00
AttachStacktrace: true,
// Enable printing of SDK debug messages.
// Useful when getting started or trying to figure something out.
2022-09-03 10:53:46 +00:00
Debug: environment != "production",
2021-10-30 03:24:53 +00:00
})
if err != nil {
2022-09-03 10:53:46 +00:00
log.Fatalf("setting up sentry: %s", err.Error())
return
2021-10-30 03:24:53 +00:00
}
defer sentry.Flush(2 * time.Second)
setupCtx, setupCancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute*4))
defer setupCancel()
2022-09-03 10:53:46 +00:00
_, _, err = joke.GetTodaysJoke(setupCtx, minioClient, redisClient, memory)
2021-10-30 03:24:53 +00:00
if err != nil {
2022-09-03 10:53:46 +00:00
log.Fatalf("getting initial joke data: %s", err.Error())
return
2021-10-30 03:24:53 +00:00
}
2022-09-03 10:53:46 +00:00
healthRouter := routes.Health(minioClient, redisClient)
jokeRouter := routes.Joke(minioClient, redisClient, memory)
2021-10-30 03:24:53 +00:00
2022-09-03 10:53:46 +00:00
router := chi.NewRouter()
2021-10-30 03:24:53 +00:00
2022-09-03 10:53:46 +00:00
router.Mount("/health", healthRouter)
router.Mount("/", jokeRouter)
2021-10-30 03:24:53 +00:00
2022-09-03 10:53:46 +00:00
server := &http.Server{
Handler: router,
Addr: hostname + ":" + port,
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
IdleTimeout: time.Second * 30,
ReadHeaderTimeout: time.Minute,
2021-07-09 12:13:19 +00:00
}
2021-07-15 13:00:44 +00:00
2022-09-03 10:53:46 +00:00
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt)
2021-07-09 12:13:19 +00:00
go func() {
2022-09-03 10:53:46 +00:00
err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("listening http server: %v", err)
2021-07-09 12:13:19 +00:00
}
}()
2022-09-03 10:53:46 +00:00
<-exitSignal
2021-07-09 12:13:19 +00:00
2022-09-03 10:53:46 +00:00
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*30)
defer shutdownCancel()
2021-07-09 12:13:19 +00:00
2022-09-03 10:53:46 +00:00
err = server.Shutdown(shutdownCtx)
if err != nil {
log.Printf("shutting down http server: %v", err)
2021-07-09 12:13:19 +00:00
}
}