-
Notifications
You must be signed in to change notification settings - Fork 2
add multiple docker cmds via api #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Bostigger
wants to merge
5
commits into
main
Choose a base branch
from
compozify-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c3bb217
add multiple docker cmds to one single file
Bostigger a28ac33
add multiple docker cmds to one single file
Bostigger cb422a6
add multiple docker cmds to one single file
Bostigger 482960b
add multiple docker cmds to one single file
Bostigger f1f571d
add multiple docker cmds to one single file
Bostigger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ package api | |
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "github.com/rs/zerolog" | ||
| "io" | ||
| "net/http" | ||
| "path" | ||
| "strings" | ||
|
|
@@ -16,74 +18,74 @@ 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 dockerCmds DockerCommands | ||
|
|
||
| var dockerCmd DockerCommand | ||
|
|
||
| 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() | ||
| code := http.StatusOK | ||
| errorMsg := "" | ||
| defer func() { | ||
| log := logger.Info() | ||
| if errorMsg != "" { | ||
| log = logger.Error() | ||
| http.Error(w, errorMsg, code) | ||
| defer func(Body io.ReadCloser) { | ||
| err := Body.Close() | ||
| if err != nil { | ||
| logger.Err(err).Msg("Error closing request body") | ||
| } | ||
| log.Msgf("Returned %d in %v", code, time.Since(start)) | ||
| }() | ||
| err := json.NewDecoder(r.Body).Decode(&dockerCmd) | ||
| }(r.Body) | ||
|
|
||
| err := json.NewDecoder(r.Body).Decode(&dockerCmds) | ||
| if err != nil { | ||
| errorMsg = fmt.Sprintf("Error decoding request body: %v", err) | ||
| code = http.StatusBadRequest | ||
| writeError(w, logger, "Error decoding request body", err, http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Validate the command. | ||
| if dockerCmd.Command == "" { | ||
| errorMsg = "Docker command cannot be empty" | ||
| code = http.StatusBadRequest | ||
| return | ||
| } | ||
| start := time.Now() | ||
| defer logDuration(logger, start) | ||
|
|
||
| // Create a new Parser | ||
| p, err := parser.New(dockerCmd.Command) | ||
| if err != nil { | ||
| errorMsg = fmt.Sprintf("Error creating parser: %v", err) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| var p *parser.Parser | ||
| for _, cmd := range dockerCmds.Commands { | ||
| if cmd == "" { | ||
| writeError(w, logger, "Docker command cannot be empty", nil, http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Parse the Docker command | ||
| err = p.Parse() | ||
| if err != nil { | ||
| errorMsg = fmt.Sprintf("Error parsing Docker command: %v", err) | ||
Bostigger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| code = http.StatusBadRequest | ||
| return | ||
| } | ||
| if p == nil { | ||
| p, err = parser.New(cmd) | ||
Bostigger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| p, err = parser.AppendToYAML([]byte(p.String()), cmd) | ||
| } | ||
|
|
||
| dockerComposeYaml := p.String() | ||
| if err != nil { | ||
| writeError(w, logger, "Error parsing Docker command", err, http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Create the response | ||
| resp := Response{ | ||
| Output: dockerComposeYaml, | ||
| if err := p.Parse(); err != nil { | ||
| writeError(w, logger, "Error parsing Docker command", err, http.StatusBadRequest) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| resp := Response{ | ||
| Output: p.String(), | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| err = json.NewEncoder(w).Encode(resp) | ||
| if err != nil { | ||
| if err := json.NewEncoder(w).Encode(resp); err != nil { | ||
| logger.Err(err).Msg("Unable to write response") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| func writeError(w http.ResponseWriter, logger zerolog.Logger, msg string, err error, code int) { | ||
| logger.Err(err).Msg(msg) | ||
| http.Error(w, fmt.Sprintf("%s: %v", msg, err), code) | ||
| } | ||
|
|
||
| func logDuration(logger zerolog.Logger, start time.Time) { | ||
| logger.Info().Msgf("Returned in %v", time.Since(start)) | ||
| } | ||
|
|
||
| // appHandler is web app http handler function. | ||
| func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) { | ||
| staticServer := http.FileServer(http.FS(server.assets)) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "net" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/rs/zerolog" | ||
| ) | ||
|
|
||
| func TestServer(t *testing.T) { | ||
| var logBuffer bytes.Buffer | ||
| logger := zerolog.New(&logBuffer).With().Timestamp().Logger() | ||
| listener, err := net.Listen("tcp", ":0") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create listener: %v", err) | ||
| } | ||
| defer func() { | ||
| if err := listener.Close(); err != nil { | ||
| logger.Error().Err(err).Msg("Failed to close listener") | ||
| } | ||
| }() | ||
|
|
||
| addr := listener.Addr().(*net.TCPAddr) | ||
| port := addr.Port | ||
| endpoint := fmt.Sprintf("http://localhost:%d/api/parse", port) | ||
| logger.Info().Msgf("Endpoint: %s", endpoint) | ||
|
|
||
| server := NewServer(&logger, listener, nil) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second) | ||
| defer cancel() | ||
|
|
||
| go func() { | ||
| if err := server.Run(ctx); err != nil && err != context.Canceled { | ||
| logger.Error().Err(err).Msg("Server error") | ||
| } | ||
| }() | ||
|
|
||
| // Wait for the server to start | ||
| time.Sleep(1 * time.Second) | ||
|
|
||
| fmt.Printf("Server is running on %s. You have 2 minutes to test manually.\n", endpoint) | ||
|
|
||
| // Wait for 2 minutes to allow manual testing | ||
| time.Sleep(2 * time.Minute) | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?