Allow CORS origins
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
api/api_server/
|
api/api_server/
|
||||||
|
api/api_client/
|
||||||
.vscode
|
.vscode
|
||||||
__debug_bin
|
__debug_bin
|
||||||
@ -7,6 +7,18 @@ servers:
|
|||||||
- url: http://localhost:8080/api/v1
|
- url: http://localhost:8080/api/v1
|
||||||
- url: https://dash.pander.me/api/v1
|
- url: https://dash.pander.me/api/v1
|
||||||
paths:
|
paths:
|
||||||
|
/journal/entry/:
|
||||||
|
post:
|
||||||
|
operationId: writeJournalEntry
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/JournalEntry'
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Journal entry successfully written.
|
||||||
/journal/entry/{date}:
|
/journal/entry/{date}:
|
||||||
get:
|
get:
|
||||||
operationId: getJournalEntryForDate
|
operationId: getJournalEntryForDate
|
||||||
@ -25,24 +37,6 @@ paths:
|
|||||||
type: string
|
type: string
|
||||||
format: date
|
format: date
|
||||||
example: 2022-02-18
|
example: 2022-02-18
|
||||||
post:
|
|
||||||
operationId: writeJournalEntryForDate
|
|
||||||
requestBody:
|
|
||||||
content:
|
|
||||||
application/json:
|
|
||||||
schema:
|
|
||||||
$ref: '#/components/schemas/JournalEntry'
|
|
||||||
responses:
|
|
||||||
'200':
|
|
||||||
description: Journal entry successfully written.
|
|
||||||
parameters:
|
|
||||||
- name: date
|
|
||||||
in: path
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
type: string
|
|
||||||
format: date
|
|
||||||
example: 2022-02-18
|
|
||||||
delete:
|
delete:
|
||||||
operationId: deleteJournalEntryForDate
|
operationId: deleteJournalEntryForDate
|
||||||
responses:
|
responses:
|
||||||
|
|||||||
@ -12,6 +12,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/handlers"
|
||||||
|
|
||||||
dashapi "github.com/moustachioed/dash/backend/dashapi"
|
dashapi "github.com/moustachioed/dash/backend/dashapi"
|
||||||
database "github.com/moustachioed/dash/backend/database"
|
database "github.com/moustachioed/dash/backend/database"
|
||||||
mapping "github.com/moustachioed/dash/backend/mapping"
|
mapping "github.com/moustachioed/dash/backend/mapping"
|
||||||
@ -29,8 +31,15 @@ func main() {
|
|||||||
DefaultApiService := service.NewApiService(db, mapper)
|
DefaultApiService := service.NewApiService(db, mapper)
|
||||||
DefaultApiController := dashapi.NewDefaultApiController(DefaultApiService)
|
DefaultApiController := dashapi.NewDefaultApiController(DefaultApiService)
|
||||||
|
|
||||||
|
cors := handlers.CORS(
|
||||||
|
// handlers.AllowedMethods([]string{"GET", "POST", "DELETE"}),
|
||||||
|
handlers.AllowedHeaders([]string{"Accept", "Accept-Language", "Content-Type", "Content-Language", "Origin"}),
|
||||||
|
handlers.AllowedOrigins([]string{"*"}),
|
||||||
|
)
|
||||||
|
|
||||||
router := dashapi.NewRouter(DefaultApiController)
|
router := dashapi.NewRouter(DefaultApiController)
|
||||||
|
router.Methods("GET", "POST", "DELETE", "OPTIONS")
|
||||||
|
|
||||||
log.Printf("Starting server.")
|
log.Printf("Starting server.")
|
||||||
log.Fatal(http.ListenAndServe(":8080", router))
|
log.Fatal(http.ListenAndServe(":8080", cors(router)))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,7 +22,7 @@ import (
|
|||||||
type DefaultApiRouter interface {
|
type DefaultApiRouter interface {
|
||||||
DeleteJournalEntryForDate(http.ResponseWriter, *http.Request)
|
DeleteJournalEntryForDate(http.ResponseWriter, *http.Request)
|
||||||
GetJournalEntryForDate(http.ResponseWriter, *http.Request)
|
GetJournalEntryForDate(http.ResponseWriter, *http.Request)
|
||||||
WriteJournalEntryForDate(http.ResponseWriter, *http.Request)
|
WriteJournalEntry(http.ResponseWriter, *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -33,5 +33,5 @@ type DefaultApiRouter interface {
|
|||||||
type DefaultApiServicer interface {
|
type DefaultApiServicer interface {
|
||||||
DeleteJournalEntryForDate(context.Context, string) (ImplResponse, error)
|
DeleteJournalEntryForDate(context.Context, string) (ImplResponse, error)
|
||||||
GetJournalEntryForDate(context.Context, string) (ImplResponse, error)
|
GetJournalEntryForDate(context.Context, string) (ImplResponse, error)
|
||||||
WriteJournalEntryForDate(context.Context, string, JournalEntry) (ImplResponse, error)
|
WriteJournalEntry(context.Context, JournalEntry) (ImplResponse, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,10 +63,10 @@ func (c *DefaultApiController) Routes() Routes {
|
|||||||
c.GetJournalEntryForDate,
|
c.GetJournalEntryForDate,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"WriteJournalEntryForDate",
|
"WriteJournalEntry",
|
||||||
strings.ToUpper("Post"),
|
strings.ToUpper("Post"),
|
||||||
"/api/v1/journal/entry/{date}",
|
"/api/v1/journal/entry/",
|
||||||
c.WriteJournalEntryForDate,
|
c.WriteJournalEntry,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,11 +103,8 @@ func (c *DefaultApiController) GetJournalEntryForDate(w http.ResponseWriter, r *
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteJournalEntryForDate -
|
// WriteJournalEntry -
|
||||||
func (c *DefaultApiController) WriteJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
|
func (c *DefaultApiController) WriteJournalEntry(w http.ResponseWriter, r *http.Request) {
|
||||||
params := mux.Vars(r)
|
|
||||||
dateParam := params["date"]
|
|
||||||
|
|
||||||
journalEntryParam := JournalEntry{}
|
journalEntryParam := JournalEntry{}
|
||||||
d := json.NewDecoder(r.Body)
|
d := json.NewDecoder(r.Body)
|
||||||
d.DisallowUnknownFields()
|
d.DisallowUnknownFields()
|
||||||
@ -119,7 +116,7 @@ func (c *DefaultApiController) WriteJournalEntryForDate(w http.ResponseWriter, r
|
|||||||
c.errorHandler(w, r, err, nil)
|
c.errorHandler(w, r, err, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
result, err := c.service.WriteJournalEntryForDate(r.Context(), dateParam, journalEntryParam)
|
result, err := c.service.WriteJournalEntry(r.Context(), journalEntryParam)
|
||||||
// If an error occurred, encode the error with the status code
|
// If an error occurred, encode the error with the status code
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.errorHandler(w, r, err, &result)
|
c.errorHandler(w, r, err, &result)
|
||||||
|
|||||||
@ -48,13 +48,13 @@ func (s *DefaultApiService) GetJournalEntryForDate(ctx context.Context, date str
|
|||||||
return Response(http.StatusNotImplemented, nil), errors.New("GetJournalEntryForDate method not implemented")
|
return Response(http.StatusNotImplemented, nil), errors.New("GetJournalEntryForDate method not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteJournalEntryForDate -
|
// WriteJournalEntry -
|
||||||
func (s *DefaultApiService) WriteJournalEntryForDate(ctx context.Context, date string, journalEntry JournalEntry) (ImplResponse, error) {
|
func (s *DefaultApiService) WriteJournalEntry(ctx context.Context, journalEntry JournalEntry) (ImplResponse, error) {
|
||||||
// TODO - update WriteJournalEntryForDate with the required logic for this service method.
|
// TODO - update WriteJournalEntry 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.
|
// 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 ...
|
//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(200, nil),nil
|
||||||
|
|
||||||
return Response(http.StatusNotImplemented, nil), errors.New("WriteJournalEntryForDate method not implemented")
|
return Response(http.StatusNotImplemented, nil), errors.New("WriteJournalEntry method not implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -42,7 +42,7 @@ func (s *ApiService) GetJournalEntryForDate(ctx context.Context, date string) (d
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WriteJournalEntryForDate -
|
// WriteJournalEntryForDate -
|
||||||
func (s *ApiService) WriteJournalEntryForDate(ctx context.Context, date string, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) {
|
func (s *ApiService) WriteJournalEntry(ctx context.Context, journalEntry dashapi.JournalEntry) (dashapi.ImplResponse, error) {
|
||||||
journal := s.mapper.JournalApiToDb(journalEntry)
|
journal := s.mapper.JournalApiToDb(journalEntry)
|
||||||
s.db.WriteJournalEntry(journal)
|
s.db.WriteJournalEntry(journal)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user