Skip to content

Commit

Permalink
refactor: refactor codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
kingmariano committed Aug 16, 2024
1 parent 7732b8d commit bf3309f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 125 deletions.
94 changes: 94 additions & 0 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
@@ -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")
}
45 changes: 45 additions & 0 deletions cmd/api/endpoints.go
Original file line number Diff line number Diff line change
@@ -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))
}
124 changes: 2 additions & 122 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions utils/loadEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit bf3309f

Please sign in to comment.