-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
72 lines (69 loc) · 2.11 KB
/
server.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
67
68
69
70
71
72
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/bervProject/go-microservice-boilerplate/graph_base"
"github.com/bervProject/go-microservice-boilerplate/models"
"github.com/bervProject/go-microservice-boilerplate/services"
"github.com/bervProject/go-microservice-boilerplate/utils"
"github.com/labstack/echo/v4"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func main() {
dsn := utils.GetEnv("DB_CONNECTION_STRING", "")
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
LogLevel: logger.Info, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: newLogger,
})
if err != nil {
log.Fatalf("failed to connect database, error: %v", err)
}
if err = db.AutoMigrate(&models.User{}); err != nil {
log.Fatalf("Failed migrate, err: %v", err)
}
graph_base.InitGraphQL(db)
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.POST("/graphql", graph_base.GraphQLHandler)
e.POST("/start-ec2", func(c echo.Context) error {
ec2Handler := services.NewEC2Handler()
return ec2Handler.StartInstance(c.Request().Context())
})
e.POST("/stop-ec2", func(c echo.Context) error {
ec2Handler := services.NewEC2Handler()
return ec2Handler.StopInstance(c.Request().Context())
})
e.POST("/status", func(c echo.Context) error {
ec2Handler := services.NewEC2Handler()
data, err := ec2Handler.InstanceStatus(c.Request().Context())
if err != nil {
return err
}
if err := c.JSON(200, data); err != nil {
return err
}
return nil
})
e.POST("/publish-message", func(c echo.Context) error {
messageRequest := &services.KafkaRequest{}
if err := c.Bind(messageRequest); err != nil {
return err
}
producer := services.NewProducer()
return producer.Publish(c.Request().Context(), messageRequest.Message)
})
e.Logger.Fatal(e.Start(fmt.Sprintf(":%s", utils.GetEnv("PORT", "1323"))))
}