cerras/addons-site

Initial work to get basic CRUD functionality setup
draft default tip
2022-01-18, Cerras
00546d6fa774
Initial work to get basic CRUD functionality setup
// Copyright (C) 2022 Cerras
//
// This file is part of addons-site.
//
// addons-site is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// addons-site is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with addons-site. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"fmt"
"time"
"github.com/gin-gonic/gin"
"keep.imfreedom.org/cerras/addons-site/api"
"keep.imfreedom.org/cerras/addons-site/internal/config"
"keep.imfreedom.org/cerras/addons-site/internal/logging"
"keep.imfreedom.org/cerras/addons-site/internal/persistence"
"keep.imfreedom.org/cerras/addons-site/internal/service"
ginzap "github.com/gin-contrib/zap"
)
func main() {
config := config.New()
ctx := context.Background()
logger, err := logging.NewZaplogger()
if err != nil {
fmt.Println(err)
return
}
defer logger.GetBaseLogger().Sync()
addonRepo, err := persistence.NewMongoAddonRepo(ctx, config.DbConnectionString, logger)
if err != nil {
logger.Error(err, "failed to create addon repository")
return
}
defer addonRepo.Close(ctx)
addonService := service.NewAddonService(addonRepo, logger)
// Create http router
r := gin.New()
// Setup base middleware
r.Use(ginzap.Ginzap(logger.GetBaseLogger(), time.RFC3339, true))
r.Use(ginzap.RecoveryWithZap(logger.GetBaseLogger(), true))
// Register routes
r.GET("/addon", api.DefaultErrorHandling(api.ListAddons(addonService, logger)))
r.GET("/addon/:id", api.DefaultErrorHandling(api.GetAddon(addonService)))
r.POST("/addon", api.DefaultErrorHandling(api.CreateAddon(addonService)))
r.PUT("/addon/:id", api.DefaultErrorHandling(api.UpdateAddon(addonService)))
r.Run(fmt.Sprintf("%s:%d", config.BindAddress, config.Port))
}