Implement rough architecture

This commit is contained in:
Martin Pander
2022-11-07 15:33:08 +01:00
parent 0ff700b945
commit 43c204f5b5
24 changed files with 1402 additions and 0 deletions

37
backend/dashapi/api.go Normal file
View File

@ -0,0 +1,37 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
import (
"context"
"net/http"
)
// DefaultApiRouter defines the required methods for binding the api requests to a responses for the DefaultApi
// The DefaultApiRouter implementation should parse necessary information from the http request,
// pass the data to a DefaultApiServicer to perform the required actions, then write the service results to the http response.
type DefaultApiRouter interface {
DeleteJournalEntryForDate(http.ResponseWriter, *http.Request)
GetJournalEntryForDate(http.ResponseWriter, *http.Request)
WriteJournalEntryForDate(http.ResponseWriter, *http.Request)
}
// DefaultApiServicer defines the api actions for the DefaultApi service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type DefaultApiServicer interface {
DeleteJournalEntryForDate(context.Context, string) (ImplResponse, error)
GetJournalEntryForDate(context.Context, string) (ImplResponse, error)
WriteJournalEntryForDate(context.Context, string, JournalEntry) (ImplResponse, error)
}

View File

@ -0,0 +1,131 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
)
// DefaultApiController binds http requests to an api service and writes the service results to the http response
type DefaultApiController struct {
service DefaultApiServicer
errorHandler ErrorHandler
}
// DefaultApiOption for how the controller is set up.
type DefaultApiOption func(*DefaultApiController)
// WithDefaultApiErrorHandler inject ErrorHandler into controller
func WithDefaultApiErrorHandler(h ErrorHandler) DefaultApiOption {
return func(c *DefaultApiController) {
c.errorHandler = h
}
}
// NewDefaultApiController creates a default api controller
func NewDefaultApiController(s DefaultApiServicer, opts ...DefaultApiOption) Router {
controller := &DefaultApiController{
service: s,
errorHandler: DefaultErrorHandler,
}
for _, opt := range opts {
opt(controller)
}
return controller
}
// Routes returns all the api routes for the DefaultApiController
func (c *DefaultApiController) Routes() Routes {
return Routes{
{
"DeleteJournalEntryForDate",
strings.ToUpper("Delete"),
"/api/v1/journal/entry/{date}",
c.DeleteJournalEntryForDate,
},
{
"GetJournalEntryForDate",
strings.ToUpper("Get"),
"/api/v1/journal/entry/{date}",
c.GetJournalEntryForDate,
},
{
"WriteJournalEntryForDate",
strings.ToUpper("Post"),
"/api/v1/journal/entry/{date}",
c.WriteJournalEntryForDate,
},
}
}
// DeleteJournalEntryForDate -
func (c *DefaultApiController) DeleteJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
dateParam := params["date"]
result, err := c.service.DeleteJournalEntryForDate(r.Context(), dateParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// GetJournalEntryForDate -
func (c *DefaultApiController) GetJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
dateParam := params["date"]
result, err := c.service.GetJournalEntryForDate(r.Context(), dateParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// WriteJournalEntryForDate -
func (c *DefaultApiController) WriteJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
dateParam := params["date"]
journalEntryParam := JournalEntry{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&journalEntryParam); err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if err := AssertJournalEntryRequired(journalEntryParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
result, err := c.service.WriteJournalEntryForDate(r.Context(), dateParam, journalEntryParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}

View File

@ -0,0 +1,60 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
import (
"context"
"net/http"
"errors"
)
// DefaultApiService is a service that implements the logic for the DefaultApiServicer
// This service should implement the business logic for every endpoint for the DefaultApi API.
// Include any external packages or services that will be required by this service.
type DefaultApiService struct {
}
// NewDefaultApiService creates a default api service
func NewDefaultApiService() DefaultApiServicer {
return &DefaultApiService{}
}
// DeleteJournalEntryForDate -
func (s *DefaultApiService) DeleteJournalEntryForDate(ctx context.Context, date string) (ImplResponse, error) {
// TODO - update DeleteJournalEntryForDate with the required logic for this service method.
// Add api_default_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
//TODO: Uncomment the next line to return response Response(200, {}) or use other options such as http.Ok ...
//return Response(200, nil),nil
return Response(http.StatusNotImplemented, nil), errors.New("DeleteJournalEntryForDate method not implemented")
}
// GetJournalEntryForDate -
func (s *DefaultApiService) GetJournalEntryForDate(ctx context.Context, date string) (ImplResponse, error) {
// TODO - update GetJournalEntryForDate with the required logic for this service method.
// Add api_default_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
//TODO: Uncomment the next line to return response Response(200, JournalEntry{}) or use other options such as http.Ok ...
//return Response(200, JournalEntry{}), nil
return Response(http.StatusNotImplemented, nil), errors.New("GetJournalEntryForDate method not implemented")
}
// WriteJournalEntryForDate -
func (s *DefaultApiService) WriteJournalEntryForDate(ctx context.Context, date string, journalEntry JournalEntry) (ImplResponse, error) {
// TODO - update WriteJournalEntryForDate with the required logic for this service method.
// Add api_default_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
//TODO: Uncomment the next line to return response Response(200, {}) or use other options such as http.Ok ...
//return Response(200, nil),nil
return Response(http.StatusNotImplemented, nil), errors.New("WriteJournalEntryForDate method not implemented")
}

62
backend/dashapi/error.go Normal file
View File

@ -0,0 +1,62 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
import (
"errors"
"fmt"
"net/http"
)
var (
// ErrTypeAssertionError is thrown when type an interface does not match the asserted type
ErrTypeAssertionError = errors.New("unable to assert type")
)
// ParsingError indicates that an error has occurred when parsing request parameters
type ParsingError struct {
Err error
}
func (e *ParsingError) Unwrap() error {
return e.Err
}
func (e *ParsingError) Error() string {
return e.Err.Error()
}
// RequiredError indicates that an error has occurred when parsing request parameters
type RequiredError struct {
Field string
}
func (e *RequiredError) Error() string {
return fmt.Sprintf("required field '%s' is zero value.", e.Field)
}
// ErrorHandler defines the required method for handling error. You may implement it and inject this into a controller if
// you would like errors to be handled differently from the DefaultErrorHandler
type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse)
// DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing
// request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used.
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse) {
if _, ok := err.(*ParsingError); ok {
// Handle parsing errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusBadRequest), w)
} else if _, ok := err.(*RequiredError); ok {
// Handle missing required errors
EncodeJSONResponse(err.Error(), func(i int) *int { return &i }(http.StatusUnprocessableEntity), w)
} else {
// Handle all other errors
EncodeJSONResponse(err.Error(), &result.Code, w)
}
}

View File

@ -0,0 +1,54 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
import (
"reflect"
)
// Response return a ImplResponse struct filled
func Response(code int, body interface{}) ImplResponse {
return ImplResponse {
Code: code,
Body: body,
}
}
// IsZeroValue checks if the val is the zero-ed value.
func IsZeroValue(val interface{}) bool {
return val == nil || reflect.DeepEqual(val, reflect.Zero(reflect.TypeOf(val)).Interface())
}
// AssertRecurseInterfaceRequired recursively checks each struct in a slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseInterfaceRequired(obj interface{}, callback func(interface{}) error) error {
return AssertRecurseValueRequired(reflect.ValueOf(obj), callback)
}
// AssertRecurseValueRequired checks each struct in the nested slice against the callback.
// This method traverse nested slices in a preorder fashion.
func AssertRecurseValueRequired(value reflect.Value, callback func(interface{}) error) error {
switch value.Kind() {
// If it is a struct we check using callback
case reflect.Struct:
if err := callback(value.Interface()); err != nil {
return err
}
// If it is a slice we continue recursion
case reflect.Slice:
for i := 0; i < value.Len(); i += 1 {
if err := AssertRecurseValueRequired(value.Index(i), callback); err != nil {
return err
}
}
}
return nil
}

