Allow CORS origins

This commit is contained in:
Martin Pander
2022-11-08 11:01:31 +01:00
parent 8a78711727
commit 82f062b831
7 changed files with 36 additions and 35 deletions

View File

@ -12,6 +12,8 @@ import (
"log"
"net/http"
"github.com/gorilla/handlers"
dashapi "github.com/moustachioed/dash/backend/dashapi"
database "github.com/moustachioed/dash/backend/database"
mapping "github.com/moustachioed/dash/backend/mapping"
@ -29,8 +31,15 @@ func main() {
DefaultApiService := service.NewApiService(db, mapper)
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.Methods("GET", "POST", "DELETE", "OPTIONS")
log.Printf("Starting server.")
log.Fatal(http.ListenAndServe(":8080", router))
log.Fatal(http.ListenAndServe(":8080", cors(router)))
}

View File

@ -22,7 +22,7 @@ import (
type DefaultApiRouter interface {
DeleteJournalEntryForDate(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 {
DeleteJournalEntryForDate(context.Context, string) (ImplResponse, error)
GetJournalEntryForDate(context.Context, string) (ImplResponse, error)
WriteJournalEntryForDate(context.Context, string, JournalEntry) (ImplResponse, error)
WriteJournalEntry(context.Context, JournalEntry) (ImplResponse, error)
}

View File

@ -63,10 +63,10 @@ func (c *DefaultApiController) Routes() Routes {
c.GetJournalEntryForDate,
},
{
"WriteJournalEntryForDate",
"WriteJournalEntry",
strings.ToUpper("Post"),
"/api/v1/journal/entry/{date}",
c.WriteJournalEntryForDate,
"/api/v1/journal/entry/",
c.WriteJournalEntry,
},
}
}
@ -103,11 +103,8 @@ func (c *DefaultApiController) GetJournalEntryForDate(w http.ResponseWriter, r *
}
// WriteJournalEntryForDate -
func (c *DefaultApiController) WriteJournalEntryForDate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
dateParam := params["date"]
// WriteJournalEntry -
func (c *DefaultApiController) WriteJournalEntry(w http.ResponseWriter, r *http.Request) {
journalEntryParam := JournalEntry{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
@ -119,7 +116,7 @@ func (c *DefaultApiController) WriteJournalEntryForDate(w http.ResponseWriter, r
c.errorHandler(w, r, err, nil)
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 err != nil {
c.errorHandler(w, r, err, &result)

View File

@ -48,13 +48,13 @@ func (s *DefaultApiService) GetJournalEntryForDate(ctx context.Context, date str
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.
// WriteJournalEntry -
func (s *DefaultApiService) WriteJournalEntry(ctx context.Context, journalEntry JournalEntry) (ImplResponse, error) {
// 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.
//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")
return Response(http.StatusNotImplemented, nil), errors.New("WriteJournalEntry method not implemented")
}

View File

@ -42,7 +42,7 @@ func (s *ApiService) GetJournalEntryForDate(ctx context.Context, date string) (d
}
// 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)
s.db.WriteJournalEntry(journal)