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 main
import (
@@ -6,9 +20,9 @@ import (
"github.com/rs/zerolog"
"github.com/hexolan/panels/panel-service/internal"
"github.com/hexolan/panels/panel-service/internal/kafka"
"github.com/hexolan/panels/panel-service/internal/postgres"
"github.com/hexolan/panels/panel-service/internal/redis"
"github.com/hexolan/panels/panel-service/internal/kafka"
"github.com/hexolan/panels/panel-service/internal/rpc"
"github.com/hexolan/panels/panel-service/internal/service"
)
@@ -34,4 +48,4 @@ func main() {
// Create and serve RPC
rpcServer := rpc.NewRPCServer(service)
rpcServer.Serve()
}
}

View File

@@ -1,12 +1,27 @@
//go:build tools
// 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 main
import (
// Protobuf Generation
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
_ "google.golang.org/grpc/cmd/protoc-gen-go-grpc"
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
// DB Migrations
_ "github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
)
_ "github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
)

View File

@@ -1,15 +1,29 @@
// 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 internal
import (
"os"
"fmt"
"os"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func NewConfig() Config {
func NewConfig() Config {
// Parse the log level
logLvl, err := zerolog.ParseLevel(optFromEnvFallback("LOG_LEVEL", "info"))
if err != nil {
@@ -24,10 +38,10 @@ func NewConfig() Config {
// Create the config
cfg := Config{
RedisHost: optFromEnvRequire("REDIS_HOST"),
RedisPass: optFromEnvRequire("REDIS_PASS"),
RedisHost: optFromEnvRequire("REDIS_HOST"),
RedisPass: optFromEnvRequire("REDIS_PASS"),
KafkaBrokers: kafkaBrokers,
LogLevel: logLvl,
LogLevel: logLvl,
}
// Assemble the Config.PostgresURL
@@ -86,4 +100,4 @@ func (cfg Config) GetPostgresURL() string {
func (cfg Config) GetLogLevel() zerolog.Level {
return cfg.LogLevel
}
}

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 internal
import (
@@ -10,7 +24,7 @@ import (
func NewServiceError(code ErrorCode, msg string) error {
return &ServiceError{
code: code,
msg: msg,
msg: msg,
}
}
@@ -20,8 +34,8 @@ func NewServiceErrorf(code ErrorCode, msg string, args ...interface{}) error {
func WrapServiceError(original_err error, code ErrorCode, msg string) error {
return &ServiceError{
code: code,
msg: msg,
code: code,
msg: msg,
original_err: original_err,
}
}
@@ -39,12 +53,12 @@ const (
func (c ErrorCode) GRPCCode() codes.Code {
codeMap := map[ErrorCode]codes.Code{
UnknownErrorCode: codes.Unknown,
NotFoundErrorCode: codes.NotFound,
ConflictErrorCode: codes.AlreadyExists,
ForbiddenErrorCode: codes.PermissionDenied,
UnknownErrorCode: codes.Unknown,
NotFoundErrorCode: codes.NotFound,
ConflictErrorCode: codes.AlreadyExists,
ForbiddenErrorCode: codes.PermissionDenied,
InvalidArgumentErrorCode: codes.InvalidArgument,
ConnectionErrorCode: codes.Unavailable,
ConnectionErrorCode: codes.Unavailable,
}
grpcCode, mapped := codeMap[c]
@@ -55,8 +69,8 @@ func (c ErrorCode) GRPCCode() codes.Code {
}
type ServiceError struct {
code ErrorCode
msg string
code ErrorCode
msg string
original_err error
}
@@ -77,4 +91,4 @@ func (e ServiceError) GRPCStatus() *status.Status {
func (e ServiceError) Unwrap() error {
return e.original_err
}
}

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 kafka
import (
@@ -17,8 +31,8 @@ type PanelEventProducer struct {
func NewPanelEventProducer(cfg internal.Config) PanelEventProducer {
writer := &kafka.Writer{
Addr: kafka.TCP(cfg.KafkaBrokers...),
Topic: "panel",
Addr: kafka.TCP(cfg.KafkaBrokers...),
Topic: "panel",
Balancer: &kafka.LeastBytes{},
}
@@ -59,4 +73,4 @@ func (ep PanelEventProducer) DispatchDeletedEvent(id int64) {
Type: "deleted",
Data: &panelv1.Panel{Id: internal.StringifyPanelId(id)},
})
}
}

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 internal
import (
@@ -13,9 +27,9 @@ import (
type Panel struct {
Id int64 `json:"id"`
Name string `json:"name"`
Name string `json:"name"`
Description string `json:"description"`
CreatedAt pgtype.Timestamp `json:"created_at"`
UpdatedAt pgtype.Timestamp `json:"updated_at"`
}
@@ -34,7 +48,7 @@ func DestringifyPanelId(reprId string) (int64, error) {
// Model for creating panels
type PanelCreate struct {
Name string `json:"name"`
Name string `json:"name"`
Description string `json:"description"`
}
@@ -48,7 +62,7 @@ func (p *PanelCreate) Validate() error {
// Model for updating a panel
type PanelUpdate struct {
Name *string `json:"name,omitempty"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
}
@@ -75,4 +89,4 @@ type PanelRepository interface {
GetPanelIdFromName(ctx context.Context, name string) (*int64, error)
UpdatePanel(ctx context.Context, id int64, data PanelUpdate) (*Panel, error)
DeletePanel(ctx context.Context, id int64) error
}
}

View File

@@ -1,18 +1,32 @@
// 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 postgres
import (
"context"
"encoding/json"
"errors"
"strings"
"encoding/json"
"github.com/rs/zerolog/log"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/rs/zerolog/log"
"github.com/hexolan/panels/panel-service/internal"
)
@@ -40,7 +54,7 @@ func (r panelDatabaseRepo) transformToPatchData(data internal.PanelUpdate) goqu.
func (r panelDatabaseRepo) GetPanelIdFromName(ctx context.Context, name string) (*int64, error) {
var id int64
err := r.db.QueryRow(ctx, "SELECT id FROM panels WHERE LOWER(name)=LOWER($1)", name).Scan(&id)
if err != nil {
if err != nil {
if err == pgx.ErrNoRows {
return nil, internal.WrapServiceError(err, internal.NotFoundErrorCode, "panel not found")
} else if strings.Contains(err.Error(), "failed to connect to") {
@@ -76,7 +90,7 @@ func (r panelDatabaseRepo) GetPanel(ctx context.Context, id int64) (*internal.Pa
var panel internal.Panel
row := r.db.QueryRow(ctx, "SELECT id, name, description, created_at, updated_at FROM panels WHERE id=$1", id)
err := row.Scan(&panel.Id, &panel.Name, &panel.Description, &panel.CreatedAt, &panel.UpdatedAt)
if err != nil {
if err != nil {
if err == pgx.ErrNoRows {
return nil, internal.WrapServiceError(err, internal.NotFoundErrorCode, "panel not found")
} else if strings.Contains(err.Error(), "failed to connect to") {
@@ -137,4 +151,4 @@ func (r panelDatabaseRepo) DeletePanel(ctx context.Context, id int64) error {
}
return nil
}
}

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 postgres
import (
"context"
"github.com/rs/zerolog/log"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/rs/zerolog/log"
"github.com/hexolan/panels/panel-service/internal"
)
@@ -14,11 +28,11 @@ func NewPostgresInterface(ctx context.Context, cfg internal.Config) *pgxpool.Poo
if err != nil {
log.Panic().Err(err).Caller().Msg("")
}
err = db.Ping(ctx)
if err != nil {
log.Warn().Err(err).Msg("failed Postgres ping")
}
return db
}
}

View File

@@ -1,12 +1,26 @@
// 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 redis
import (
"time"
"context"
"encoding/json"
"time"
"github.com/rs/zerolog/log"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
"github.com/hexolan/panels/panel-service/internal"
)
@@ -19,7 +33,7 @@ type panelCacheRepo struct {
func NewPanelRepository(rdb *redis.Client, repo internal.PanelRepository) internal.PanelRepository {
return panelCacheRepo{
rdb: rdb,
rdb: rdb,
repo: repo,
}
}
@@ -57,7 +71,7 @@ func (r panelCacheRepo) cachePanel(ctx context.Context, panel *internal.Panel) {
return
}
err = r.rdb.Set(ctx, internal.StringifyPanelId(panel.Id), string(value), 5 * time.Minute).Err()
err = r.rdb.Set(ctx, internal.StringifyPanelId(panel.Id), string(value), 5*time.Minute).Err()
if err != nil {
log.Error().Err(err).Msg("failed to cache panel")
return
@@ -120,4 +134,4 @@ func (r panelCacheRepo) DeletePanel(ctx context.Context, id int64) error {
// Purge any cached version of the panel.
r.purgeCachedPanel(ctx, id)
return err
}
}

View File

@@ -1,24 +1,38 @@
// 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 redis
import (
"time"
"context"
"time"
"github.com/rs/zerolog/log"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
"github.com/hexolan/panels/panel-service/internal"
)
func NewRedisInterface(ctx context.Context, cfg internal.Config) *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: cfg.RedisHost,
Password: cfg.RedisPass,
DB: 0,
rdb := redis.NewClient(&redis.Options{
Addr: cfg.RedisHost,
Password: cfg.RedisPass,
DB: 0,
DialTimeout: time.Millisecond * 250,
ReadTimeout: time.Millisecond * 500,
})
})
_, err := rdb.Ping(ctx).Result()
if err != nil {
@@ -26,4 +40,4 @@ func NewRedisInterface(ctx context.Context, cfg internal.Config) *redis.Client {
}
return rdb
}
}

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 rpc
import (
@@ -13,7 +27,7 @@ import (
type panelServer struct {
pb.UnimplementedPanelServiceServer
service internal.PanelService
}
@@ -29,7 +43,7 @@ func (svr *panelServer) CreatePanel(ctx context.Context, request *pb.CreatePanel
// Convert to business model
data := pb.PanelCreateFromProto(request.GetData())
// Attempt to create the panel
panel, err := svr.service.CreatePanel(ctx, data)
if err != nil {
@@ -159,4 +173,4 @@ func (svr *panelServer) DeletePanelByName(ctx context.Context, request *pb.Delet
return nil, err
}
return &emptypb.Empty{}, nil
}
}

View File

@@ -11,10 +11,10 @@ import (
// Panel -> Protobuf 'Panel'
func PanelToProto(panel *internal.Panel) *Panel {
proto := Panel{
Id: internal.StringifyPanelId(panel.Id),
Name: panel.Name,
Id: internal.StringifyPanelId(panel.Id),
Name: panel.Name,
Description: panel.Description,
CreatedAt: timestamppb.New(panel.CreatedAt.Time),
CreatedAt: timestamppb.New(panel.CreatedAt.Time),
}
// convert nullable attributes to PB form (if present)
@@ -44,7 +44,7 @@ func PanelFromProto(proto *Panel) (*internal.Panel, error) {
// Protobuf 'PanelMutable' -> PanelCreate
func PanelCreateFromProto(proto *PanelMutable) internal.PanelCreate {
return internal.PanelCreate{
Name: proto.GetName(),
Name: proto.GetName(),
Description: proto.GetDescription(),
}
}
@@ -52,7 +52,7 @@ func PanelCreateFromProto(proto *PanelMutable) internal.PanelCreate {
// Protobuf 'PanelMutable' -> PanelUpdate
func PanelUpdateFromProto(proto *PanelMutable) internal.PanelUpdate {
return internal.PanelUpdate{
Name: proto.Name,
Name: proto.Name,
Description: proto.Description,
}
}
}

View File

@@ -1,6 +1,20 @@
// 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.
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc-gen-go v1.31.0
// protoc v4.23.4
// source: panel.proto

View File

@@ -1,6 +1,20 @@
// 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.
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.2.0
// - protoc-gen-go-grpc v1.3.0
// - protoc v4.23.4
// source: panel.proto
@@ -19,6 +33,16 @@ import (
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
PanelService_CreatePanel_FullMethodName = "/panels.panel.v1.PanelService/CreatePanel"
PanelService_GetPanel_FullMethodName = "/panels.panel.v1.PanelService/GetPanel"
PanelService_GetPanelByName_FullMethodName = "/panels.panel.v1.PanelService/GetPanelByName"
PanelService_UpdatePanel_FullMethodName = "/panels.panel.v1.PanelService/UpdatePanel"
PanelService_UpdatePanelByName_FullMethodName = "/panels.panel.v1.PanelService/UpdatePanelByName"
PanelService_DeletePanel_FullMethodName = "/panels.panel.v1.PanelService/DeletePanel"
PanelService_DeletePanelByName_FullMethodName = "/panels.panel.v1.PanelService/DeletePanelByName"
)
// PanelServiceClient is the client API for PanelService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
@@ -42,7 +66,7 @@ func NewPanelServiceClient(cc grpc.ClientConnInterface) PanelServiceClient {
func (c *panelServiceClient) CreatePanel(ctx context.Context, in *CreatePanelRequest, opts ...grpc.CallOption) (*Panel, error) {
out := new(Panel)
err := c.cc.Invoke(ctx, "/panels.panel.v1.PanelService/CreatePanel", in, out, opts...)
err := c.cc.Invoke(ctx, PanelService_CreatePanel_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -51,7 +75,7 @@ func (c *panelServiceClient) CreatePanel(ctx context.Context, in *CreatePanelReq
func (c *panelServiceClient) GetPanel(ctx context.Context, in *GetPanelByIdRequest, opts ...grpc.CallOption) (*Panel, error) {
out := new(Panel)
err := c.cc.Invoke(ctx, "/panels.panel.v1.PanelService/GetPanel", in, out, opts...)
err := c.cc.Invoke(ctx, PanelService_GetPanel_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -60,7 +84,7 @@ func (c *panelServiceClient) GetPanel(ctx context.Context, in *GetPanelByIdReque
func (c *panelServiceClient) GetPanelByName(ctx context.Context, in *GetPanelByNameRequest, opts ...grpc.CallOption) (*Panel, error) {
out := new(Panel)
err := c.cc.Invoke(ctx, "/panels.panel.v1.PanelService/GetPanelByName", in, out, opts...)
err := c.cc.Invoke(ctx, PanelService_GetPanelByName_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -69,7 +93,7 @@ func (c *panelServiceClient) GetPanelByName(ctx context.Context, in *GetPanelByN
func (c *panelServiceClient) UpdatePanel(ctx context.Context, in *UpdatePanelByIdRequest, opts ...grpc.CallOption) (*Panel, error) {
out := new(Panel)
err := c.cc.Invoke(ctx, "/panels.panel.v1.PanelService/UpdatePanel", in, out, opts...)
err := c.cc.Invoke(ctx, PanelService_UpdatePanel_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -78,7 +102,7 @@ func (c *panelServiceClient) UpdatePanel(ctx context.Context, in *UpdatePanelByI
func (c *panelServiceClient) UpdatePanelByName(ctx context.Context, in *UpdatePanelByNameRequest, opts ...grpc.CallOption) (*Panel, error) {
out := new(Panel)
err := c.cc.Invoke(ctx, "/panels.panel.v1.PanelService/UpdatePanelByName", in, out, opts...)
err := c.cc.Invoke(ctx, PanelService_UpdatePanelByName_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -87,7 +111,7 @@ func (c *panelServiceClient) UpdatePanelByName(ctx context.Context, in *UpdatePa
func (c *panelServiceClient) DeletePanel(ctx context.Context, in *DeletePanelByIdRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
out := new(emptypb.Empty)
err := c.cc.Invoke(ctx, "/panels.panel.v1.PanelService/DeletePanel", in, out, opts...)
err := c.cc.Invoke(ctx, PanelService_DeletePanel_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -96,7 +120,7 @@ func (c *panelServiceClient) DeletePanel(ctx context.Context, in *DeletePanelByI
func (c *panelServiceClient) DeletePanelByName(ctx context.Context, in *DeletePanelByNameRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
out := new(emptypb.Empty)
err := c.cc.Invoke(ctx, "/panels.panel.v1.PanelService/DeletePanelByName", in, out, opts...)
err := c.cc.Invoke(ctx, PanelService_DeletePanelByName_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -165,7 +189,7 @@ func _PanelService_CreatePanel_Handler(srv interface{}, ctx context.Context, dec
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/panels.panel.v1.PanelService/CreatePanel",
FullMethod: PanelService_CreatePanel_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PanelServiceServer).CreatePanel(ctx, req.(*CreatePanelRequest))
@@ -183,7 +207,7 @@ func _PanelService_GetPanel_Handler(srv interface{}, ctx context.Context, dec fu
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/panels.panel.v1.PanelService/GetPanel",
FullMethod: PanelService_GetPanel_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PanelServiceServer).GetPanel(ctx, req.(*GetPanelByIdRequest))
@@ -201,7 +225,7 @@ func _PanelService_GetPanelByName_Handler(srv interface{}, ctx context.Context,
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/panels.panel.v1.PanelService/GetPanelByName",
FullMethod: PanelService_GetPanelByName_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PanelServiceServer).GetPanelByName(ctx, req.(*GetPanelByNameRequest))
@@ -219,7 +243,7 @@ func _PanelService_UpdatePanel_Handler(srv interface{}, ctx context.Context, dec
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/panels.panel.v1.PanelService/UpdatePanel",
FullMethod: PanelService_UpdatePanel_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PanelServiceServer).UpdatePanel(ctx, req.(*UpdatePanelByIdRequest))
@@ -237,7 +261,7 @@ func _PanelService_UpdatePanelByName_Handler(srv interface{}, ctx context.Contex
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/panels.panel.v1.PanelService/UpdatePanelByName",
FullMethod: PanelService_UpdatePanelByName_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PanelServiceServer).UpdatePanelByName(ctx, req.(*UpdatePanelByNameRequest))
@@ -255,7 +279,7 @@ func _PanelService_DeletePanel_Handler(srv interface{}, ctx context.Context, dec
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/panels.panel.v1.PanelService/DeletePanel",
FullMethod: PanelService_DeletePanel_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PanelServiceServer).DeletePanel(ctx, req.(*DeletePanelByIdRequest))
@@ -273,7 +297,7 @@ func _PanelService_DeletePanelByName_Handler(srv interface{}, ctx context.Contex
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/panels.panel.v1.PanelService/DeletePanelByName",
FullMethod: PanelService_DeletePanelByName_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PanelServiceServer).DeletePanelByName(ctx, req.(*DeletePanelByNameRequest))

View File

@@ -1,15 +1,29 @@
// 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 rpc
import (
"net"
"context"
"net"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/hexolan/panels/panel-service/internal"
"github.com/hexolan/panels/panel-service/internal/rpc/panelv1"
@@ -47,16 +61,16 @@ func loggingInterceptor(logger zerolog.Logger) logging.Logger {
logger := logger.With().Fields(fields).Logger()
switch lvl {
case logging.LevelError:
logger.Error().Msg(msg)
case logging.LevelWarn:
logger.Warn().Msg(msg)
case logging.LevelInfo:
logger.Info().Msg(msg)
case logging.LevelDebug:
logger.Debug().Msg(msg)
default:
logger.Debug().Interface("unknown-log-level", lvl).Msg(msg)
case logging.LevelError:
logger.Error().Msg(msg)
case logging.LevelWarn:
logger.Warn().Msg(msg)
case logging.LevelInfo:
logger.Info().Msg(msg)
case logging.LevelDebug:
logger.Debug().Msg(msg)
default:
logger.Debug().Interface("unknown-log-level", lvl).Msg(msg)
}
})
}
@@ -67,11 +81,11 @@ func (r *RPCServer) Serve() {
if err != nil {
log.Panic().Err(err).Caller().Msg("failed to listen on RPC port (:9090)")
}
// Begin serving gRPC.
log.Info().Str("address", lis.Addr().String()).Msg("attempting to serve RPC...")
err = r.grpcSvr.Serve(lis)
if err != nil {
log.Panic().Err(err).Caller().Msg("failed to serve RPC")
}
}
}

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 service
import (
@@ -16,7 +30,7 @@ type panelService struct {
func NewPanelService(events kafka.PanelEventProducer, repo internal.PanelRepository) internal.PanelService {
return panelService{
events: events,
repo: repo,
repo: repo,
}
}
@@ -52,7 +66,7 @@ func (srv panelService) GetPanelByName(ctx context.Context, name string) (*inter
if err != nil {
return nil, err
}
// Pass to service method for GetPanel (by id).
return srv.GetPanel(ctx, *id)
}
@@ -92,7 +106,7 @@ func (srv panelService) UpdatePanelByName(ctx context.Context, name string, data
if err != nil {
return nil, err
}
// Pass to service method for UpdatePanel (by id).
return srv.UpdatePanel(ctx, *id, data)
}
@@ -123,4 +137,4 @@ func (srv panelService) DeletePanelByName(ctx context.Context, name string) erro
// Pass to service method for DeletePanel (by id).
return srv.DeletePanel(ctx, *id)
}
}