From bf3309f7c3e6a1de0fee9e9a15e72713b8917945 Mon Sep 17 00:00:00 2001 From: kingmariano Date: Fri, 16 Aug 2024 20:20:20 +0100 Subject: [PATCH] refactor: refactor codebase --- cmd/api/api.go | 94 ++++++++++++++++++++++++++++++++ cmd/api/endpoints.go | 45 ++++++++++++++++ main.go | 124 +------------------------------------------ tests/router_test.go | 2 +- utils/loadEnv.go | 4 +- 5 files changed, 144 insertions(+), 125 deletions(-) create mode 100644 cmd/api/api.go create mode 100644 cmd/api/endpoints.go diff --git a/cmd/api/api.go b/cmd/api/api.go new file mode 100644 index 0000000..b7cf2b0 --- /dev/null +++ b/cmd/api/api.go @@ -0,0 +1,94 @@ +package api +import ( + "context" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/kingmariano/omnicron/config" + "github.com/kingmariano/omnicron/utils" + "log" + "net/http" + "os" + "os/exec" + "os/signal" + "syscall" + "time" +) + +// starts the python fastAPI server +func startFastAPIServer() *exec.Cmd { + cmd := exec.Command("python", "./python/main.py") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + if err := cmd.Start(); err != nil { + log.Fatalf("Failed to start FastAPI server: %v", err) + } + + // Give the server a few seconds to start + time.Sleep(5 * time.Second) + + return cmd +} + +func StartServer() { + // Load environment variables + apiKey, grokAPIKey, replicateAPIKey, cloudinaryURL, port, fastAPIBaseURL, err := utils.LoadEnv() + if err != nil { + log.Fatalf("error opening .env %v ", err) + } + + // Start the FastAPI server + cmd := startFastAPIServer() + defer func() { + if err := cmd.Process.Kill(); err != nil { + log.Printf("Failed to kill process: %v", err) + } + }() + + cfg := &config.APIConfig{ + APIKey: apiKey, + GrokAPIKey: grokAPIKey, + ReplicateAPIKey: replicateAPIKey, + CloudinaryURL: cloudinaryURL, + FASTAPIBaseURL: fastAPIBaseURL, + Port: port, + } + + router := chi.NewRouter() + router.Use(middleware.Logger) + router.Use(middleware.Recoverer) + + v1Router := chi.NewRouter() + callEndpoints(v1Router, cfg) + router.Mount("/api/v1", v1Router) + + server := &http.Server{ + Addr: ":" + port, + Handler: router, + ReadHeaderTimeout: 5 * time.Second, + } + + // Create a channel to listen for OS signals for graceful shutdown + stop := make(chan os.Signal, 1) + signal.Notify(stop, os.Interrupt, syscall.SIGTERM) + + go func() { + log.Printf("Server started at port %s", port) + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("Failed to listen and serve: %v", err) + } + }() + + <-stop + + log.Println("Shutting down the server...") + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := server.Shutdown(ctx); err != nil { + log.Fatalf("Server forced to shutdown: %v", err) + } + + log.Println("Server exiting") +} diff --git a/cmd/api/endpoints.go b/cmd/api/endpoints.go new file mode 100644 index 0000000..4e8135b --- /dev/null +++ b/cmd/api/endpoints.go @@ -0,0 +1,45 @@ +package api + +import ( + "github.com/go-chi/chi/v5" + "github.com/kingmariano/omnicron/config" + ware "github.com/kingmariano/omnicron/middleware" + "github.com/kingmariano/omnicron/packages/convert2mp3" + "github.com/kingmariano/omnicron/packages/docgpt" + "github.com/kingmariano/omnicron/packages/gpt" + "github.com/kingmariano/omnicron/packages/grok" + "github.com/kingmariano/omnicron/packages/image2text" + "github.com/kingmariano/omnicron/packages/musicdownloader" + "github.com/kingmariano/omnicron/packages/musicsearch" + "github.com/kingmariano/omnicron/packages/replicate/generateimages" + "github.com/kingmariano/omnicron/packages/replicate/generatemusic" + "github.com/kingmariano/omnicron/packages/replicate/generatevideos" + "github.com/kingmariano/omnicron/packages/replicate/imageupscale" + "github.com/kingmariano/omnicron/packages/replicate/stt" + "github.com/kingmariano/omnicron/packages/replicate/tts" + "github.com/kingmariano/omnicron/packages/shazam" + "github.com/kingmariano/omnicron/packages/videodownloader" + "github.com/kingmariano/omnicron/packages/youtubesummarize" + "github.com/kingmariano/omnicron/utils" +) + +func callEndpoints(v1Router *chi.Mux, cfg *config.APIConfig){ + v1Router.Get("/readiness", utils.HandleReadiness()) + v1Router.Post("/groq/chatcompletion", ware.MiddleWareAuth(grok.ChatCompletion, cfg)) + v1Router.Post("/groq/transcription", ware.MiddleWareAuth(grok.Transcription, cfg)) // deprecated + v1Router.Post("/replicate/imagegeneration", ware.MiddleWareAuth(generateimages.ImageGeneration, cfg)) + v1Router.Post("/replicate/imageupscale", ware.MiddleWareAuth(imageupscale.ImageUpscale, cfg)) + v1Router.Post("/replicate/videogeneration", ware.MiddleWareAuth(generatevideos.VideoGeneration, cfg)) + v1Router.Post("/replicate/tts", ware.MiddleWareAuth(tts.TTS, cfg)) + v1Router.Post("/replicate/stt", ware.MiddleWareAuth(stt.STT, cfg)) + v1Router.Post("/replicate/musicgeneration", ware.MiddleWareAuth(generatemusic.MusicGen, cfg)) + v1Router.Post("/downloadvideo", ware.MiddleWareAuth(videodownloader.DownloadVideo, cfg)) + v1Router.Post("/convert2mp3", ware.MiddleWareAuth(convert2mp3.ConvertToMp3, cfg)) + v1Router.Post("/downloadmusic", ware.MiddleWareAuth(musicdownloader.DownloadMusic, cfg)) + v1Router.Post("/gpt4free", ware.MiddleWareAuth(gpt.ChatCompletion, cfg)) + v1Router.Post("/shazam", ware.MiddleWareAuth(shazam.Shazam, cfg)) + v1Router.Post("/musicsearch", ware.MiddleWareAuth(musicsearch.MusicSearch, cfg)) + v1Router.Post("/youtubesummarization", ware.MiddleWareAuth(youtubesummarize.YoutubeSummarization, cfg)) + v1Router.Post("/image2text", ware.MiddleWareAuth(image2text.Image2text, cfg)) + v1Router.Post("/docgpt", ware.MiddleWareAuth(docgpt.DocGPT, cfg)) +} \ No newline at end of file diff --git a/main.go b/main.go index 2e18cb5..271f94d 100644 --- a/main.go +++ b/main.go @@ -20,130 +20,10 @@ package main -import ( - "context" - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" - "github.com/kingmariano/omnicron/config" - ware "github.com/kingmariano/omnicron/middleware" - "github.com/kingmariano/omnicron/packages/convert2mp3" - "github.com/kingmariano/omnicron/packages/docgpt" - "github.com/kingmariano/omnicron/packages/gpt" - "github.com/kingmariano/omnicron/packages/grok" - "github.com/kingmariano/omnicron/packages/image2text" - "github.com/kingmariano/omnicron/packages/musicdownloader" - "github.com/kingmariano/omnicron/packages/musicsearch" - "github.com/kingmariano/omnicron/packages/replicate/generateimages" - "github.com/kingmariano/omnicron/packages/replicate/generatemusic" - "github.com/kingmariano/omnicron/packages/replicate/generatevideos" - "github.com/kingmariano/omnicron/packages/replicate/imageupscale" - "github.com/kingmariano/omnicron/packages/replicate/stt" - "github.com/kingmariano/omnicron/packages/replicate/tts" - "github.com/kingmariano/omnicron/packages/shazam" - "github.com/kingmariano/omnicron/packages/videodownloader" - "github.com/kingmariano/omnicron/packages/youtubesummarize" - "github.com/kingmariano/omnicron/utils" - "log" - "net/http" - "os" - "os/exec" - "os/signal" - "syscall" - "time" -) +import "github.com/kingmariano/omnicron/cmd/api" -// starts the python fastAPI server -func startFastAPIServer() *exec.Cmd { - cmd := exec.Command("python", "./python/main.py") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Start(); err != nil { - log.Fatalf("Failed to start FastAPI server: %v", err) - } - - // Give the server a few seconds to start - time.Sleep(5 * time.Second) - - return cmd -} func main() { - // Load environment variables - apiKey, grokAPIKey, replicateAPIKey, cloudinaryURL, port, fastAPIBaseURL, err := utils.LoadEnv(".env") - if err != nil { - log.Fatalf("error opening .env %v ", err) - } - - // Start the FastAPI server - cmd := startFastAPIServer() - defer func() { - if err := cmd.Process.Kill(); err != nil { - log.Printf("Failed to kill process: %v", err) - } - }() - - cfg := &config.APIConfig{ - APIKey: apiKey, - GrokAPIKey: grokAPIKey, - ReplicateAPIKey: replicateAPIKey, - CloudinaryURL: cloudinaryURL, - FASTAPIBaseURL: fastAPIBaseURL, - Port: port, - } - - router := chi.NewRouter() - router.Use(middleware.Logger) - router.Use(middleware.Recoverer) - - v1Router := chi.NewRouter() - v1Router.Get("/readiness", utils.HandleReadiness()) - v1Router.Post("/groq/chatcompletion", ware.MiddleWareAuth(grok.ChatCompletion, cfg)) - v1Router.Post("/groq/transcription", ware.MiddleWareAuth(grok.Transcription, cfg)) // deprecated - v1Router.Post("/replicate/imagegeneration", ware.MiddleWareAuth(generateimages.ImageGeneration, cfg)) - v1Router.Post("/replicate/imageupscale", ware.MiddleWareAuth(imageupscale.ImageUpscale, cfg)) - v1Router.Post("/replicate/videogeneration", ware.MiddleWareAuth(generatevideos.VideoGeneration, cfg)) - v1Router.Post("/replicate/tts", ware.MiddleWareAuth(tts.TTS, cfg)) - v1Router.Post("/replicate/stt", ware.MiddleWareAuth(stt.STT, cfg)) - v1Router.Post("/replicate/musicgeneration", ware.MiddleWareAuth(generatemusic.MusicGen, cfg)) - v1Router.Post("/downloadvideo", ware.MiddleWareAuth(videodownloader.DownloadVideo, cfg)) - v1Router.Post("/convert2mp3", ware.MiddleWareAuth(convert2mp3.ConvertToMp3, cfg)) - v1Router.Post("/downloadmusic", ware.MiddleWareAuth(musicdownloader.DownloadMusic, cfg)) - v1Router.Post("/gpt4free", ware.MiddleWareAuth(gpt.ChatCompletion, cfg)) - v1Router.Post("/shazam", ware.MiddleWareAuth(shazam.Shazam, cfg)) - v1Router.Post("/musicsearch", ware.MiddleWareAuth(musicsearch.MusicSearch, cfg)) - v1Router.Post("/youtubesummarization", ware.MiddleWareAuth(youtubesummarize.YoutubeSummarization, cfg)) - v1Router.Post("/image2text", ware.MiddleWareAuth(image2text.Image2text, cfg)) - v1Router.Post("/docgpt", ware.MiddleWareAuth(docgpt.DocGPT, cfg)) - router.Mount("/api/v1", v1Router) - - server := &http.Server{ - Addr: ":" + port, - Handler: router, - ReadHeaderTimeout: 5 * time.Second, - } - - // Create a channel to listen for OS signals for graceful shutdown - stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, syscall.SIGTERM) - - go func() { - log.Printf("Server started at port %s", port) - if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Fatalf("Failed to listen and serve: %v", err) - } - }() - - <-stop - - log.Println("Shutting down the server...") - - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - if err := server.Shutdown(ctx); err != nil { - log.Fatalf("Server forced to shutdown: %v", err) - } - - log.Println("Server exiting") + api.StartServer() // start the API server } diff --git a/tests/router_test.go b/tests/router_test.go index eed71ab..0972e89 100644 --- a/tests/router_test.go +++ b/tests/router_test.go @@ -24,7 +24,7 @@ import ( ) func setupRouter(t *testing.T) (*chi.Mux, *config.APIConfig) { - apiKey, grokAPIKey, replicateAPIKey, cloudinaryURL, port, _, err := utils.LoadEnv("../.env") + apiKey, grokAPIKey, replicateAPIKey, cloudinaryURL, port, _, err := utils.LoadEnv() if err != nil { // if the given environment path is not set. Get the variables from the root environment path diff --git a/utils/loadEnv.go b/utils/loadEnv.go index 13cb798..0f635b2 100644 --- a/utils/loadEnv.go +++ b/utils/loadEnv.go @@ -28,8 +28,8 @@ import ( ) // this function loads the environment variables from the path -func LoadEnv(path string) (string, string, string, string, string, string, error) { - err := godotenv.Load(path) +func LoadEnv() (string, string, string, string, string, string, error) { + err := godotenv.Load() if err != nil { log.Print(err) }