16
backend/dashapi/impl.go Normal file
View File

@ -0,0 +1,16 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
// ImplResponse response defines an error code with the associated body
type ImplResponse struct {
Code int
Body interface{}
}

32
backend/dashapi/logger.go Normal file
View File

@ -0,0 +1,32 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
import (
"log"
"net/http"
"time"
)
func Logger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
inner.ServeHTTP(w, r)
log.Printf(
"%s %s %s %s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
})
}

View File

@ -0,0 +1,51 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
type JournalEntry struct {
Date string `json:"date"`
Thankful []string `json:"thankful,omitempty"`
LookingForward []string `json:"looking_forward,omitempty"`
BeenGreat []string `json:"been_great,omitempty"`
DoBetter []string `json:"do_better,omitempty"`
Journal string `json:"journal,omitempty"`
}
// AssertJournalEntryRequired checks if the required fields are not zero-ed
func AssertJournalEntryRequired(obj JournalEntry) error {
elements := map[string]interface{}{
"date": obj.Date,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}
return nil
}
// AssertRecurseJournalEntryRequired recursively checks if required fields are not zero-ed in a nested slice.
// Accepts only nested slice of JournalEntry (e.g. [][]JournalEntry), otherwise ErrTypeAssertionError is thrown.
func AssertRecurseJournalEntryRequired(objSlice interface{}) error {
return AssertRecurseInterfaceRequired(objSlice, func(obj interface{}) error {
aJournalEntry, ok := obj.(JournalEntry)
if !ok {
return ErrTypeAssertionError
}
return AssertJournalEntryRequired(aJournalEntry)
})
}

