port is now being served
This commit is contained in:
4
.env.example
Normal file
4
.env.example
Normal file
@@ -0,0 +1,4 @@
|
||||
APP_PORT=8080
|
||||
|
||||
OLLAMA_API_KEY=your_api_key_here
|
||||
ROBOWFLOW_API_KEY=your_api_key_here
|
||||
3
Makefile
3
Makefile
@@ -1,10 +1,9 @@
|
||||
env ?= development
|
||||
port ?= 8080
|
||||
|
||||
junk2jive:
|
||||
@go build ./cmd/junk2jive
|
||||
run:
|
||||
@go run ./cmd/junk2jive -e $(env) -p $(port)
|
||||
@go run ./cmd/junk2jive -p $(port)
|
||||
test:
|
||||
@go test ./... -v
|
||||
test_coverage:
|
||||
|
||||
@@ -1,36 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/logger"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flags := config.DeclareFlags()
|
||||
port := config.ConfigPort(flags)
|
||||
|
||||
// Load configuration
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load configuration: %v", err)
|
||||
}
|
||||
// Set up routes
|
||||
router := router.SetupRouter(config.AllowedOrigins)
|
||||
server := config.SetupServer(port, router, flags)
|
||||
|
||||
// Initialize services
|
||||
// This would initialize your OpenAI and Robowflow services
|
||||
// based on your configuration
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
// Set up the router
|
||||
router := routes.SetupRoutes(cfg)
|
||||
go func() {
|
||||
logger.Info("Starting server on %s", port)
|
||||
err := server.ListenAndServe()
|
||||
if err != nil {
|
||||
logger.Error("Error starting server %v", err)
|
||||
}
|
||||
|
||||
// Start the server
|
||||
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
|
||||
log.Printf("Starting server on %s", addr)
|
||||
if err := http.ListenAndServe(addr, router); err != nil {
|
||||
log.Fatalf("Failed to start server: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
<-stop
|
||||
logger.Info("Shutting down gracefully...")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(ctx); err != nil {
|
||||
logger.Error("Error shutting down HTTP server: %v", err)
|
||||
}
|
||||
|
||||
logger.Info("Shutdown complete.")
|
||||
|
||||
}
|
||||
|
||||
67
coverage.out
67
coverage.out
@@ -1,36 +1,27 @@
|
||||
mode: set
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:13.27,19.2 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:21.57,22.45 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:22.45,24.6 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:25.5,25.24 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/text.go:20.61,21.57 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/text.go:21.57,23.68 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/text.go:23.68,26.10 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/text.go:28.9,30.23 3 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/text.go:30.23,33.10 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/text.go:35.9,41.44 3 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:16.59,17.57 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:17.57,19.62 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:19.62,22.10 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:24.9,25.23 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:25.23,28.10 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:29.9,33.23 3 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:33.23,36.10 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:37.9,41.57 3 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:41.57,44.10 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:47.9,49.23 3 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:49.23,52.10 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:55.9,58.23 4 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:58.23,61.10 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers/visual.go:64.9,70.44 3 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes/routes.go:29.44,34.2 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes/routes.go:36.44,66.55 13 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes/routes.go:66.55,67.46 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes/routes.go:67.46,70.74 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes/routes.go:70.74,72.5 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes/routes.go:74.4,75.65 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes/routes.go:78.2,78.11 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/routes/routes.go:82.39,84.2 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/cmd/junk2jive/main.go:15.13,26.12 7 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/cmd/junk2jive/main.go:26.12,29.17 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/cmd/junk2jive/main.go:29.17,31.4 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/cmd/junk2jive/main.go:35.2,41.45 5 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/cmd/junk2jive/main.go:41.45,43.3 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/cmd/junk2jive/main.go:45.2,45.35 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:22.31,23.48 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:23.48,25.3 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:28.28,37.2 3 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:39.38,41.21 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:41.21,43.3 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:43.8,44.34 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:44.34,47.4 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:48.3,48.51 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:50.2,50.13 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config/config.go:53.81,62.2 3 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router/router.go:26.26,30.2 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router/router.go:32.44,62.55 13 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router/router.go:62.55,63.46 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router/router.go:63.46,66.74 2 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router/router.go:66.74,68.5 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router/router.go:74.2,74.11 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router/router.go:78.39,80.2 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/services/openai.go:33.53,35.2 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/services/openai.go:37.67,58.19 5 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/services/openai.go:58.19,60.6 1 0
|
||||
@@ -77,12 +68,6 @@ gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/logger/request.go:32.4
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/logger/request.go:36.4,36.80 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/logger/request.go:36.80,38.5 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/logger/request.go:38.10,40.5 1 0
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:13.43,16.2 2 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:18.38,19.63 1 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:19.63,21.3 1 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:22.2,22.12 1 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:25.76,30.2 4 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:32.100,35.2 2 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter/limiter.go:19.25,23.2 1 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter/limiter.go:25.36,27.16 2 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter/limiter.go:27.16,29.3 1 1
|
||||
@@ -97,3 +82,9 @@ gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter/limiter.go:51.
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter/limiter.go:52.72,56.24 3 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter/limiter.go:56.24,59.5 2 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter/limiter.go:61.4,61.24 1 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:13.43,16.2 2 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:18.38,19.63 1 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:19.63,21.3 1 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:22.2,22.12 1 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:25.76,30.2 4 1
|
||||
gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response/response.go:32.100,35.2 2 1
|
||||
|
||||
4
data/config.yaml
Normal file
4
data/config.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
cors:
|
||||
allowed_origins:
|
||||
- "https://junk2jive.miguelmuniz.com"
|
||||
- "https://api.junk2jive.miguelmuniz.com"
|
||||
@@ -3,22 +3,20 @@ package config
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/logger"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/router"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
OllamaKey string
|
||||
RoboflowKey string
|
||||
Port string
|
||||
}
|
||||
var AllowedOrigins []string
|
||||
|
||||
type Flags struct {
|
||||
OllamaKey string
|
||||
RoboflowKey int
|
||||
Port string
|
||||
// OllamaKey string
|
||||
// RoboflowKey int
|
||||
Port int
|
||||
}
|
||||
|
||||
func LoadEnv(filePath string) {
|
||||
@@ -28,16 +26,13 @@ func LoadEnv(filePath string) {
|
||||
}
|
||||
|
||||
func DeclareFlags() *Flags {
|
||||
env := flag.String("e", "development", "Set the environment ( development | production )")
|
||||
// env := flag.String("e", "development", "Set the environment ( development | production )")
|
||||
port := flag.Int("p", 0, "Set the port that will be used")
|
||||
envFile := flag.String("ef", "", "Set the imported env file")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
return &Flags{
|
||||
OllamaKey: *env,
|
||||
RoboflowKey: *port,
|
||||
Port: *envFile,
|
||||
Port: *port,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,3 +49,14 @@ func ConfigPort(flags *Flags) string {
|
||||
}
|
||||
return port
|
||||
}
|
||||
|
||||
func SetupServer(port string, router *router.Router, flags *Flags) *http.Server {
|
||||
var server *http.Server
|
||||
|
||||
server = &http.Server{
|
||||
Addr: port,
|
||||
Handler: router.GetRouter(),
|
||||
}
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/services"
|
||||
)
|
||||
|
||||
type TextPromptRequest struct {
|
||||
Query string `json:"query"`
|
||||
}
|
||||
|
||||
type DIYResponse struct {
|
||||
Prompt string `json:"prompt"`
|
||||
Result string `json:"result"`
|
||||
}
|
||||
|
||||
func TextPromptHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req TextPromptRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
openAI := services.NewOpenAIService(cfg.OpenAIKey)
|
||||
result, err := openAI.GenerateDIY(req.Query)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to generate DIY ideas", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response := DIYResponse{
|
||||
Prompt: "AI Suggestions for " + req.Query + " are:",
|
||||
Result: result,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/services"
|
||||
)
|
||||
|
||||
func VisualAIHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Parse multipart form with 10MB limit
|
||||
if err := r.ParseMultipartForm(10 << 20); err != nil {
|
||||
http.Error(w, "Unable to parse form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
file, handler, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(w, "Error retrieving the file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Create temporary file
|
||||
tempFile, err := os.CreateTemp("", "upload-*"+filepath.Ext(handler.Filename))
|
||||
if err != nil {
|
||||
http.Error(w, "Error creating temporary file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer os.Remove(tempFile.Name())
|
||||
defer tempFile.Close()
|
||||
|
||||
// Copy uploaded file to temp file
|
||||
if _, err = io.Copy(tempFile, file); err != nil {
|
||||
http.Error(w, "Error saving the file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Process with Roboflow
|
||||
roboflowService := services.NewRoboflowService(cfg.RoboflowKey)
|
||||
detectedObjects, err := roboflowService.DetectObjects(tempFile.Name())
|
||||
if err != nil {
|
||||
http.Error(w, "Error detecting objects", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate DIY ideas based on detected objects
|
||||
openAI := services.NewOpenAIService(cfg.OpenAIKey)
|
||||
query := strings.Join(detectedObjects, ", ")
|
||||
result, err := openAI.GenerateDIY(query)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to generate DIY ideas", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare response
|
||||
response := DIYResponse{
|
||||
Prompt: fmt.Sprintf("AI Suggestions for %s are:", query),
|
||||
Result: result,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
}
|
||||
2
internal/ollama/ollama.go
Normal file
2
internal/ollama/ollama.go
Normal file
@@ -0,0 +1,2 @@
|
||||
package ollama
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package routes
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -10,8 +10,6 @@ import (
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/time/rate"
|
||||
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/config"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/handlers"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/limiter"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/logger"
|
||||
"gitea.miguelmuniz.com/rogueking/junk2jive-server/internal/response"
|
||||
@@ -20,16 +18,14 @@ import (
|
||||
// Router encapsulates the Chi router and its dependencies
|
||||
type Router struct {
|
||||
router *chi.Mux
|
||||
config *config.Config
|
||||
logger *zap.Logger
|
||||
rateLimiter *limiter.RateLimiter
|
||||
}
|
||||
|
||||
// NewRouter creates and configures a new Router instance
|
||||
func NewRouter(cfg *config.Config) *Router {
|
||||
func NewRouter() *Router {
|
||||
return &Router{
|
||||
router: chi.NewRouter(),
|
||||
config: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,8 +67,8 @@ func SetupRouter(origins []string) *Router {
|
||||
response.RespondWithJSON(w, http.StatusTeapot, map[string]string{"error": "I'm A Teapot!"})
|
||||
})
|
||||
|
||||
subRouter.Post("/text", handlers.TextPromptHandler(r.config))
|
||||
subRouter.Post("/visual", handlers.VisualAIHandler(r.config))
|
||||
// subRouter.Post("/text", handlers.TextPromptHandler())
|
||||
// subRouter.Post("/visual", handlers.VisualAIHandler())
|
||||
})
|
||||
})
|
||||
return &r
|
||||
Reference in New Issue
Block a user