-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (55 loc) · 2.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/VinayakBagaria/photogram/api/resthandlers"
"github.com/VinayakBagaria/photogram/api/routes"
"github.com/VinayakBagaria/photogram/config"
"github.com/VinayakBagaria/photogram/db"
"github.com/VinayakBagaria/photogram/docs"
"github.com/VinayakBagaria/photogram/service"
"github.com/VinayakBagaria/photogram/storage"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
func main() {
err := config.Init("config", "./")
if err != nil {
log.Fatalln("Unable to read the config file: %w", err)
}
router := gin.Default()
// Logger middleware will write the logs to gin.DefaultWriter = os.Stdout
router.Use(gin.Logger())
// Recovery middleware recovers from any panics and writes a 500 if there was one.
router.Use(gin.Recovery())
router.MaxMultipartMemory = 8 << 20 // 8 MiB
// Set swagger data
docs.SwaggerInfo.Title = "Cat Pictures"
docs.SwaggerInfo.Description = "RESTful API for uploading and managing cat pictures."
docs.SwaggerInfo.Schemes = []string{"http"}
// Initialize the db
dbConfig := db.NewConfiguration()
dbHandler, err := db.NewConnection(dbConfig)
if err != nil {
log.Panicln(err)
}
repository := db.NewPicturesRepository(dbHandler)
localStorage := storage.NewStorage(config.GetConfigValue("server.imagePath"))
service := service.NewPicturesService(repository, localStorage)
handler := resthandlers.NewPicturesHandler(service)
routesList := routes.NewPicturesRoutes(handler)
serverHandler := resthandlers.NewServerHandler()
serverRoutesList := routes.NewServerRouteList(serverHandler)
routes.Install(router, routesList)
routes.Install(router, serverRoutesList)
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
apiPort, err := strconv.Atoi(config.GetConfigValue("server.port"))
if err != nil {
log.Fatalln("Unable to parse api port")
}
log.Printf("API service running on port: %d", apiPort)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", apiPort), router))
}