Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@ package api
import (
"encoding/json"
"fmt"
"github.com/profclems/compozify/pkg/parser"
Comment thread
Bostigger marked this conversation as resolved.
Outdated
"net/http"
"path"
"strings"
"time"

"github.com/profclems/compozify/pkg/parser"
)

// Response is the response body for the ParseDockerCommand handler.
type Response struct {
Output string `json:"output"`
}

// ParseDockerCommand parses a Docker command and returns the equivalent Docker Compose YAML.
func (server *Server) ParseDockerCommand(w http.ResponseWriter, r *http.Request) {
type DockerCommand struct {
Command string `json:"command"`
// ParseDockerCommands ParseDockerCommand parses a Docker command and returns the equivalent Docker Compose YAML.
func (server *Server) ParseDockerCommands(w http.ResponseWriter, r *http.Request) {
type DockerCommands struct {
Commands []string `json:"commands"`
}

var dockerCmd DockerCommand
var dockerCmds DockerCommands

logger := server.logger.With().Str("handler", "ParseDockerCommand").Str("remoteAddr", r.RemoteAddr).Logger()
logger := server.logger.With().Str("handler", "ParseDockerCommands").Str("remoteAddr", r.RemoteAddr).Logger()
logger.Info().Msgf("%s %s %s", r.Method, r.URL.Path, r.Proto)

start := time.Now()
Expand All @@ -38,35 +37,42 @@ func (server *Server) ParseDockerCommand(w http.ResponseWriter, r *http.Request)
}
log.Msgf("Returned %d in %v", code, time.Since(start))
}()
err := json.NewDecoder(r.Body).Decode(&dockerCmd)

err := json.NewDecoder(r.Body).Decode(&dockerCmds)
if err != nil {
errorMsg = fmt.Sprintf("Error decoding request body: %v", err)
code = http.StatusBadRequest
return
}

// Validate the command.
if dockerCmd.Command == "" {
errorMsg = "Docker command cannot be empty"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace the errorMsg and the code here with the writeError method?

code = http.StatusBadRequest
return
}
var p *parser.Parser
for _, cmd := range dockerCmds.Commands {
if cmd == "" {
errorMsg = "Docker command cannot be empty"
code = http.StatusBadRequest
return
}

// Create a new Parser
p, err := parser.New(dockerCmd.Command)
if err != nil {
errorMsg = fmt.Sprintf("Error creating parser: %v", err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace the errorMsg and code with the writeError method?

code = http.StatusBadRequest
return
}
// Create a new Parser or append to existing parser
if p == nil {
p, err = parser.New(cmd)
Comment thread
Bostigger marked this conversation as resolved.
} else {
yamlBytes := []byte(p.String())
p, err = parser.AppendToYAML(yamlBytes, cmd)
}

// Parse the Docker command
err = p.Parse()
if err != nil {
errorMsg = fmt.Sprintf("Error parsing Docker command: %v", err)
Comment thread
Bostigger marked this conversation as resolved.
code = http.StatusBadRequest
return
if err != nil {
errorMsg = fmt.Sprintf("Error parsing Docker command: %v", err)
code = http.StatusBadRequest
return
}

err = p.Parse()
if err != nil {
errorMsg = fmt.Sprintf("Error parsing Docker command: %v", err)
code = http.StatusBadRequest
return
}
}

dockerComposeYaml := p.String()
Expand Down
2 changes: 1 addition & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewServer(logger *zerolog.Logger, listener net.Listener, assets fs.FS) *Ser
}

r := mux.NewRouter()
r.HandleFunc("/api/parse", server.ParseDockerCommand).Methods("POST")
r.HandleFunc("/api/parse", server.ParseDockerCommands).Methods("POST")
r.PathPrefix("/").HandlerFunc(server.appHandler)

server.http = http.Server{
Expand Down
26 changes: 26 additions & 0 deletions api/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api

import (
"context"
"net"
"os"
"testing"

"github.com/rs/zerolog"
)

func TestServer(t *testing.T) {
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
listener, err := net.Listen("tcp", ":8080")
if err != nil {
t.Fatalf("Failed to create listener: %v", err)
}

server := NewServer(&logger, listener, nil)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err := server.Run(ctx); err != nil {
t.Fatalf("Server error: %v", err)
}
}