219
backend/dashapi/routers.go Normal file
View File

@ -0,0 +1,219 @@
/*
* Dash API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 0.1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package dashapi
import (
"encoding/json"
"errors"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"
)
// A Route defines the parameters for an api endpoint
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
// Routes are a collection of defined api endpoints
type Routes []Route
// Router defines the required methods for retrieving api routes
type Router interface {
Routes() Routes
}
const errMsgRequiredMissing = "required parameter is missing"
// NewRouter creates a new router for any number of api routers
func NewRouter(routers ...Router) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, api := range routers {
for _, route := range api.Routes() {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
handler = handlers.CORS()(handler)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
}
return router
}
// EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code
func EncodeJSONResponse(i interface{}, status *int, w http.ResponseWriter) error {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if status != nil {
w.WriteHeader(*status)
} else {
w.WriteHeader(http.StatusOK)
}
return json.NewEncoder(w).Encode(i)
}
// ReadFormFileToTempFile reads file data from a request form and writes it to a temporary file
func ReadFormFileToTempFile(r *http.Request, key string) (*os.File, error) {
_, fileHeader, err := r.FormFile(key)
if err != nil {
return nil, err
}
return readFileHeaderToTempFile(fileHeader)
}
// ReadFormFilesToTempFiles reads files array data from a request form and writes it to a temporary files
func ReadFormFilesToTempFiles(r *http.Request, key string) ([]*os.File, error) {
if err := r.ParseMultipartForm(32 << 20); err != nil {
return nil, err
}
files := make([]*os.File, 0, len(r.MultipartForm.File[key]))
for _, fileHeader := range r.MultipartForm.File[key] {
file, err := readFileHeaderToTempFile(fileHeader)
if err != nil {
return nil, err
}
files = append(files, file)
}
return files, nil
}
// readFileHeaderToTempFile reads multipart.FileHeader and writes it to a temporary file
func readFileHeaderToTempFile(fileHeader *multipart.FileHeader) (*os.File, error) {
formFile, err := fileHeader.Open()
if err != nil {
return nil, err
}
defer formFile.Close()
fileBytes, err := ioutil.ReadAll(formFile)
if err != nil {
return nil, err
}
file, err := ioutil.TempFile("", fileHeader.Filename)
if err != nil {
return nil, err
}
defer file.Close()
file.Write(fileBytes)
return file, nil
}
// parseInt64Parameter parses a string parameter to an int64.
func parseInt64Parameter(param string, required bool) (int64, error) {
if param == "" {
if required {
return 0, errors.New(errMsgRequiredMissing)
}
return 0, nil
}
return strconv.ParseInt(param, 10, 64)
}
// parseInt32Parameter parses a string parameter to an int32.
func parseInt32Parameter(param string, required bool) (int32, error) {
if param == "" {
if required {
return 0, errors.New(errMsgRequiredMissing)
}
return 0, nil
}
val, err := strconv.ParseInt(param, 10, 32)
if err != nil {
return -1, err
}
return int32(val), nil
}
// parseBoolParameter parses a string parameter to a bool
func parseBoolParameter(param string) (bool, error) {
val, err := strconv.ParseBool(param)
if err != nil {
return false, err
}
return bool(val), nil
}
// parseInt64ArrayParameter parses a string parameter containing array of values to []int64.
func parseInt64ArrayParameter(param, delim string, required bool) ([]int64, error) {
if param == "" {
if required {
return nil, errors.New(errMsgRequiredMissing)
}
return nil, nil
}
str := strings.Split(param, delim)
ints := make([]int64, len(str))
for i, s := range str {
if v, err := strconv.ParseInt(s, 10, 64); err != nil {
return nil, err
} else {
ints[i] = v
}
}
return ints, nil
}
// parseInt32ArrayParameter parses a string parameter containing array of values to []int32.
func parseInt32ArrayParameter(param, delim string, required bool) ([]int32, error) {
if param == "" {
if required {
return nil, errors.New(errMsgRequiredMissing)
}
return nil, nil
}
str := strings.Split(param, delim)
ints := make([]int32, len(str))
for i, s := range str {
if v, err := strconv.ParseInt(s, 10, 32); err != nil {
return nil, err
} else {
ints[i] = int32(v)
}
}
return ints, nil
}