1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/main.go

197 lines
5.8 KiB
Go
Raw Normal View History

2019-05-05 23:36:49 +03:00
package main
import (
"github.com/gorilla/handlers"
2019-05-06 01:58:01 +03:00
"log"
2019-05-05 23:36:49 +03:00
"net/http"
"strconv"
"time"
2020-04-20 02:58:54 +03:00
"github.com/gobuffalo/packr/v2"
2019-05-06 00:23:54 +03:00
"github.com/gorilla/mux"
2019-05-11 18:49:56 +03:00
"github.com/jinzhu/gorm"
2020-03-31 13:22:17 +03:00
"github.com/muety/wakapi/middlewares"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/routes"
"github.com/muety/wakapi/services"
"github.com/muety/wakapi/utils"
"github.com/rubenv/sql-migrate"
2019-05-11 18:49:56 +03:00
_ "github.com/jinzhu/gorm/dialects/mysql"
2020-03-31 13:03:49 +03:00
_ "github.com/jinzhu/gorm/dialects/postgres"
2020-03-31 15:43:15 +03:00
_ "github.com/jinzhu/gorm/dialects/sqlite"
2019-05-05 23:36:49 +03:00
)
2020-05-24 17:34:32 +03:00
var (
db *gorm.DB
config *models.Config
)
var (
aliasService *services.AliasService
heartbeatService *services.HeartbeatService
userService *services.UserService
summaryService *services.SummaryService
aggregationService *services.AggregationService
)
2020-03-31 12:24:44 +03:00
// TODO: Refactor entire project to be structured after business domains
2019-05-05 23:36:49 +03:00
func main() {
config = models.GetConfig()
2020-05-24 17:34:32 +03:00
2020-04-20 02:58:54 +03:00
// Enable line numbers in logging
if config.IsDev() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
2019-05-05 23:36:49 +03:00
2019-05-11 18:49:56 +03:00
// Connect to database
2020-05-24 17:34:32 +03:00
var err error
db, err = gorm.Open(config.DbDialect, utils.MakeConnectionString(config))
2020-04-20 02:58:54 +03:00
if config.DbDialect == "sqlite3" {
db.DB().Exec("PRAGMA foreign_keys = ON;")
}
2020-03-09 19:30:23 +03:00
db.LogMode(config.IsDev())
db.DB().SetMaxIdleConns(int(config.DbMaxConn))
db.DB().SetMaxOpenConns(int(config.DbMaxConn))
2019-05-05 23:36:49 +03:00
if err != nil {
2020-04-20 02:58:54 +03:00
log.Println(err)
log.Fatal("could not connect to database")
2019-05-05 23:36:49 +03:00
}
2020-03-31 13:03:49 +03:00
// TODO: Graceful shutdown
2019-05-11 18:49:56 +03:00
defer db.Close()
// Migrate database schema
migrateDo := databaseMigrateActions(config.DbDialect)
migrateDo(db)
2019-05-05 23:36:49 +03:00
2019-05-06 01:40:41 +03:00
// Services
aliasService = services.NewAliasService(db)
heartbeatService = services.NewHeartbeatService(db)
userService = services.NewUserService(db)
summaryService = services.NewSummaryService(db, heartbeatService, aliasService)
aggregationService = services.NewAggregationService(db, userService, summaryService, heartbeatService)
2020-02-20 16:28:55 +03:00
2020-05-24 17:34:32 +03:00
// Custom migrations and initial data
addDefaultUser()
migrateLanguages()
2019-10-11 10:06:34 +03:00
// Aggregate heartbeats to summaries and persist them
2020-05-24 17:34:32 +03:00
go aggregationService.Schedule()
2019-05-06 01:40:41 +03:00
2020-03-09 19:30:23 +03:00
if config.CleanUp {
2020-05-24 17:34:32 +03:00
go heartbeatService.ScheduleCleanUp()
2020-03-09 19:30:23 +03:00
}
2019-05-06 01:40:41 +03:00
// Handlers
heartbeatHandler := routes.NewHeartbeatHandler(heartbeatService)
summaryHandler := routes.NewSummaryHandler(summaryService)
healthHandler := routes.NewHealthHandler(db)
publicHandler := routes.NewIndexHandler(userService)
2019-05-06 01:40:41 +03:00
// Setup Routers
2019-05-06 00:23:54 +03:00
router := mux.NewRouter()
publicRouter := router.PathPrefix("/").Subrouter()
summaryRouter := publicRouter.PathPrefix("/summary").Subrouter()
apiRouter := router.PathPrefix("/api").Subrouter()
2019-05-06 00:23:54 +03:00
// Middlewares
recoveryMiddleware := handlers.RecoveryHandler()
loggingMiddleware := middlewares.NewLoggingMiddleware().Handler
corsMiddleware := handlers.CORS()
authenticateMiddleware := middlewares.NewAuthenticateMiddleware(
2020-05-24 17:34:32 +03:00
userService,
[]string{"/api/health"},
).Handler
// Router configs
router.Use(loggingMiddleware, recoveryMiddleware)
summaryRouter.Use(authenticateMiddleware)
apiRouter.Use(corsMiddleware, authenticateMiddleware)
// Public Routes
publicRouter.Path("/").Methods(http.MethodGet).HandlerFunc(publicHandler.Index)
publicRouter.Path("/login").Methods(http.MethodPost).HandlerFunc(publicHandler.Login)
publicRouter.Path("/logout").Methods(http.MethodPost).HandlerFunc(publicHandler.Logout)
publicRouter.Path("/signup").Methods(http.MethodGet, http.MethodPost).HandlerFunc(publicHandler.Signup)
// Summary Routes
summaryRouter.Methods(http.MethodGet).HandlerFunc(summaryHandler.Index)
2020-02-20 16:28:55 +03:00
2019-05-06 00:23:54 +03:00
// API Routes
2020-03-31 12:24:44 +03:00
apiRouter.Path("/heartbeat").Methods(http.MethodPost).HandlerFunc(heartbeatHandler.ApiPost)
apiRouter.Path("/summary").Methods(http.MethodGet).HandlerFunc(summaryHandler.ApiGet)
2020-04-08 22:29:11 +03:00
apiRouter.Path("/health").Methods(http.MethodGet).HandlerFunc(healthHandler.ApiGet)
2019-05-06 00:23:54 +03:00
2020-02-20 16:28:55 +03:00
// Static Routes
router.PathPrefix("/assets").Handler(http.FileServer(http.Dir("./static")))
2020-02-21 14:41:29 +03:00
2019-05-05 23:36:49 +03:00
// Listen HTTP
portString := config.Addr + ":" + strconv.Itoa(config.Port)
2019-05-05 23:36:49 +03:00
s := &http.Server{
2019-05-06 00:23:54 +03:00
Handler: router,
2019-05-05 23:36:49 +03:00
Addr: portString,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
2019-05-06 01:58:01 +03:00
log.Printf("Listening on %+s\n", portString)
2019-05-05 23:36:49 +03:00
s.ListenAndServe()
}
2019-05-21 23:01:14 +03:00
func databaseMigrateActions(dbDialect string) func(db *gorm.DB) {
var migrateDo func(db *gorm.DB)
if dbDialect == "sqlite3" {
migrations := &migrate.PackrMigrationSource{
Box: packr.New("migrations", "./migrations/sqlite3"),
}
migrateDo = func(db *gorm.DB) {
n, err := migrate.Exec(db.DB(), "sqlite3", migrations, migrate.Up)
if err != nil {
log.Fatal(err)
}
log.Printf("Applied %d migrations!\n", n)
}
} else {
migrateDo = func(db *gorm.DB) {
db.AutoMigrate(&models.Alias{})
db.AutoMigrate(&models.Summary{})
db.AutoMigrate(&models.SummaryItem{})
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Heartbeat{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT")
db.AutoMigrate(&models.SummaryItem{}).AddForeignKey("summary_id", "summaries(id)", "CASCADE", "CASCADE")
}
}
return migrateDo
}
2020-05-24 17:34:32 +03:00
func migrateLanguages() {
for k, v := range config.CustomLanguages {
2019-05-21 23:01:14 +03:00
result := db.Model(models.Heartbeat{}).
Where("language = ?", "").
Where("entity LIKE ?", "%."+k).
Updates(models.Heartbeat{Language: v})
if result.Error != nil {
log.Fatal(result.Error)
}
2019-05-22 00:09:47 +03:00
if result.RowsAffected > 0 {
log.Printf("Migrated %+v rows for custom language %+s.\n", result.RowsAffected, k)
}
}
}
2020-05-24 17:34:32 +03:00
func addDefaultUser() {
u, created, err := userService.CreateOrGet(&models.Signup{
Username: config.DefaultUserName,
Password: config.DefaultUserPassword,
})
if err != nil {
log.Println("unable to create default user")
log.Fatal(err)
} else if created {
log.Printf("created default user '%s' with password '%s' and API key '%s'\n", u.ID, config.DefaultUserPassword, u.ApiKey)
} else {
log.Printf("default user '%s' already existing\n", u.ID)
2019-05-21 23:01:14 +03:00
}
}