-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add simple versioning support (#22)
- Loading branch information
1 parent
3763605
commit 1e20100
Showing
18 changed files
with
136 additions
and
50 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package api | ||
package v1 | ||
|
||
import ( | ||
"github.com/glass-cms/glasscms/item" | ||
|
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,21 @@ | ||
# Code Generation | ||
|
||
This project uses [TypeSpec](https://typespec.io/) for describing the API of the application. The TypeSpec is compiled in an OpenAPI 3.0 specification file, which is then used to generate the client and server code. | ||
|
||
## Generating the OpenAPI specification | ||
|
||
The TypeSpec files are located in the `typespec` directory. To compile the OpenAPI specification, run the following command: | ||
|
||
```bash | ||
make typespec | ||
``` | ||
|
||
The OpenAPI specification will be generated in the `openapi` directory. | ||
|
||
## Generating the client and server code | ||
|
||
The client and server code is generated using the [oapi-codegen](https://github.com/oapi-codegen/oapi-codegen) module. To generate the code, run the following command: | ||
|
||
```bash | ||
go generate ./... | ||
``` |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package main | ||
|
||
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=codegen-server.yaml openapi.yaml | ||
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=openapi/codegen.cfg.v1.yaml openapi/openapi.v1.yaml |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package: api | ||
package: v1 | ||
generate: | ||
std-http-server: true | ||
models: true | ||
output: api/api.gen.go | ||
output: api/v1/api.gen.go |
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,9 @@ | ||
package handler | ||
|
||
import "net/http" | ||
|
||
// VersionedHandler is an interface that defines an HTTP handler. | ||
type VersionedHandler interface { | ||
// HttpHandler returns an http.Handler that implements the API for a specific version. | ||
Handler(baseRouter *http.ServeMux, middlewares []func(http.Handler) http.Handler) http.Handler | ||
} |
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,17 @@ | ||
package v1 | ||
|
||
import "net/http" | ||
|
||
func (s *APIHandler) ItemsDelete(w http.ResponseWriter, _ *http.Request) { | ||
// TODO. | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} | ||
|
||
func (s *APIHandler) ItemsList(w http.ResponseWriter, _ *http.Request) { | ||
// TODO. | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} | ||
|
||
func (s *APIHandler) ItemsCreate(w http.ResponseWriter, _ *http.Request) { | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} |
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,47 @@ | ||
// Package v1 implements the API handlers for the v1 version of the Glass CMS API. | ||
package v1 | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
|
||
v1 "github.com/glass-cms/glasscms/api/v1" | ||
"github.com/glass-cms/glasscms/item" | ||
"github.com/glass-cms/glasscms/server/handler" | ||
) | ||
|
||
type APIHandler struct { | ||
logger *slog.Logger | ||
repository *item.Repository | ||
} | ||
|
||
// NewAPIHandler returns a new instance of ApiHandler. | ||
func NewAPIHandler( | ||
logger *slog.Logger, | ||
repo *item.Repository, | ||
) *APIHandler { | ||
return &APIHandler{ | ||
logger: logger, | ||
repository: repo, | ||
} | ||
} | ||
|
||
// Handler returns an http.Handler that implements the API. | ||
func (s *APIHandler) Handler( | ||
baseRouter *http.ServeMux, | ||
middlewares []func(http.Handler) http.Handler, | ||
) http.Handler { | ||
convertedMiddlewares := make([]v1.MiddlewareFunc, len(middlewares)) | ||
for i, mw := range middlewares { | ||
convertedMiddlewares[i] = v1.MiddlewareFunc(mw) | ||
} | ||
|
||
return v1.HandlerWithOptions(s, v1.StdHTTPServerOptions{ | ||
BaseURL: "/v1", | ||
BaseRouter: baseRouter, | ||
Middlewares: convertedMiddlewares, | ||
}) | ||
} | ||
|
||
var _ v1.ServerInterface = (*APIHandler)(nil) | ||
var _ handler.VersionedHandler = (*APIHandler)(nil) |
This file was deleted.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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