forked from palembang-digital/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (87 loc) · 2.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"errors"
"log"
"net/http"
"time"
"github.com/asaskevich/govalidator"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
_ "github.com/lib/pq"
echoSwagger "github.com/swaggo/echo-swagger"
"github.com/palembang-digital/website/api/v1"
_ "github.com/palembang-digital/website/api/v1/docs"
"github.com/palembang-digital/website/pkg/services"
)
// @title Palembang Digital API
// @version 1.0.0
// @description API documentation for palembangdigital.org
// @contact.name Palembang Digital
// @contact.url https://palembangdigital.org
// @contact.email support@palembangdigital.org
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /api/v1
func main() {
log.Println("Reading the configuration from environment variables ...")
cfg, err := ReadConfig()
if err != nil {
panic(err)
}
log.Println("Migrating the database ...")
m, err := migrate.New(cfg.Database.MigrationsPath, cfg.Database.URL)
if err != nil {
panic(err)
}
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
panic(err)
}
log.Println("Initializing the database connection ...")
db, err := sqlx.Connect(cfg.Database.Driver, cfg.Database.URL)
if err != nil {
panic(err)
}
defer db.Close()
log.Println("Initializing services ...")
eventsService := services.NewEventsService(db)
organizationsService := services.NewOrganizationsService(db)
startupsService := services.NewStartupsService(db)
log.Println("Initializing the web server ...")
e := echo.New()
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.Recover())
e.Validator = &requestValidator{}
// Utility endpoints
e.GET("/docs/api/v1/index.html", echoSwagger.WrapHandler)
e.GET("/docs/api/v1/doc.json", echoSwagger.WrapHandler)
e.GET("/docs/api/v1/*", echoSwagger.WrapHandler)
e.GET("/ping", ping)
// Serve API
api := api.NewAPI(eventsService, organizationsService, startupsService, cfg.AdminUsername, cfg.AdminPassword)
api.Register(e.Group("/api/v1", middleware.Logger()))
// Serve UI
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: cfg.UIBuildPath,
HTML5: true,
}))
// Start server
s := &http.Server{
Addr: "0.0.0.0:" + cfg.Port,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
e.Logger.Fatal(e.StartServer(s))
}
type requestValidator struct{}
func (rv *requestValidator) Validate(i interface{}) (err error) {
_, err = govalidator.ValidateStruct(i)
return
}
// ping write pong to http.ResponseWriter.
func ping(c echo.Context) error {
return c.String(http.StatusOK, "pong")
}