feat:#34 refactored function to intended service, fixed docker files

This commit is contained in:
Elmar Kresse
2024-06-10 16:57:40 +02:00
parent cb76b5c188
commit 2d7701b0c9
96 changed files with 212 additions and 79 deletions

View File

@@ -24,6 +24,7 @@ import (
"htwkalender/service/db"
"htwkalender/service/ical"
"io"
"log/slog"
"net/http"
)
@@ -36,6 +37,7 @@ func addFeedRoutes(app *pocketbase.PocketBase) {
requestBody, _ := io.ReadAll(c.Request().Body)
result, err := ical.CreateIndividualFeed(requestBody, app)
if err != nil {
slog.Error("Failed to create individual feed", "error", err)
return c.JSON(http.StatusInternalServerError, "Failed to create individual feed")
}
return c.JSON(http.StatusOK, result)

View File

@@ -19,11 +19,12 @@ package main
import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/fiber/v3/middleware/logger"
"htwkalender-ical/service"
)
// main function for the ical service
// uses rest api to get the data from the backend
// uses rest api to get the data from the data-manager
// exposes rest api endpoints with fiber to serve the data for clients
func main() {
@@ -38,7 +39,9 @@ func main() {
RequestMethods: append(fiber.DefaultMethods[:], webdavRequestMethods...),
})
// Add routes to the app instance for the backend ical service
app.Use(logger.New())
// Add routes to the app instance for the data-manager ical service
service.AddFeedRoutes(app)
log.Fatal(app.Listen(":8091"))

View File

@@ -2,6 +2,7 @@ package connector
import (
"encoding/json"
"errors"
"htwkalender-ical/model"
"log/slog"
)
@@ -77,3 +78,24 @@ func parseModuleResponse(body []byte) (model.Module, error) {
return module, nil
}
func SaveFeedRecord(modules []model.FeedCollection) (string, error) {
var token string
response, err := PostRequestApi("/api/feed", modules)
if err != nil {
return "", err
}
if response.StatusCode() != 200 {
return "", errors.New("failed to save feed")
}
// parse the response body json to string
err = json.Unmarshal(response.Body(), &token)
if err != nil {
return "", err
}
return token, nil
}

View File

@@ -2,12 +2,13 @@ package connector
import (
"github.com/gofiber/fiber/v3/client"
"htwkalender-ical/model"
"time"
)
func RequestApi(path string) (*client.Response, error) {
const host = "http://127.0.0.1:8090"
var host = "http://htwkalender-backend:8090"
func RequestApi(path string) (*client.Response, error) {
cc := client.New()
cc.SetTimeout(5 * time.Second)
@@ -23,8 +24,6 @@ func RequestApi(path string) (*client.Response, error) {
func DeleteRequestApi(path string) error {
var host = "http://htwkalender-backend:8090"
cc := client.New()
cc.SetTimeout(5 * time.Second)
@@ -36,3 +35,25 @@ func DeleteRequestApi(path string) error {
return nil
}
func PostRequestApi(path string, body []model.FeedCollection) (*client.Response, error) {
cc := client.New()
cc.SetTimeout(5 * time.Second)
config := client.Config{
Body: body,
Header: map[string]string{
"Content-Type": "application/json",
"Accept": "*/*",
},
}
// set retry to 0
response, err := cc.Post(host+path, config)
if err != nil {
return nil, err
}
return response, nil
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/jordic/goics"
"htwkalender-ical/model"
"htwkalender-ical/service/connector"
"log/slog"
"time"
)
@@ -61,3 +62,15 @@ func DeleteFeedRecord(token string) error {
}
return nil
}
func CreateFeed(modules []model.FeedCollection) (string, error) {
// Save feed
token, err := connector.SaveFeedRecord(modules)
if err != nil {
slog.Error("Failed to save feed", "error", err)
return "", err
}
return token, nil
}

View File

@@ -1,13 +1,16 @@
package service
import (
"encoding/json"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"htwkalender-ical/model"
"htwkalender-ical/service/ical"
"log/slog"
"net/http"
)
// add routes to the app instance for the backend ical service
// add routes to the app instance for the data-manager ical service
// with golang fiber
func AddFeedRoutes(app *fiber.App) {
@@ -29,6 +32,27 @@ func AddFeedRoutes(app *fiber.App) {
return c.SendString(results)
})
// Define a route for the POST method on the root path '/api/feed'
app.Post("/api/feed", func(c fiber.Ctx) error {
modules := []model.FeedCollection{}
//obtain the body of the request
err := json.Unmarshal(c.Body(), &modules)
if err != nil {
log.Error("Failed to unmarshal request body", "error", err)
return c.SendStatus(fiber.StatusBadRequest)
}
//create a new feed
token, err := ical.CreateFeed(modules)
if err != nil {
println(err)
log.Error("Failed to create feed", "error", err)
return c.SendStatus(fiber.StatusInternalServerError)
}
return c.JSON(token)
})
// Define a route for the GET method on the root path '/'
app.Get("/api/collections/feeds/records/:token", func(c fiber.Ctx) error {