From ecac5edaf8852118af141a751046589f75548789 Mon Sep 17 00:00:00 2001 From: Kevin Lyda <kevin@lyda.ie> Date: Sun, 24 Nov 2024 19:08:40 +0000 Subject: [PATCH] Create some more endpoints --- api/boxes.yaml | 101 ++++++++++++++++++++++++++++++++++++++++---- cmd/root.go | 26 ++---------- cmd/server.go | 23 ++++++++++ go.mod | 2 + go.sum | 12 ++++++ server/endpoints.go | 16 +++++++ server/server.go | 6 ++- 7 files changed, 153 insertions(+), 33 deletions(-) create mode 100644 cmd/server.go diff --git a/api/boxes.yaml b/api/boxes.yaml index f956820..3b1973c 100644 --- a/api/boxes.yaml +++ b/api/boxes.yaml @@ -26,17 +26,69 @@ paths: '403': $ref: '#/components/responses/Error' + /boxes: + get: + summary: Boxes + description: Get a list of boxes. + operationId: GetBoxes + parameters: + - $ref: '#/components/parameters/LocationID' + responses: + '200': + $ref: '#/components/responses/Boxes' + '403': + $ref: '#/components/responses/Error' + + /contents: + get: + summary: Contents + description: Get a list of contents. + operationId: GetContents + parameters: + - $ref: '#/components/parameters/LocationID' + - $ref: '#/components/parameters/BoxID' + responses: + '200': + $ref: '#/components/responses/Contents' + '403': + $ref: '#/components/responses/Error' + components: - schemas: - Location: - type: object - properties: - id: - type: string - description: - type: string + + parameters: + BoxID: + name: box + in: query + description: The box ID being queried. + schema: + type: string + + LocationID: + name: location + in: query + description: The location ID being queried. + schema: + type: string responses: + Contents: + description: List of contents. + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Content' + + Boxes: + description: List of boxes. + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Box' + Locations: description: List of locations. content: @@ -52,8 +104,41 @@ components: application/json: schema: type: object + required: [code, message] properties: code: type: integer message: type: string + + schemas: + Content: + type: object + required: [id, box, description] + properties: + id: + type: string + box: + type: string + description: + type: string + + Box: + type: object + required: [id, location, description] + properties: + id: + type: string + location: + type: string + description: + type: string + + Location: + type: object + required: [id, description] + properties: + id: + type: string + description: + type: string diff --git a/cmd/root.go b/cmd/root.go index 4f19587..f549d2d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,19 +29,11 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "boxes", - Short: "A brief description of your application", - Long: `A longer description that spans multiple lines and likely contains -examples and usage of using your application. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, + Short: "Track your stuff at home", + Long: `Put contents in boxes, put boxes in locations and still +find your stuff.`, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -52,15 +44,3 @@ func Execute() { os.Exit(1) } } - -func init() { - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.boxes.yaml)") - - // Cobra also supports local flags, which will only run - // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..afa7b90 --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,23 @@ +/* +Package cmd implements the commands. + +# Copyright © 2024 Kevin Lyda <kevin@lyda.ie> +*/ +package cmd + +import ( + "git.lyda.ie/kevin/boxes/server" + "github.com/spf13/cobra" +) + +var serverCmd = &cobra.Command{ + Use: "server", + Short: "Run the server", + Long: `Run the api server for tracking your stuff.`, + Run: server.Run, +} + +func init() { + rootCmd.AddCommand(serverCmd) + serverCmd.Flags().StringP("listen", "l", ":8070", "Where to listen for API requests") +} diff --git a/go.mod b/go.mod index b90e833..e060325 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,10 @@ require ( ) require ( + github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/invopop/yaml v0.3.1 // indirect github.com/josharian/intern v1.0.0 // indirect diff --git a/go.sum b/go.sum index 29b3580..f1aafec 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +1,24 @@ +github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= +github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= +github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= +github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/getkin/kin-openapi v0.128.0 h1:jqq3D9vC9pPq1dGcOCv7yOp1DaEe7c/T1vzcLbITSp4= github.com/getkin/kin-openapi v0.128.0/go.mod h1:OZrfXzUfGrNbsKj+xmFBx6E5c6yH3At/tAKSc2UszXM= github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ= github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY= github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso= github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81KkeA= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0= github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= @@ -28,11 +36,15 @@ github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmt github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg= github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s= github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= diff --git a/server/endpoints.go b/server/endpoints.go index fbc23d9..81c9512 100644 --- a/server/endpoints.go +++ b/server/endpoints.go @@ -23,3 +23,19 @@ func (ep *Endpoints) GetLocations(_ context.Context, _ api.GetLocationsRequestOb LocationsJSONResponse: loc, }, nil } + +// GetBoxes returns a list of locations. +func (ep *Endpoints) GetBoxes(_ context.Context, _ api.GetBoxesRequestObject) (api.GetBoxesResponseObject, error) { + boxes := []api.Box{} + return api.GetBoxes200JSONResponse{ + BoxesJSONResponse: boxes, + }, nil +} + +// GetContents returns a list of locations. +func (ep *Endpoints) GetContents(_ context.Context, _ api.GetContentsRequestObject) (api.GetContentsResponseObject, error) { + content := []api.Content{} + return api.GetContents200JSONResponse{ + ContentsJSONResponse: content, + }, nil +} diff --git a/server/server.go b/server/server.go index 3f09009..77b30d0 100644 --- a/server/server.go +++ b/server/server.go @@ -10,10 +10,12 @@ import ( ) // StartServer starts the metrics and API servers. -func StartServer(_ *cobra.Command, _ []string) { +func Run(cmd *cobra.Command, _ []string) { e := echo.New() endpoints := NewEndpoints() server := api.NewStrictHandler(endpoints, nil) api.RegisterHandlers(e, server) - fmt.Printf("Server exited with: %+v\n", e.Start(":8070")) + listen, err := cmd.Flags().GetString("listen") + cobra.CheckErr(err) + fmt.Printf("Server exited with: %+v\n", e.Start(listen)) } -- GitLab