🎉 Initial Commit
This commit is contained in:
		
							
								
								
									
										24
									
								
								pkg/internal/cache/store.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								pkg/internal/cache/store.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| package cache | ||||
|  | ||||
| import ( | ||||
| 	"github.com/dgraph-io/ristretto" | ||||
| 	"github.com/eko/gocache/lib/v4/store" | ||||
| 	ristrettoCache "github.com/eko/gocache/store/ristretto/v4" | ||||
| ) | ||||
|  | ||||
| var S store.StoreInterface | ||||
|  | ||||
| func NewStore() error { | ||||
| 	ristretto, err := ristretto.NewCache(&ristretto.Config{ | ||||
| 		NumCounters: 1000, | ||||
| 		MaxCost:     100, | ||||
| 		BufferItems: 64, | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	S = ristrettoCache.NewRistretto(ristretto) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										23
									
								
								pkg/internal/database/migrator.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								pkg/internal/database/migrator.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| package database | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/models" | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
|  | ||||
| var AutoMaintainRange = []any{ | ||||
| 	&models.Product{}, | ||||
| 	&models.ProductMeta{}, | ||||
| 	&models.ProductRelease{}, | ||||
| 	&models.ProductReleaseMeta{}, | ||||
| } | ||||
|  | ||||
| func RunMigration(source *gorm.DB) error { | ||||
| 	if err := source.AutoMigrate( | ||||
| 		AutoMaintainRange..., | ||||
| 	); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										27
									
								
								pkg/internal/database/source.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								pkg/internal/database/source.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| package database | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/gap" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"github.com/samber/lo" | ||||
| 	"github.com/spf13/viper" | ||||
| 	"gorm.io/driver/postgres" | ||||
| 	"gorm.io/gorm" | ||||
| 	"gorm.io/gorm/logger" | ||||
| ) | ||||
|  | ||||
| var C *gorm.DB | ||||
|  | ||||
| func NewGorm() error { | ||||
| 	var err error | ||||
|  | ||||
| 	dsn, err := cruda.NewCrudaConn(gap.Nx).AllocDatabase("matrix") | ||||
| 	C, err = gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.New(&log.Logger, logger.Config{ | ||||
| 		Colorful:                  true, | ||||
| 		IgnoreRecordNotFoundError: true, | ||||
| 		LogLevel:                  lo.Ternary(viper.GetBool("debug.database"), logger.Info, logger.Silent), | ||||
| 	})}) | ||||
|  | ||||
| 	return err | ||||
| } | ||||
							
								
								
									
										44
									
								
								pkg/internal/gap/server.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								pkg/internal/gap/server.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| package gap | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex" | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/proto" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"github.com/samber/lo" | ||||
|  | ||||
| 	"github.com/spf13/viper" | ||||
| ) | ||||
|  | ||||
| var Nx *nex.Conn | ||||
|  | ||||
| func InitializeToNexus() error { | ||||
| 	grpcBind := strings.SplitN(viper.GetString("grpc_bind"), ":", 2) | ||||
| 	httpBind := strings.SplitN(viper.GetString("bind"), ":", 2) | ||||
|  | ||||
| 	outboundIp, _ := nex.GetOutboundIP() | ||||
|  | ||||
| 	grpcOutbound := fmt.Sprintf("%s:%s", outboundIp, grpcBind[1]) | ||||
| 	httpOutbound := fmt.Sprintf("%s:%s", outboundIp, httpBind[1]) | ||||
|  | ||||
| 	var err error | ||||
| 	Nx, err = nex.NewNexusConn(viper.GetString("nexus_addr"), &proto.ServiceInfo{ | ||||
| 		Id:       viper.GetString("id"), | ||||
| 		Type:     "ma", | ||||
| 		Label:    "Matrix", | ||||
| 		GrpcAddr: grpcOutbound, | ||||
| 		HttpAddr: lo.ToPtr("http://" + httpOutbound + "/api"), | ||||
| 	}) | ||||
| 	if err == nil { | ||||
| 		go func() { | ||||
| 			err := Nx.RunRegistering() | ||||
| 			if err != nil { | ||||
| 				log.Error().Err(err).Msg("An error occurred while registering service...") | ||||
| 			} | ||||
| 		}() | ||||
| 	} | ||||
|  | ||||
| 	return err | ||||
| } | ||||
							
								
								
									
										26
									
								
								pkg/internal/grpc/health.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								pkg/internal/grpc/health.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	health "google.golang.org/grpc/health/grpc_health_v1" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| func (v *Server) Check(ctx context.Context, request *health.HealthCheckRequest) (*health.HealthCheckResponse, error) { | ||||
| 	return &health.HealthCheckResponse{ | ||||
| 		Status: health.HealthCheckResponse_SERVING, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (v *Server) Watch(request *health.HealthCheckRequest, server health.Health_WatchServer) error { | ||||
| 	for { | ||||
| 		if server.Send(&health.HealthCheckResponse{ | ||||
| 			Status: health.HealthCheckResponse_SERVING, | ||||
| 		}) != nil { | ||||
| 			break | ||||
| 		} | ||||
| 		time.Sleep(1000 * time.Millisecond) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										40
									
								
								pkg/internal/grpc/server.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								pkg/internal/grpc/server.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"net" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/proto" | ||||
| 	"github.com/spf13/viper" | ||||
| 	"google.golang.org/grpc" | ||||
| 	health "google.golang.org/grpc/health/grpc_health_v1" | ||||
| 	"google.golang.org/grpc/reflection" | ||||
| ) | ||||
|  | ||||
| type Server struct { | ||||
| 	proto.UnimplementedDirectoryServiceServer | ||||
| 	health.UnimplementedHealthServer | ||||
|  | ||||
| 	srv *grpc.Server | ||||
| } | ||||
|  | ||||
| func NewGrpc() *Server { | ||||
| 	server := &Server{ | ||||
| 		srv: grpc.NewServer(), | ||||
| 	} | ||||
|  | ||||
| 	proto.RegisterDirectoryServiceServer(server.srv, server) | ||||
| 	health.RegisterHealthServer(server.srv, server) | ||||
|  | ||||
| 	reflection.Register(server.srv) | ||||
|  | ||||
| 	return server | ||||
| } | ||||
|  | ||||
| func (v *Server) Listen() error { | ||||
| 	listener, err := net.Listen("tcp", viper.GetString("grpc_bind")) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return v.srv.Serve(listener) | ||||
| } | ||||
							
								
								
									
										42
									
								
								pkg/internal/grpc/services.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								pkg/internal/grpc/services.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex" | ||||
| 	"strconv" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/proto" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/database" | ||||
| ) | ||||
|  | ||||
| func (v *Server) BroadcastEvent(ctx context.Context, in *proto.EventInfo) (*proto.EventResponse, error) { | ||||
| 	switch in.GetEvent() { | ||||
| 	case "deletion": | ||||
| 		data := nex.DecodeMap(in.GetData()) | ||||
| 		resType, ok := data["type"].(string) | ||||
| 		if !ok { | ||||
| 			break | ||||
| 		} | ||||
| 		switch resType { | ||||
| 		case "account": | ||||
| 			id, ok := data["id"].(string) | ||||
| 			if !ok { | ||||
| 				break | ||||
| 			} | ||||
| 			numericId, err := strconv.Atoi(id) | ||||
| 			if err != nil { | ||||
| 				break | ||||
| 			} | ||||
| 			tx := database.C.Begin() | ||||
| 			for _, model := range database.AutoMaintainRange { | ||||
| 				switch model.(type) { | ||||
| 				default: | ||||
| 					tx.Delete(model, "account_id = ?", numericId) | ||||
| 				} | ||||
| 			} | ||||
| 			tx.Commit() | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return &proto.EventResponse{}, nil | ||||
| } | ||||
							
								
								
									
										5
									
								
								pkg/internal/meta.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								pkg/internal/meta.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | ||||
| package pkg | ||||
|  | ||||
| const ( | ||||
| 	AppVersion = "1.0.0" | ||||
| ) | ||||
							
								
								
									
										30
									
								
								pkg/internal/models/product.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								pkg/internal/models/product.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| package models | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" | ||||
| 	"gorm.io/datatypes" | ||||
| ) | ||||
|  | ||||
| type Product struct { | ||||
| 	cruda.BaseModel | ||||
|  | ||||
| 	Icon        string                      `json:"icon"` // random id of atttachment | ||||
| 	Name        string                      `json:"name"` | ||||
| 	Alias       string                      `json:"alias" gorm:"uniqueIndex"` | ||||
| 	Description string                      `json:"description"` | ||||
| 	Previews    datatypes.JSONSlice[string] `json:"previews"` // random id of attachments | ||||
| 	Tags        datatypes.JSONSlice[string] `json:"tags"` | ||||
|  | ||||
| 	Meta      ProductMeta      `json:"meta" gorm:"foreignKey:ProductID"` | ||||
| 	Releases  []ProductRelease `json:"releases" gorm:"foreignKey:ProductID"` | ||||
| 	AccountID uint             `json:"account_id"` | ||||
| } | ||||
|  | ||||
| type ProductMeta struct { | ||||
| 	cruda.BaseModel | ||||
|  | ||||
| 	Introduction string                      `json:"introduction"` | ||||
| 	Attachments  datatypes.JSONSlice[string] `json:"attachments"` // random id of attachments | ||||
|  | ||||
| 	ProductID uint `json:"product_id"` | ||||
| } | ||||
							
								
								
									
										36
									
								
								pkg/internal/models/release.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								pkg/internal/models/release.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| package models | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" | ||||
| 	"gorm.io/datatypes" | ||||
| ) | ||||
|  | ||||
| type ProductReleaseType int | ||||
|  | ||||
| const ( | ||||
| 	ReleaseTypeMinor = ProductReleaseType(iota) | ||||
| 	ReleaseTypeRegular | ||||
| 	ReleaseTypeMajor | ||||
| ) | ||||
|  | ||||
| type ProductRelease struct { | ||||
| 	cruda.BaseModel | ||||
|  | ||||
| 	Version string                             `json:"version"` | ||||
| 	Type    ProductReleaseType                 `json:"type"` | ||||
| 	Channel string                             `json:"channel"` | ||||
| 	Assets  datatypes.JSONType[map[string]any] `json:"assets"` | ||||
|  | ||||
| 	ProductID uint               `json:"product_id"` | ||||
| 	Meta      ProductReleaseMeta `json:"meta" gorm:"foreignKey:ReleaseID"` | ||||
| } | ||||
|  | ||||
| type ProductReleaseMeta struct { | ||||
| 	cruda.BaseModel | ||||
|  | ||||
| 	Title       string                      `json:"title"` | ||||
| 	Description string                      `json:"description"` | ||||
| 	Content     string                      `json:"content"` | ||||
| 	Attachments datatypes.JSONSlice[string] `json:"attachments"` | ||||
| 	ReleaseID   uint                        `json:"release_id"` | ||||
| } | ||||
							
								
								
									
										29
									
								
								pkg/internal/server/api/index.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								pkg/internal/server/api/index.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| package api | ||||
|  | ||||
| import ( | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| ) | ||||
|  | ||||
| func MapAPIs(app *fiber.App, baseURL string) { | ||||
| 	api := app.Group(baseURL).Name("API") | ||||
| 	{ | ||||
| 		products := api.Group("/products") | ||||
| 		{ | ||||
| 			products.Get("/", listProduct) | ||||
| 			products.Get("/created", listCreatedProduct) | ||||
| 			products.Get("/:productId", getProduct) | ||||
| 			products.Post("/", createProduct) | ||||
| 			products.Put("/:productId", updateProduct) | ||||
| 			products.Delete("/:productId", deleteProduct) | ||||
|  | ||||
| 			releases := products.Group("/:productId/releases") | ||||
| 			{ | ||||
| 				releases.Get("/", listRelease) | ||||
| 				releases.Get("/:releaseId", getRelease) | ||||
| 				releases.Post("/", createRelease) | ||||
| 				releases.Put("/:releaseId", updateRelease) | ||||
| 				releases.Delete("/:releaseId", deleteRelease) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										175
									
								
								pkg/internal/server/api/products_api.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										175
									
								
								pkg/internal/server/api/products_api.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,175 @@ | ||||
| package api | ||||
|  | ||||
| import ( | ||||
| 	"strconv" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/models" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/server/exts" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/services" | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| ) | ||||
|  | ||||
| func listProduct(c *fiber.Ctx) error { | ||||
| 	take := c.QueryInt("take", 0) | ||||
| 	offset := c.QueryInt("offset", 0) | ||||
|  | ||||
| 	count, err := services.CountProduct() | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	items, err := services.ListProduct(take, offset) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	return c.JSON(fiber.Map{ | ||||
| 		"count": count, | ||||
| 		"data":  items, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func listCreatedProduct(c *fiber.Ctx) error { | ||||
| 	if err := sec.EnsureAuthenticated(c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	user := c.Locals("nex_user").(*sec.UserInfo) | ||||
|  | ||||
| 	take := c.QueryInt("take", 0) | ||||
| 	offset := c.QueryInt("offset", 0) | ||||
|  | ||||
| 	count, err := services.CountCreatedProduct(user.ID) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	items, err := services.ListCreatedProduct(user.ID, take, offset) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	return c.JSON(fiber.Map{ | ||||
| 		"count": count, | ||||
| 		"data":  items, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func getProduct(c *fiber.Ctx) error { | ||||
| 	alias := c.Params("productId") | ||||
|  | ||||
| 	var item models.Product | ||||
| 	if numericId, err := strconv.Atoi(alias); err == nil { | ||||
| 		item, err = services.GetProduct(uint(numericId)) | ||||
| 		if err != nil { | ||||
| 			return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 		} | ||||
| 	} else { | ||||
| 		item, err = services.GetProduct(uint(numericId)) | ||||
| 		if err != nil { | ||||
| 			return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return c.JSON(item) | ||||
| } | ||||
|  | ||||
| func createProduct(c *fiber.Ctx) error { | ||||
| 	if err := sec.EnsureAuthenticated(c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	user := c.Locals("nex_user").(*sec.UserInfo) | ||||
|  | ||||
| 	var data struct { | ||||
| 		Name         string   `json:"name" validate:"required,max=256"` | ||||
| 		Description  string   `json:"description" validate:"max=4096"` | ||||
| 		Introduction string   `json:"introduction"` | ||||
| 		Alias        string   `json:"alias" validate:"required"` | ||||
| 		Tags         []string `json:"tags"` | ||||
| 		Attachments  []string `json:"attachments"` | ||||
| 	} | ||||
|  | ||||
| 	if err := exts.BindAndValidate(c, &data); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	product := models.Product{ | ||||
| 		Name:        data.Name, | ||||
| 		Alias:       data.Alias, | ||||
| 		Description: data.Description, | ||||
| 		Tags:        data.Tags, | ||||
| 		Meta: models.ProductMeta{ | ||||
| 			Introduction: data.Introduction, | ||||
| 			Attachments:  data.Attachments, | ||||
| 		}, | ||||
| 		AccountID: user.ID, | ||||
| 	} | ||||
|  | ||||
| 	if product, err := services.NewProduct(product); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} else { | ||||
| 		return c.JSON(product) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func updateProduct(c *fiber.Ctx) error { | ||||
| 	id, _ := c.ParamsInt("productId", 0) | ||||
|  | ||||
| 	if err := sec.EnsureAuthenticated(c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	user := c.Locals("nex_user").(*sec.UserInfo) | ||||
|  | ||||
| 	var data struct { | ||||
| 		Icon         string   `json:"icon"` | ||||
| 		Name         string   `json:"name" validate:"required,max=256"` | ||||
| 		Description  string   `json:"description" validate:"max=4096"` | ||||
| 		Introduction string   `json:"introduction"` | ||||
| 		Alias        string   `json:"alias" validate:"required"` | ||||
| 		Tags         []string `json:"tags"` | ||||
| 		Previews     []string `json:"previews"` | ||||
| 		Attachments  []string `json:"attachments"` | ||||
| 	} | ||||
|  | ||||
| 	if err := exts.BindAndValidate(c, &data); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	product, err := services.GetProductWithUser(uint(id), user.ID) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	product.Icon = data.Icon | ||||
| 	product.Name = data.Name | ||||
| 	product.Description = data.Description | ||||
| 	product.Tags = data.Tags | ||||
| 	product.Previews = data.Previews | ||||
| 	product.Meta.Introduction = data.Introduction | ||||
| 	product.Meta.Attachments = data.Attachments | ||||
|  | ||||
| 	if product, err := services.UpdateProduct(product); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} else { | ||||
| 		return c.JSON(product) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func deleteProduct(c *fiber.Ctx) error { | ||||
| 	id, _ := c.ParamsInt("productId", 0) | ||||
|  | ||||
| 	if err := sec.EnsureAuthenticated(c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	user := c.Locals("nex_user").(*sec.UserInfo) | ||||
|  | ||||
| 	product, err := services.GetProductWithUser(uint(id), user.ID) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusNotFound, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	if _, err := services.DeleteProduct(product); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
| 	return c.SendStatus(fiber.StatusOK) | ||||
| } | ||||
							
								
								
									
										167
									
								
								pkg/internal/server/api/releases_api.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										167
									
								
								pkg/internal/server/api/releases_api.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,167 @@ | ||||
| package api | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/models" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/server/exts" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/services" | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| 	"gorm.io/datatypes" | ||||
| ) | ||||
|  | ||||
| func listRelease(c *fiber.Ctx) error { | ||||
| 	take := c.QueryInt("take", 0) | ||||
| 	offset := c.QueryInt("offset", 0) | ||||
| 	id, _ := c.ParamsInt("productId", 0) | ||||
|  | ||||
| 	count, err := services.CountRelease(id) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	items, err := services.ListRelease(id, take, offset) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	return c.JSON(fiber.Map{ | ||||
| 		"count": count, | ||||
| 		"data":  items, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func getRelease(c *fiber.Ctx) error { | ||||
| 	productId, _ := c.ParamsInt("productId", 0) | ||||
| 	id, _ := c.ParamsInt("releaseId", 0) | ||||
|  | ||||
| 	if item, err := services.GetReleaseWithProduct(uint(id), uint(productId)); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 	} else { | ||||
| 		return c.JSON(item) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func createRelease(c *fiber.Ctx) error { | ||||
| 	if err := sec.EnsureAuthenticated(c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	user := c.Locals("nex_user").(*sec.UserInfo) | ||||
|  | ||||
| 	productId, _ := c.ParamsInt("productId", 0) | ||||
|  | ||||
| 	var data struct { | ||||
| 		Version     string         `json:"version" validate:"required"` | ||||
| 		Type        int            `json:"type" validate:"required"` | ||||
| 		Channel     string         `json:"channel" validate:"required"` | ||||
| 		Title       string         `json:"title" validate:"required,max=1024"` | ||||
| 		Description string         `json:"description" validate:"required,max=4096"` | ||||
| 		Content     string         `json:"content" validate:"required"` | ||||
| 		Assets      map[string]any `json:"assets" validate:"required"` | ||||
| 		Attachments []string       `json:"attachments"` | ||||
| 	} | ||||
|  | ||||
| 	if err := exts.BindAndValidate(c, &data); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	product, err := services.GetProductWithUser(uint(productId), user.ID) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	release := models.ProductRelease{ | ||||
| 		Version:   data.Version, | ||||
| 		Type:      models.ProductReleaseType(data.Type), | ||||
| 		Channel:   data.Channel, | ||||
| 		Assets:    datatypes.NewJSONType(data.Assets), | ||||
| 		ProductID: product.ID, | ||||
| 		Meta: models.ProductReleaseMeta{ | ||||
| 			Title:       data.Title, | ||||
| 			Description: data.Description, | ||||
| 			Content:     data.Content, | ||||
| 			Attachments: data.Attachments, | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	if release, err := services.NewRelease(release); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} else { | ||||
| 		return c.JSON(release) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func updateRelease(c *fiber.Ctx) error { | ||||
| 	if err := sec.EnsureAuthenticated(c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	user := c.Locals("nex_user").(*sec.UserInfo) | ||||
|  | ||||
| 	productId, _ := c.ParamsInt("productId", 0) | ||||
| 	id, _ := c.ParamsInt("releaseId", 0) | ||||
|  | ||||
| 	var data struct { | ||||
| 		Version     string         `json:"version" validate:"required"` | ||||
| 		Type        int            `json:"type" validate:"required"` | ||||
| 		Channel     string         `json:"channel" validate:"required"` | ||||
| 		Title       string         `json:"title" validate:"required,max=1024"` | ||||
| 		Description string         `json:"description" validate:"required,max=4096"` | ||||
| 		Content     string         `json:"content" validate:"required"` | ||||
| 		Assets      map[string]any `json:"assets" validate:"required"` | ||||
| 		Attachments []string       `json:"attachments"` | ||||
| 	} | ||||
|  | ||||
| 	if err := exts.BindAndValidate(c, &data); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	product, err := services.GetProductWithUser(uint(productId), user.ID) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	release, err := services.GetReleaseWithProduct(uint(id), product.ID) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	release.Version = data.Version | ||||
| 	release.Type = models.ProductReleaseType(data.Type) | ||||
| 	release.Channel = data.Channel | ||||
| 	release.Assets = datatypes.NewJSONType(data.Assets) | ||||
| 	release.Meta.Title = data.Title | ||||
| 	release.Meta.Description = data.Description | ||||
| 	release.Meta.Content = data.Content | ||||
| 	release.Meta.Attachments = data.Attachments | ||||
|  | ||||
| 	if release, err := services.UpdateRelease(release); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} else { | ||||
| 		return c.JSON(release) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func deleteRelease(c *fiber.Ctx) error { | ||||
| 	if err := sec.EnsureAuthenticated(c); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	user := c.Locals("nex_user").(*sec.UserInfo) | ||||
|  | ||||
| 	productId, _ := c.ParamsInt("productId", 0) | ||||
| 	id, _ := c.ParamsInt("releaseId", 0) | ||||
|  | ||||
| 	product, err := services.GetProductWithUser(uint(productId), user.ID) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusNotFound, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	release, err := services.GetReleaseWithProduct(uint(id), product.ID) | ||||
| 	if err != nil { | ||||
| 		return fiber.NewError(fiber.StatusNotFound, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	if _, err := services.DeleteRelease(release); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} else { | ||||
| 		return c.SendStatus(fiber.StatusOK) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										18
									
								
								pkg/internal/server/exts/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								pkg/internal/server/exts/utils.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| package exts | ||||
|  | ||||
| import ( | ||||
| 	"github.com/go-playground/validator/v10" | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| ) | ||||
|  | ||||
| var validation = validator.New(validator.WithRequiredStructEnabled()) | ||||
|  | ||||
| func BindAndValidate(c *fiber.Ctx, out any) error { | ||||
| 	if err := c.BodyParser(out); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} else if err := validation.Struct(out); err != nil { | ||||
| 		return fiber.NewError(fiber.StatusBadRequest, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										71
									
								
								pkg/internal/server/server.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								pkg/internal/server/server.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,71 @@ | ||||
| package server | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" | ||||
|  | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/server/api" | ||||
|  | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| 	"github.com/gofiber/fiber/v2/middleware/cors" | ||||
| 	"github.com/gofiber/fiber/v2/middleware/idempotency" | ||||
| 	"github.com/gofiber/fiber/v2/middleware/logger" | ||||
| 	jsoniter "github.com/json-iterator/go" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"github.com/spf13/viper" | ||||
| ) | ||||
|  | ||||
| var IReader *sec.InternalTokenReader | ||||
|  | ||||
| type App struct { | ||||
| 	app *fiber.App | ||||
| } | ||||
|  | ||||
| func NewServer() *App { | ||||
| 	app := fiber.New(fiber.Config{ | ||||
| 		DisableStartupMessage: true, | ||||
| 		EnableIPValidation:    true, | ||||
| 		ServerHeader:          "Matrix.Nucleus", | ||||
| 		AppName:               "Matrix.Nucleus", | ||||
| 		ProxyHeader:           fiber.HeaderXForwardedFor, | ||||
| 		JSONEncoder:           jsoniter.ConfigCompatibleWithStandardLibrary.Marshal, | ||||
| 		JSONDecoder:           jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal, | ||||
| 		BodyLimit:             512 * 1024 * 1024 * 1024, // 512 TiB | ||||
| 		EnablePrintRoutes:     viper.GetBool("debug.print_routes"), | ||||
| 	}) | ||||
|  | ||||
| 	app.Use(idempotency.New()) | ||||
| 	app.Use(cors.New(cors.Config{ | ||||
| 		AllowCredentials: true, | ||||
| 		AllowMethods: strings.Join([]string{ | ||||
| 			fiber.MethodGet, | ||||
| 			fiber.MethodPost, | ||||
| 			fiber.MethodHead, | ||||
| 			fiber.MethodOptions, | ||||
| 			fiber.MethodPut, | ||||
| 			fiber.MethodDelete, | ||||
| 			fiber.MethodPatch, | ||||
| 		}, ","), | ||||
| 		AllowOriginsFunc: func(origin string) bool { | ||||
| 			return true | ||||
| 		}, | ||||
| 	})) | ||||
|  | ||||
| 	app.Use(logger.New(logger.Config{ | ||||
| 		Format: "${status} | ${latency} | ${method} ${path}\n", | ||||
| 		Output: log.Logger, | ||||
| 	})) | ||||
|  | ||||
| 	app.Use(sec.ContextMiddleware(IReader)) | ||||
|  | ||||
| 	api.MapAPIs(app, "/api") | ||||
|  | ||||
| 	return &App{app} | ||||
| } | ||||
|  | ||||
| func (v *App) Listen() { | ||||
| 	if err := v.app.Listen(viper.GetString("bind")); err != nil { | ||||
| 		log.Fatal().Err(err).Msg("An error occurred when starting server...") | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										24
									
								
								pkg/internal/services/cleaner.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								pkg/internal/services/cleaner.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| package services | ||||
|  | ||||
| import ( | ||||
| 	database2 "git.solsynth.dev/matrix/nucleus/pkg/internal/database" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/rs/zerolog/log" | ||||
| ) | ||||
|  | ||||
| func DoAutoDatabaseCleanup() { | ||||
| 	deadline := time.Now().Add(60 * time.Minute) | ||||
| 	log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...") | ||||
|  | ||||
| 	var count int64 | ||||
| 	for _, model := range database2.AutoMaintainRange { | ||||
| 		tx := database2.C.Unscoped().Delete(model, "deleted_at >= ?", deadline) | ||||
| 		if tx.Error != nil { | ||||
| 			log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...") | ||||
| 		} | ||||
| 		count += tx.RowsAffected | ||||
| 	} | ||||
|  | ||||
| 	log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.") | ||||
| } | ||||
							
								
								
									
										90
									
								
								pkg/internal/services/product.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								pkg/internal/services/product.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | ||||
| package services | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/database" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/models" | ||||
| 	"gorm.io/gorm" | ||||
| 	"gorm.io/gorm/clause" | ||||
| ) | ||||
|  | ||||
| func CountProduct() (int64, error) { | ||||
| 	var count int64 | ||||
| 	if err := database.C.Model(&models.Product{}).Count(&count).Error; err != nil { | ||||
| 		return count, err | ||||
| 	} | ||||
| 	return count, nil | ||||
| } | ||||
|  | ||||
| func CountCreatedProduct(user uint) (int64, error) { | ||||
| 	var count int64 | ||||
| 	if err := database.C.Model(&models.Product{}).Where("account_id = ?", user).Count(&count).Error; err != nil { | ||||
| 		return count, err | ||||
| 	} | ||||
| 	return count, nil | ||||
| } | ||||
|  | ||||
| func ListProduct(take, offset int) ([]models.Product, error) { | ||||
| 	var items []models.Product | ||||
| 	if err := database.C.Limit(take).Offset(offset).Preload("Meta").Find(&items).Error; err != nil { | ||||
| 		return items, err | ||||
| 	} | ||||
| 	return items, nil | ||||
| } | ||||
|  | ||||
| func ListCreatedProduct(user uint, take, offset int) ([]models.Product, error) { | ||||
| 	var items []models.Product | ||||
| 	if err := database.C. | ||||
| 		Where("account_id = ?", user). | ||||
| 		Preload("Meta"). | ||||
| 		Limit(take).Offset(offset).Find(&items).Error; err != nil { | ||||
| 		return items, err | ||||
| 	} | ||||
| 	return items, nil | ||||
| } | ||||
|  | ||||
| func GetProduct(id uint) (models.Product, error) { | ||||
| 	var item models.Product | ||||
| 	if err := database.C.Where("id = ?", id).Preload("Meta").First(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func GetProductWithUser(id uint, user uint) (models.Product, error) { | ||||
| 	var item models.Product | ||||
| 	if err := database.C.Where("id = ? AND account_id = ?", id, user).Preload("Meta").First(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func GetProductByAlias(alias string) (models.Product, error) { | ||||
| 	var item models.Product | ||||
| 	if err := database.C.Where("alias = ?", alias).Preload("Meta").First(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func NewProduct(item models.Product) (models.Product, error) { | ||||
| 	if err := database.C.Create(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func UpdateProduct(item models.Product) (models.Product, error) { | ||||
| 	if err := database.C.Session(&gorm.Session{ | ||||
| 		FullSaveAssociations: true, | ||||
| 	}).Save(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func DeleteProduct(item models.Product) (models.Product, error) { | ||||
| 	if err := database.C.Select(clause.Associations).Delete(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
							
								
								
									
										65
									
								
								pkg/internal/services/release.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								pkg/internal/services/release.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| package services | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/database" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/models" | ||||
| 	"gorm.io/gorm" | ||||
| 	"gorm.io/gorm/clause" | ||||
| ) | ||||
|  | ||||
| func CountRelease(product int) (int64, error) { | ||||
| 	var count int64 | ||||
| 	if err := database.C.Model(&models.ProductRelease{}).Where("product_id = ?", product).Count(&count).Error; err != nil { | ||||
| 		return count, err | ||||
| 	} | ||||
| 	return count, nil | ||||
| } | ||||
|  | ||||
| func ListRelease(product int, take, offset int) ([]models.ProductRelease, error) { | ||||
| 	var items []models.ProductRelease | ||||
| 	if err := database.C. | ||||
| 		Where("product_id = ?", product).Preload("Meta"). | ||||
| 		Limit(take).Offset(offset).Find(&items).Error; err != nil { | ||||
| 		return items, err | ||||
| 	} | ||||
| 	return items, nil | ||||
| } | ||||
|  | ||||
| func GetRelease(id uint) (models.ProductRelease, error) { | ||||
| 	var item models.ProductRelease | ||||
| 	if err := database.C.Where("id = ?", id).Preload("Meta").First(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func GetReleaseWithProduct(id uint, product uint) (models.ProductRelease, error) { | ||||
| 	var item models.ProductRelease | ||||
| 	if err := database.C.Where("id = ? AND product_id = ?", id, product).Preload("Meta").First(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func NewRelease(item models.ProductRelease) (models.ProductRelease, error) { | ||||
| 	if err := database.C.Create(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func UpdateRelease(item models.ProductRelease) (models.ProductRelease, error) { | ||||
| 	if err := database.C.Session(&gorm.Session{ | ||||
| 		FullSaveAssociations: true, | ||||
| 	}).Save(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func DeleteRelease(item models.ProductRelease) (models.ProductRelease, error) { | ||||
| 	if err := database.C.Select(clause.Associations).Delete(&item).Error; err != nil { | ||||
| 		return item, err | ||||
| 	} | ||||
| 	return item, nil | ||||
| } | ||||
							
								
								
									
										92
									
								
								pkg/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								pkg/main.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,92 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"os/signal" | ||||
| 	"syscall" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" | ||||
| 	pkg "git.solsynth.dev/matrix/nucleus/pkg/internal" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/gap" | ||||
| 	"github.com/fatih/color" | ||||
|  | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/cache" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/database" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/grpc" | ||||
|  | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/server" | ||||
| 	"git.solsynth.dev/matrix/nucleus/pkg/internal/services" | ||||
| 	"github.com/robfig/cron/v3" | ||||
|  | ||||
| 	"github.com/rs/zerolog" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"github.com/spf13/viper" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| 	zerolog.TimeFieldFormat = zerolog.TimeFormatUnix | ||||
| 	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) | ||||
| } | ||||
|  | ||||
| func main() { | ||||
| 	// Booting screen | ||||
| 	fmt.Println(color.YellowString(" __  __       _        _\n|  \\/  | __ _| |_ _ __(_)_  __\n| |\\/| |/ _` | __| '__| \\ \\/ /\n| |  | | (_| | |_| |  | |>  <\n|_|  |_|\\__,_|\\__|_|  |_/_/\\_\\")) | ||||
| 	fmt.Printf("%s v%s\n", color.New(color.FgHiYellow).Add(color.Bold).Sprintf("Matrix.Nucleus"), pkg.AppVersion) | ||||
| 	fmt.Printf("The server side software of Matrix Software Marketplace\n") | ||||
| 	color.HiBlack("=====================================================\n") | ||||
|  | ||||
| 	// Configure settings | ||||
| 	viper.AddConfigPath(".") | ||||
| 	viper.AddConfigPath("..") | ||||
| 	viper.SetConfigName("settings") | ||||
| 	viper.SetConfigType("toml") | ||||
|  | ||||
| 	// Load settings | ||||
| 	if err := viper.ReadInConfig(); err != nil { | ||||
| 		log.Panic().Err(err).Msg("An error occurred when loading settings.") | ||||
| 	} | ||||
|  | ||||
| 	// Connect to nexus | ||||
| 	if err := gap.InitializeToNexus(); err != nil { | ||||
| 		log.Error().Err(err).Msg("An error occurred when registering service to nexus...") | ||||
| 	} | ||||
|  | ||||
| 	// Load keypair | ||||
| 	if reader, err := sec.NewInternalTokenReader(viper.GetString("security.internal_public_key")); err != nil { | ||||
| 		log.Error().Err(err).Msg("An error occurred when reading internal public key for jwt. Authentication related features will be disabled.") | ||||
| 	} else { | ||||
| 		server.IReader = reader | ||||
| 		log.Info().Msg("Internal jwt public key loaded.") | ||||
| 	} | ||||
|  | ||||
| 	// Connect to database | ||||
| 	if err := database.NewGorm(); err != nil { | ||||
| 		log.Fatal().Err(err).Msg("An error occurred when connect to database.") | ||||
| 	} else if err := database.RunMigration(database.C); err != nil { | ||||
| 		log.Fatal().Err(err).Msg("An error occurred when running database auto migration.") | ||||
| 	} | ||||
|  | ||||
| 	// Initialize cache | ||||
| 	if err := cache.NewStore(); err != nil { | ||||
| 		log.Fatal().Err(err).Msg("An error occurred when initializing cache.") | ||||
| 	} | ||||
|  | ||||
| 	// Configure timed tasks | ||||
| 	quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger))) | ||||
| 	quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup) | ||||
| 	quartz.Start() | ||||
|  | ||||
| 	// Server | ||||
| 	go server.NewServer().Listen() | ||||
|  | ||||
| 	// Grpc Server | ||||
| 	go grpc.NewGrpc().Listen() | ||||
|  | ||||
| 	// Messages | ||||
| 	quit := make(chan os.Signal, 1) | ||||
| 	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) | ||||
| 	<-quit | ||||
|  | ||||
| 	quartz.Stop() | ||||
| } | ||||
		Reference in New Issue
	
	Block a user