style: license headers + gofmt

This commit is contained in:
2024-03-27 11:12:43 +00:00
parent e9a1653c4e
commit 4d9a7a4856
109 changed files with 2059 additions and 530 deletions

View File

@@ -1,3 +1,17 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
@@ -14,7 +28,7 @@ import (
func NewAPIApp(cfg internal.Config) *fiber.App {
app := fiber.New(fiber.Config{
AppName: "Panels REST Gateway",
AppName: "Panels REST Gateway",
ErrorHandler: handlers.ErrorHandler,
// Swap out the JSON encoder for faster marshaling
@@ -38,4 +52,4 @@ func ServeAPIApp(app *fiber.App) {
if err != nil {
panic(fmt.Sprintf("failed to serve API: %v", err))
}
}
}

View File

@@ -1,11 +1,25 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handlers
import (
"crypto/rsa"
"github.com/golang-jwt/jwt/v5"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/keyauth"
"github.com/golang-jwt/jwt/v5"
"github.com/hexolan/panels/gateway-service/internal"
)
@@ -24,7 +38,7 @@ func NewAuthMiddleware(cfg internal.Config) {
tokenValidator := tokenValidator{pubKey: cfg.JWTPubKey}
AuthMiddleware = keyauth.New(keyauth.Config{
AuthScheme: "Bearer",
Validator: tokenValidator.ValidateToken,
Validator: tokenValidator.ValidateToken,
})
}
@@ -56,4 +70,4 @@ func (tv tokenValidator) ValidateToken(c *fiber.Ctx, userToken string) (bool, er
c.Locals("tokenClaims", claims)
return true, nil
}
}

View File

@@ -1,3 +1,17 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handlers
import (
@@ -10,15 +24,15 @@ import (
)
func ErrorHandler(c *fiber.Ctx, err error) error {
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)
code := fiber.StatusInternalServerError
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)
code := fiber.StatusInternalServerError
msg := err.Error()
// Retrieval of codes from fiber.Errors
var e *fiber.Error
if errors.As(err, &e) {
code = e.Code
} else {
// Retrieval of codes from fiber.Errors
var e *fiber.Error
if errors.As(err, &e) {
code = e.Code
} else {
// Retrival of codes from gRPC errors.
status, ok := status.FromError(err)
if ok {
@@ -51,5 +65,5 @@ func ErrorHandler(c *fiber.Ctx, err error) error {
}
}
return c.Status(code).JSON(fiber.Map{"status": "failure", "msg": msg})
}
return c.Status(code).JSON(fiber.Map{"status": "failure", "msg": msg})
}

View File

@@ -1,10 +1,24 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"github.com/gofiber/fiber/v2"
"github.com/hexolan/panels/gateway-service/internal/api/v1"
"github.com/hexolan/panels/gateway-service/internal/api/handlers"
"github.com/hexolan/panels/gateway-service/internal/api/v1"
)
func RegisterRoutes(app *fiber.App) {
@@ -36,10 +50,10 @@ func RegisterRoutes(app *fiber.App) {
panelV1.Get("/id/:panel_id/posts", v1.GetPanelPostsFromId)
panelV1.Get("/name/:panel_name/posts", v1.GetPanelPostsFromName)
panelV1.Get("/id/:panel_id/posts/:id", v1.GetPanelPostFromId)
panelV1.Get("/name/:panel_name/posts/:id", v1.GetPanelPostFromName)
panelV1.Post("/id/:panel_id", handlers.AuthMiddleware, v1.CreatePanelPostFromId)
panelV1.Post("/name/:panel_name", handlers.AuthMiddleware, v1.CreatePanelPostFromName)
@@ -51,10 +65,10 @@ func RegisterRoutes(app *fiber.App) {
userV1.Get("/username/:username", v1.GetUserByUsername)
userV1.Delete("/username/:username", handlers.AuthMiddleware, v1.DeleteUserByUsername)
userV1.Get("/me", handlers.AuthMiddleware, v1.GetCurrentUser)
userV1.Delete("/me", handlers.AuthMiddleware, v1.DeleteCurrentUser)
// Auth Service Routes
authV1.Post("/login", v1.LoginWithPassword)
@@ -63,4 +77,4 @@ func RegisterRoutes(app *fiber.App) {
commentV1.Post("/", handlers.AuthMiddleware, v1.CreateComment)
commentV1.Patch("/:id", handlers.AuthMiddleware, v1.UpdateComment)
commentV1.Delete("/:id", handlers.AuthMiddleware, v1.DeleteComment)
}
}

View File

@@ -1,8 +1,22 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1
import (
"time"
"context"
"time"
"github.com/gofiber/fiber/v2"
@@ -21,7 +35,7 @@ func setAuthMethod(userId string, password string) error {
_, err := rpc.Svcs.GetAuthSvc().SetPasswordAuth(
ctx,
&authv1.SetPasswordAuthMethod{
UserId: userId,
UserId: userId,
Password: password,
},
)
@@ -34,7 +48,7 @@ func authWithPassword(userId string, password string) (*authv1.AuthToken, error)
token, err := rpc.Svcs.GetAuthSvc().AuthWithPassword(
ctx,
&authv1.PasswordAuthRequest{
UserId: userId,
UserId: userId,
Password: password,
},
)
@@ -63,8 +77,8 @@ func LoginWithPassword(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"status": "success",
"data": fiber.Map{
"user": user,
"user": user,
"token": token,
},
})
}
}

View File

@@ -1,3 +1,17 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1
import (
@@ -48,7 +62,7 @@ func UpdateComment(c *fiber.Ctx) error {
return err
}
if (comment.AuthorId != currentUser.Id) {
if comment.AuthorId != currentUser.Id {
return fiber.NewError(fiber.StatusForbidden, "no permissions to update that comment")
}
@@ -64,7 +78,7 @@ func UpdateComment(c *fiber.Ctx) error {
comment, err = rpc.Svcs.GetCommentSvc().UpdateComment(
ctx,
&commentv1.UpdateCommentRequest{
Id: c.Params("id"),
Id: c.Params("id"),
Data: updatedComment,
},
)
@@ -87,7 +101,7 @@ func DeleteComment(c *fiber.Ctx) error {
return err
}
if (comment.AuthorId != currentUser.Id && !currentUser.IsAdmin) {
if comment.AuthorId != currentUser.Id && !currentUser.IsAdmin {
return fiber.NewError(fiber.StatusForbidden, "no permissions to delete that comment")
}
@@ -123,16 +137,16 @@ func CreateComment(c *fiber.Ctx) error {
if err != nil {
return err
}
// Create the comment
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
comment, err := rpc.Svcs.GetCommentSvc().CreateComment(
ctx,
&commentv1.CreateCommentRequest{
PostId: post.Id,
PostId: post.Id,
AuthorId: tokenClaims.Subject,
Data: newComment,
Data: newComment,
},
)
if err != nil {
@@ -140,4 +154,4 @@ func CreateComment(c *fiber.Ctx) error {
}
return c.JSON(fiber.Map{"status": "success", "data": comment})
}
}

View File

@@ -1,8 +1,22 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1
import (
"time"
"context"
"time"
"github.com/gofiber/fiber/v2"
@@ -29,7 +43,7 @@ func getPanelIDFromName(name string) (string, error) {
if err != nil {
return "", err
}
return panel.GetId(), nil
}
@@ -76,7 +90,7 @@ func UpdatePanelById(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
panel, err := rpc.Svcs.GetPanelSvc().UpdatePanel(
ctx,
ctx,
&panelv1.UpdatePanelByIdRequest{Id: c.Params("id"), Data: patchData},
)
if err != nil {
@@ -106,7 +120,7 @@ func UpdatePanelByName(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
panel, err := rpc.Svcs.GetPanelSvc().UpdatePanelByName(
ctx,
ctx,
&panelv1.UpdatePanelByNameRequest{Name: c.Params("name"), Data: patchData},
)
if err != nil {
@@ -183,4 +197,4 @@ func CreatePanel(c *fiber.Ctx) error {
}
return c.JSON(fiber.Map{"status": "success", "data": panel})
}
}

View File

@@ -1,14 +1,28 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1
import (
"time"
"context"
"time"
"github.com/gofiber/fiber/v2"
"github.com/hexolan/panels/gateway-service/internal/api/handlers"
"github.com/hexolan/panels/gateway-service/internal/rpc"
"github.com/hexolan/panels/gateway-service/internal/rpc/postv1"
"github.com/hexolan/panels/gateway-service/internal/api/handlers"
)
func getPostById(postId string) (*postv1.Post, error) {
@@ -155,7 +169,7 @@ func UpdatePost(c *fiber.Ctx) error {
return err
}
if (post.AuthorId != currentUser.Id) {
if post.AuthorId != currentUser.Id {
return fiber.NewError(fiber.StatusForbidden, "no permissions to update that post")
}
@@ -191,10 +205,10 @@ func DeletePost(c *fiber.Ctx) error {
return err
}
if (post.AuthorId != currentUser.Id && !currentUser.IsAdmin) {
if post.AuthorId != currentUser.Id && !currentUser.IsAdmin {
return fiber.NewError(fiber.StatusForbidden, "no permissions to delete that post")
}
// delete the post
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
@@ -235,8 +249,8 @@ func CreatePanelPostFromId(c *fiber.Ctx) error {
ctx,
&postv1.CreatePostRequest{
PanelId: panel.Id,
UserId: tokenClaims.Subject,
Data: newPost,
UserId: tokenClaims.Subject,
Data: newPost,
},
)
if err != nil {
@@ -272,8 +286,8 @@ func CreatePanelPostFromName(c *fiber.Ctx) error {
ctx,
&postv1.CreatePostRequest{
PanelId: panelId,
UserId: tokenClaims.Subject,
Data: newPost,
UserId: tokenClaims.Subject,
Data: newPost,
},
)
if err != nil {
@@ -281,4 +295,4 @@ func CreatePanelPostFromName(c *fiber.Ctx) error {
}
return c.JSON(fiber.Map{"status": "success", "data": post})
}
}

View File

@@ -1,14 +1,28 @@
// Copyright 2023 Declan Teevan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1
import (
"time"
"context"
"time"
"github.com/gofiber/fiber/v2"
"github.com/hexolan/panels/gateway-service/internal/api/handlers"
"github.com/hexolan/panels/gateway-service/internal/rpc"
"github.com/hexolan/panels/gateway-service/internal/rpc/userv1"
"github.com/hexolan/panels/gateway-service/internal/api/handlers"
)
type userSignupForm struct {
@@ -87,7 +101,7 @@ func DeleteUserById(c *fiber.Ctx) error {
if currentUser.Id != c.Params("id") && !currentUser.IsAdmin {
return fiber.NewError(fiber.StatusForbidden, "no permissions to delete that user")
}
// delete the user
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
@@ -113,7 +127,7 @@ func DeleteUserByUsername(c *fiber.Ctx) error {
if currentUser.Id != c.Params("id") && !currentUser.IsAdmin {
return fiber.NewError(fiber.StatusForbidden, "no permissions to delete that user")
}
// delete the user
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
@@ -155,7 +169,7 @@ func UserSignup(c *fiber.Ctx) error {
if err := c.BodyParser(form); err != nil {
fiber.NewError(fiber.StatusBadRequest, "malformed request")
}
// Attempt to create the user
// todo: defer this logic away from gateway-service in future (potentially into seperate registration-service)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
@@ -189,8 +203,8 @@ func UserSignup(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"status": "success",
"data": fiber.Map{
"user": user,
"user": user,
"token": token,
},
})
}
}