2019-05-05 23:36:49 +03:00
|
|
|
package main
|
|
|
|
|
2021-01-24 02:13:37 +03:00
|
|
|
//go:generate $GOPATH/bin/pkger
|
2021-01-22 15:50:46 +03:00
|
|
|
|
2019-05-05 23:36:49 +03:00
|
|
|
import (
|
2021-01-30 13:17:37 +03:00
|
|
|
"github.com/emvi/logbuch"
|
2020-05-24 15:50:04 +03:00
|
|
|
"github.com/gorilla/handlers"
|
2021-01-22 15:50:46 +03:00
|
|
|
"github.com/markbates/pkger"
|
2020-09-29 19:55:07 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2021-02-02 23:50:43 +03:00
|
|
|
"github.com/muety/wakapi/migrations"
|
2020-11-01 18:56:36 +03:00
|
|
|
"github.com/muety/wakapi/repositories"
|
2021-02-03 23:28:02 +03:00
|
|
|
"github.com/muety/wakapi/routes/api"
|
2021-02-07 14:28:42 +03:00
|
|
|
"github.com/muety/wakapi/utils"
|
2021-01-31 20:29:24 +03:00
|
|
|
"gorm.io/gorm/logger"
|
2021-01-31 18:46:39 +03:00
|
|
|
"log"
|
2019-05-05 23:36:49 +03:00
|
|
|
"net/http"
|
2021-01-31 18:46:39 +03:00
|
|
|
"os"
|
2019-05-05 23:36:49 +03:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2019-05-06 00:23:54 +03:00
|
|
|
"github.com/gorilla/mux"
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/middlewares"
|
|
|
|
"github.com/muety/wakapi/routes"
|
2020-09-12 17:09:23 +03:00
|
|
|
shieldsV1Routes "github.com/muety/wakapi/routes/compat/shields/v1"
|
|
|
|
wtV1Routes "github.com/muety/wakapi/routes/compat/wakatime/v1"
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
2020-11-01 22:14:10 +03:00
|
|
|
_ "gorm.io/driver/mysql"
|
|
|
|
_ "gorm.io/driver/postgres"
|
|
|
|
_ "gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
2019-05-05 23:36:49 +03:00
|
|
|
)
|
|
|
|
|
2020-05-24 17:34:32 +03:00
|
|
|
var (
|
|
|
|
db *gorm.DB
|
2020-09-29 19:55:07 +03:00
|
|
|
config *conf.Config
|
2020-05-24 17:34:32 +03:00
|
|
|
)
|
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
var (
|
2020-11-08 12:12:49 +03:00
|
|
|
aliasRepository repositories.IAliasRepository
|
|
|
|
heartbeatRepository repositories.IHeartbeatRepository
|
|
|
|
userRepository repositories.IUserRepository
|
|
|
|
languageMappingRepository repositories.ILanguageMappingRepository
|
|
|
|
summaryRepository repositories.ISummaryRepository
|
|
|
|
keyValueRepository repositories.IKeyValueRepository
|
2020-11-01 18:56:36 +03:00
|
|
|
)
|
|
|
|
|
2020-05-24 17:34:32 +03:00
|
|
|
var (
|
2020-11-08 12:12:49 +03:00
|
|
|
aliasService services.IAliasService
|
|
|
|
heartbeatService services.IHeartbeatService
|
|
|
|
userService services.IUserService
|
|
|
|
languageMappingService services.ILanguageMappingService
|
|
|
|
summaryService services.ISummaryService
|
|
|
|
aggregationService services.IAggregationService
|
|
|
|
keyValueService services.IKeyValueService
|
2021-01-17 11:24:09 +03:00
|
|
|
miscService services.IMiscService
|
2020-05-24 17:34:32 +03:00
|
|
|
)
|
|
|
|
|
2020-03-31 12:24:44 +03:00
|
|
|
// TODO: Refactor entire project to be structured after business domains
|
|
|
|
|
2021-02-07 13:54:07 +03:00
|
|
|
// @title Wakapi API
|
|
|
|
// @version 1.0
|
|
|
|
// @description REST API to interact with [Wakapi](https://wakapi.dev)
|
|
|
|
// @description
|
|
|
|
// @description ## Authentication
|
|
|
|
// @description Set header `Authorization` to your API Key encoded as Base64 and prefixed with `Basic`
|
|
|
|
// @description **Example:** `Basic ODY2NDhkNzQtMTljNS00NTJiLWJhMDEtZmIzZWM3MGQ0YzJmCg==`
|
|
|
|
|
|
|
|
// @contact.name Ferdinand Mütsch
|
|
|
|
// @contact.url https://github.com/muety
|
|
|
|
// @contact.email ferdinand@muetsch.io
|
|
|
|
|
|
|
|
// @license.name GPL-3.0
|
|
|
|
// @license.url https://github.com/muety/wakapi/blob/master/LICENSE
|
|
|
|
|
|
|
|
// @securitydefinitions.apikey ApiKeyAuth
|
|
|
|
// @in header
|
|
|
|
// @name Authorization
|
|
|
|
|
|
|
|
// @BasePath /api
|
2019-05-05 23:36:49 +03:00
|
|
|
func main() {
|
2020-09-29 19:55:07 +03:00
|
|
|
config = conf.Load()
|
2020-05-24 17:34:32 +03:00
|
|
|
|
2021-01-30 13:17:37 +03:00
|
|
|
// Set log level
|
2020-04-25 23:53:55 +03:00
|
|
|
if config.IsDev() {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.SetLevel(logbuch.LevelDebug)
|
|
|
|
} else {
|
|
|
|
logbuch.SetLevel(logbuch.LevelInfo)
|
2020-04-25 23:53:55 +03:00
|
|
|
}
|
2019-05-05 23:36:49 +03:00
|
|
|
|
2021-01-31 20:29:24 +03:00
|
|
|
// Set up GORM
|
|
|
|
gormLogger := logger.New(
|
|
|
|
log.New(os.Stdout, "", log.LstdFlags),
|
|
|
|
logger.Config{
|
|
|
|
SlowThreshold: time.Minute,
|
|
|
|
Colorful: false,
|
|
|
|
LogLevel: logger.Silent,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-05-11 18:49:56 +03:00
|
|
|
// Connect to database
|
2020-05-24 17:34:32 +03:00
|
|
|
var err error
|
2021-01-31 20:29:24 +03:00
|
|
|
db, err = gorm.Open(config.Db.GetDialector(), &gorm.Config{Logger: gormLogger})
|
2020-10-04 11:37:38 +03:00
|
|
|
if config.Db.Dialect == "sqlite3" {
|
2020-11-01 22:14:10 +03:00
|
|
|
db.Raw("PRAGMA foreign_keys = ON;")
|
2020-04-20 02:58:54 +03:00
|
|
|
}
|
2020-11-01 22:14:10 +03:00
|
|
|
|
2020-11-06 19:09:41 +03:00
|
|
|
if config.IsDev() {
|
|
|
|
db = db.Debug()
|
|
|
|
}
|
2020-11-01 22:14:10 +03:00
|
|
|
sqlDb, _ := db.DB()
|
|
|
|
sqlDb.SetMaxIdleConns(int(config.Db.MaxConn))
|
|
|
|
sqlDb.SetMaxOpenConns(int(config.Db.MaxConn))
|
2019-05-05 23:36:49 +03:00
|
|
|
if err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Error(err.Error())
|
|
|
|
logbuch.Fatal("could not connect to database")
|
2019-05-05 23:36:49 +03:00
|
|
|
}
|
2020-11-01 22:14:10 +03:00
|
|
|
defer sqlDb.Close()
|
2019-05-11 18:49:56 +03:00
|
|
|
|
|
|
|
// Migrate database schema
|
2021-02-21 14:02:19 +03:00
|
|
|
migrations.Run(db, config)
|
2019-05-05 23:36:49 +03:00
|
|
|
|
2020-11-01 18:56:36 +03:00
|
|
|
// Repositories
|
|
|
|
aliasRepository = repositories.NewAliasRepository(db)
|
|
|
|
heartbeatRepository = repositories.NewHeartbeatRepository(db)
|
|
|
|
userRepository = repositories.NewUserRepository(db)
|
2020-11-01 22:14:10 +03:00
|
|
|
languageMappingRepository = repositories.NewLanguageMappingRepository(db)
|
2020-11-01 18:56:36 +03:00
|
|
|
summaryRepository = repositories.NewSummaryRepository(db)
|
|
|
|
keyValueRepository = repositories.NewKeyValueRepository(db)
|
|
|
|
|
2019-05-06 01:40:41 +03:00
|
|
|
// Services
|
2020-11-01 18:56:36 +03:00
|
|
|
aliasService = services.NewAliasService(aliasRepository)
|
|
|
|
userService = services.NewUserService(userRepository)
|
2020-11-01 22:14:10 +03:00
|
|
|
languageMappingService = services.NewLanguageMappingService(languageMappingRepository)
|
|
|
|
heartbeatService = services.NewHeartbeatService(heartbeatRepository, languageMappingService)
|
|
|
|
summaryService = services.NewSummaryService(summaryRepository, heartbeatService, aliasService)
|
2020-11-01 18:56:36 +03:00
|
|
|
aggregationService = services.NewAggregationService(userService, summaryService, heartbeatService)
|
|
|
|
keyValueService = services.NewKeyValueService(keyValueRepository)
|
2021-01-17 11:24:09 +03:00
|
|
|
miscService = services.NewMiscService(userService, summaryService, keyValueService)
|
2020-02-20 16:28:55 +03:00
|
|
|
|
2021-01-17 11:24:09 +03:00
|
|
|
// Schedule background tasks
|
2020-05-24 17:34:32 +03:00
|
|
|
go aggregationService.Schedule()
|
2021-01-17 11:24:09 +03:00
|
|
|
go miscService.ScheduleCountTotalTime()
|
2019-05-06 01:40:41 +03:00
|
|
|
|
2020-11-08 14:46:12 +03:00
|
|
|
routes.Init()
|
|
|
|
|
2021-02-03 23:28:02 +03:00
|
|
|
// API Handlers
|
|
|
|
healthApiHandler := api.NewHealthApiHandler(db)
|
2021-02-06 22:09:08 +03:00
|
|
|
heartbeatApiHandler := api.NewHeartbeatApiHandler(userService, heartbeatService, languageMappingService)
|
|
|
|
summaryApiHandler := api.NewSummaryApiHandler(userService, summaryService)
|
2021-02-12 20:37:30 +03:00
|
|
|
metricsHandler := api.NewMetricsHandler(userService, summaryService, heartbeatService, keyValueService)
|
2021-02-03 23:28:02 +03:00
|
|
|
|
|
|
|
// Compat Handlers
|
2021-02-06 22:09:08 +03:00
|
|
|
wakatimeV1AllHandler := wtV1Routes.NewAllTimeHandler(userService, summaryService)
|
|
|
|
wakatimeV1SummariesHandler := wtV1Routes.NewSummariesHandler(userService, summaryService)
|
|
|
|
wakatimeV1StatsHandler := wtV1Routes.NewStatsHandler(userService, summaryService)
|
2021-02-03 23:28:02 +03:00
|
|
|
shieldV1BadgeHandler := shieldsV1Routes.NewBadgeHandler(summaryService, userService)
|
|
|
|
|
|
|
|
// MVC Handlers
|
|
|
|
summaryHandler := routes.NewSummaryHandler(summaryService, userService)
|
2021-02-05 20:47:28 +03:00
|
|
|
settingsHandler := routes.NewSettingsHandler(userService, heartbeatService, summaryService, aliasService, aggregationService, languageMappingService, keyValueService)
|
2021-01-17 11:24:09 +03:00
|
|
|
homeHandler := routes.NewHomeHandler(keyValueService)
|
2020-11-28 22:23:40 +03:00
|
|
|
loginHandler := routes.NewLoginHandler(userService)
|
2020-11-06 23:19:54 +03:00
|
|
|
imprintHandler := routes.NewImprintHandler(keyValueService)
|
2019-05-06 01:40:41 +03:00
|
|
|
|
2020-05-24 15:50:04 +03:00
|
|
|
// Setup Routers
|
2019-05-06 00:23:54 +03:00
|
|
|
router := mux.NewRouter()
|
2021-02-03 23:28:02 +03:00
|
|
|
rootRouter := router.PathPrefix("/").Subrouter()
|
2021-03-10 01:56:38 +03:00
|
|
|
apiRouter := router.PathPrefix("/api").Subrouter().StrictSlash(true)
|
2021-02-03 23:28:02 +03:00
|
|
|
|
|
|
|
// Globally used middlewares
|
2021-03-26 15:10:10 +03:00
|
|
|
router.Use(middlewares.NewPrincipalMiddleware())
|
2021-03-24 00:12:15 +03:00
|
|
|
router.Use(middlewares.NewLoggingMiddleware(logbuch.Info, []string{"/assets"}))
|
2021-03-26 15:10:10 +03:00
|
|
|
router.Use(handlers.RecoveryHandler())
|
2021-03-24 00:12:15 +03:00
|
|
|
if config.Sentry.Dsn != "" {
|
|
|
|
router.Use(middlewares.NewSentryMiddleware())
|
|
|
|
}
|
2021-01-30 12:34:52 +03:00
|
|
|
|
|
|
|
// Route registrations
|
2021-02-03 23:28:02 +03:00
|
|
|
homeHandler.RegisterRoutes(rootRouter)
|
|
|
|
loginHandler.RegisterRoutes(rootRouter)
|
|
|
|
imprintHandler.RegisterRoutes(rootRouter)
|
|
|
|
summaryHandler.RegisterRoutes(rootRouter)
|
|
|
|
settingsHandler.RegisterRoutes(rootRouter)
|
|
|
|
|
|
|
|
// API route registrations
|
|
|
|
summaryApiHandler.RegisterRoutes(apiRouter)
|
|
|
|
healthApiHandler.RegisterRoutes(apiRouter)
|
|
|
|
heartbeatApiHandler.RegisterRoutes(apiRouter)
|
2021-02-12 20:37:30 +03:00
|
|
|
metricsHandler.RegisterRoutes(apiRouter)
|
2021-02-07 00:40:54 +03:00
|
|
|
wakatimeV1AllHandler.RegisterRoutes(apiRouter)
|
|
|
|
wakatimeV1SummariesHandler.RegisterRoutes(apiRouter)
|
|
|
|
wakatimeV1StatsHandler.RegisterRoutes(apiRouter)
|
|
|
|
shieldV1BadgeHandler.RegisterRoutes(apiRouter)
|
2020-09-06 13:15:46 +03:00
|
|
|
|
2020-02-20 16:28:55 +03:00
|
|
|
// Static Routes
|
2021-02-07 14:28:42 +03:00
|
|
|
fileServer := http.FileServer(utils.NeuteredFileSystem{Fs: pkger.Dir("/static")})
|
2021-02-07 13:54:07 +03:00
|
|
|
router.PathPrefix("/assets").Handler(fileServer)
|
|
|
|
router.PathPrefix("/swagger-ui").Handler(fileServer)
|
2021-02-07 14:28:42 +03:00
|
|
|
router.PathPrefix("/docs").Handler(
|
|
|
|
middlewares.NewFileTypeFilterMiddleware([]string{".go"})(fileServer),
|
|
|
|
)
|
2020-02-21 14:41:29 +03:00
|
|
|
|
2019-05-05 23:36:49 +03:00
|
|
|
// Listen HTTP
|
2020-12-13 00:07:00 +03:00
|
|
|
listen(router)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listen(handler http.Handler) {
|
|
|
|
var s4, s6 *http.Server
|
|
|
|
|
|
|
|
// IPv4
|
|
|
|
if config.Server.ListenIpV4 != "" {
|
|
|
|
bindString4 := config.Server.ListenIpV4 + ":" + strconv.Itoa(config.Server.Port)
|
|
|
|
s4 = &http.Server{
|
|
|
|
Handler: handler,
|
|
|
|
Addr: bindString4,
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPv6
|
|
|
|
if config.Server.ListenIpV6 != "" {
|
|
|
|
bindString6 := "[" + config.Server.ListenIpV6 + "]:" + strconv.Itoa(config.Server.Port)
|
|
|
|
s6 = &http.Server{
|
|
|
|
Handler: handler,
|
|
|
|
Addr: bindString6,
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
}
|
2019-05-05 23:36:49 +03:00
|
|
|
}
|
2020-12-13 00:07:00 +03:00
|
|
|
|
|
|
|
if config.UseTLS() {
|
|
|
|
if s4 != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Info("--> Listening for HTTPS on %s... ✅", s4.Addr)
|
2021-01-05 13:28:51 +03:00
|
|
|
go func() {
|
|
|
|
if err := s4.ListenAndServeTLS(config.Server.TlsCertPath, config.Server.TlsKeyPath); err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Fatal(err.Error())
|
2021-01-05 13:28:51 +03:00
|
|
|
}
|
|
|
|
}()
|
2020-12-13 00:07:00 +03:00
|
|
|
}
|
|
|
|
if s6 != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Info("--> Listening for HTTPS on %s... ✅", s6.Addr)
|
2021-01-05 13:28:51 +03:00
|
|
|
go func() {
|
|
|
|
if err := s6.ListenAndServeTLS(config.Server.TlsCertPath, config.Server.TlsKeyPath); err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Fatal(err.Error())
|
2021-01-05 13:28:51 +03:00
|
|
|
}
|
|
|
|
}()
|
2020-12-13 00:07:00 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if s4 != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Info("--> Listening for HTTP on %s... ✅", s4.Addr)
|
2021-01-05 13:28:51 +03:00
|
|
|
go func() {
|
|
|
|
if err := s4.ListenAndServe(); err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Fatal(err.Error())
|
2021-01-05 13:28:51 +03:00
|
|
|
}
|
|
|
|
}()
|
2020-12-13 00:07:00 +03:00
|
|
|
}
|
|
|
|
if s6 != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Info("--> Listening for HTTP on %s... ✅", s6.Addr)
|
2021-01-05 13:28:51 +03:00
|
|
|
go func() {
|
|
|
|
if err := s6.ListenAndServe(); err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
logbuch.Fatal(err.Error())
|
2021-01-05 13:28:51 +03:00
|
|
|
}
|
|
|
|
}()
|
2020-12-13 00:07:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<-make(chan interface{}, 1)
|
2019-05-05 23:36:49 +03:00
|
|
|
}
|