-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,70 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
"github.com/spf13/cobra" | ||
"manala/core/application" | ||
internalConfig "manala/internal/config" | ||
internalLog "manala/internal/log" | ||
"manala/web/api" | ||
"net/http" | ||
) | ||
|
||
func newWebCmd(config *internalConfig.Config, log *internalLog.Logger) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "web", | ||
Aliases: []string{"w"}, | ||
Args: cobra.NoArgs, | ||
DisableAutoGenTag: true, | ||
Short: "Web interface", | ||
Long: `Web (manala web) will launch web interface. | ||
Example: manala web -> resulting in a web interface launch`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// Application options | ||
var appOptions []application.Option | ||
|
||
// Flag - Repository url | ||
if cmd.Flags().Changed("repository") { | ||
repoUrl, _ := cmd.Flags().GetString("repository") | ||
appOptions = append(appOptions, application.WithRepositoryUrl(repoUrl)) | ||
} | ||
|
||
// Flag - Repository ref | ||
if cmd.Flags().Changed("ref") { | ||
repoRef, _ := cmd.Flags().GetString("ref") | ||
appOptions = append(appOptions, application.WithRepositoryRef(repoRef)) | ||
} | ||
|
||
// Application | ||
app := application.NewApplication( | ||
config, | ||
log, | ||
appOptions..., | ||
) | ||
|
||
// Web Api | ||
webApi := api.New(app) | ||
|
||
router := chi.NewRouter() | ||
router.Use(middleware.Logger) | ||
|
||
router.Mount("/api", webApi.Handle()) | ||
|
||
// Server | ||
server := &http.Server{ | ||
Addr: ":9400", | ||
Handler: router, | ||
} | ||
|
||
return server.ListenAndServe() | ||
}, | ||
} | ||
|
||
// Flags | ||
cmd.Flags().StringP("repository", "o", "", "use repository") | ||
cmd.Flags().String("ref", "", "use repository ref") | ||
|
||
return cmd | ||
} |
This file contains 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 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 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 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 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 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,104 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/render" | ||
"manala/app/interfaces" | ||
"manala/core" | ||
"manala/core/application" | ||
"manala/web/api/views" | ||
"net/http" | ||
) | ||
|
||
func New(app *application.Application) *Api { | ||
api := &Api{ | ||
app: app, | ||
} | ||
|
||
return api | ||
} | ||
|
||
type Api struct { | ||
app *application.Application | ||
} | ||
|
||
func (api *Api) Handle() http.Handler { | ||
router := chi.NewRouter() | ||
|
||
// Recipe | ||
router.Route("/recipe", func(router chi.Router) { | ||
router.Get("/", api.ListRecipes) | ||
router.Route("/{name}", func(router chi.Router) { | ||
router.Use(api.RecipeContext) | ||
router.Get("/", api.GetRecipe) | ||
router.Get("/options", api.GetRecipeOptions) | ||
}) | ||
}) | ||
|
||
return router | ||
} | ||
|
||
func (api *Api) ListRecipes(response http.ResponseWriter, request *http.Request) { | ||
var recViews []*views.RecipeView | ||
|
||
// Walk into recipes | ||
if err := api.app.WalkRecipes(func(rec interfaces.Recipe) error { | ||
recViews = append(recViews, views.NormalizeRecipe(rec)) | ||
return nil | ||
}); err != nil { | ||
http.Error(response, http.StatusText(500), 500) | ||
return | ||
} | ||
|
||
render.JSON(response, request, recViews) | ||
} | ||
|
||
func (api *Api) RecipeContext(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { | ||
name := chi.URLParam(request, "name") | ||
rec, err := api.app.LoadRecipe(name) | ||
if err != nil { | ||
var _notFoundRecipeManifestError *core.NotFoundRecipeManifestError | ||
if errors.As(err, &_notFoundRecipeManifestError) { | ||
http.Error(response, http.StatusText(404), 404) | ||
} else { | ||
http.Error(response, http.StatusText(500), 500) | ||
} | ||
return | ||
} | ||
ctx := context.WithValue(request.Context(), "recipe", rec) | ||
next.ServeHTTP(response, request.WithContext(ctx)) | ||
}) | ||
} | ||
|
||
func (api *Api) GetRecipe(response http.ResponseWriter, request *http.Request) { | ||
ctx := request.Context() | ||
rec, ok := ctx.Value("recipe").(interfaces.Recipe) | ||
if !ok { | ||
http.Error(response, http.StatusText(422), 422) | ||
return | ||
} | ||
|
||
recView := views.NormalizeRecipe(rec) | ||
|
||
render.JSON(response, request, recView) | ||
} | ||
|
||
func (api *Api) GetRecipeOptions(response http.ResponseWriter, request *http.Request) { | ||
ctx := request.Context() | ||
rec, ok := ctx.Value("recipe").(interfaces.Recipe) | ||
if !ok { | ||
http.Error(response, http.StatusText(422), 422) | ||
return | ||
} | ||
|
||
var recOptionsViews []*views.RecipeOptionView | ||
|
||
for _, option := range rec.Options() { | ||
recOptionsViews = append(recOptionsViews, views.NormalizeRecipeOption(option)) | ||
} | ||
|
||
render.JSON(response, request, recOptionsViews) | ||
} |
This file contains 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,31 @@ | ||
package views | ||
|
||
import ( | ||
"manala/app/interfaces" | ||
) | ||
|
||
func NormalizeRecipe(rec interfaces.Recipe) *RecipeView { | ||
return &RecipeView{ | ||
Name: rec.Name(), | ||
Description: rec.Description(), | ||
} | ||
} | ||
|
||
type RecipeView struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
} | ||
|
||
/**********/ | ||
/* Option */ | ||
/**********/ | ||
|
||
func NormalizeRecipeOption(option interfaces.RecipeOption) *RecipeOptionView { | ||
return &RecipeOptionView{ | ||
Label: option.Label(), | ||
} | ||
} | ||
|
||
type RecipeOptionView struct { | ||
Label string `json:"label"` | ||
} |
This file contains 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,28 @@ | ||
package views | ||
|
||
import ( | ||
"github.com/stretchr/testify/suite" | ||
"manala/app/mocks" | ||
"testing" | ||
) | ||
|
||
type RecipeSuite struct{ suite.Suite } | ||
|
||
func TestRecipeSuite(t *testing.T) { | ||
suite.Run(t, new(RecipeSuite)) | ||
} | ||
|
||
func (s *RecipeSuite) TestNormalize() { | ||
recName := "name" | ||
recDescription := "description" | ||
|
||
recMock := mocks.MockRecipe() | ||
recMock. | ||
On("Name").Return(recName). | ||
On("Description").Return(recDescription) | ||
|
||
recView := NormalizeRecipe(recMock) | ||
|
||
s.Equal(recName, recView.Name) | ||
s.Equal(recDescription, recView.Description) | ||
} |