2022-04-02 12:05:32 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-06-08 21:17:08 -07:00
|
|
|
"context"
|
2023-01-22 10:12:55 -08:00
|
|
|
"database/sql"
|
2024-05-05 10:02:17 -07:00
|
|
|
"errors"
|
2022-06-19 22:02:44 -07:00
|
|
|
"fmt"
|
2024-05-05 10:02:17 -07:00
|
|
|
"os"
|
2022-07-14 09:59:55 -07:00
|
|
|
|
2024-04-26 16:02:14 -07:00
|
|
|
_ "github.com/glebarez/go-sqlite"
|
|
|
|
"github.com/pressly/goose/v3"
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/docs"
|
2024-05-01 18:27:03 -07:00
|
|
|
v1 "git.jamestombleson.com/jtom38/newsbot-api/internal/handler/v1"
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/services/cron"
|
2022-04-02 12:05:32 -07:00
|
|
|
)
|
|
|
|
|
2024-07-17 22:01:29 -07:00
|
|
|
// @title NewsBot collector
|
|
|
|
// @version 0.1
|
|
|
|
// @BasePath /api
|
|
|
|
// @securityDefinitions.apikey Bearer
|
|
|
|
// @in header
|
|
|
|
// @name Authorization
|
|
|
|
// @description Type "Bearer" followed by a space and JWT token.
|
2022-04-02 12:05:32 -07:00
|
|
|
func main() {
|
2024-04-26 16:02:14 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
cfg := services.NewConfig()
|
2024-04-26 16:02:14 -07:00
|
|
|
configs := services.GetEnvConfig()
|
2024-04-23 07:15:38 -07:00
|
|
|
address := cfg.GetConfig(services.ServerAddress)
|
2022-07-14 09:59:55 -07:00
|
|
|
docs.SwaggerInfo.Host = fmt.Sprintf("%v:8081", address)
|
|
|
|
|
2024-04-26 16:02:14 -07:00
|
|
|
db, err := sql.Open("sqlite", "newsbot.db")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
err = migrateDatabase(db)
|
2024-04-26 16:02:14 -07:00
|
|
|
if err != nil {
|
2024-05-05 10:02:17 -07:00
|
|
|
fmt.Print(err)
|
2023-01-22 10:12:55 -08:00
|
|
|
}
|
|
|
|
|
2024-05-01 18:27:03 -07:00
|
|
|
c := cron.NewScheduler(ctx, db)
|
2022-06-19 22:02:44 -07:00
|
|
|
c.Start()
|
2022-06-08 21:17:08 -07:00
|
|
|
|
2024-05-02 17:38:30 -07:00
|
|
|
server := v1.NewServer(ctx, configs, db)
|
2022-04-02 12:05:32 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
fmt.Println("API is online and waiting for requests.")
|
2024-04-26 16:02:14 -07:00
|
|
|
fmt.Printf("API: http://%v:8081/api\r\n", configs.ServerAddress)
|
|
|
|
fmt.Printf("Swagger: http://%v:8081/swagger/index.html\r\n", configs.ServerAddress)
|
2022-07-14 09:59:55 -07:00
|
|
|
|
2024-05-02 17:38:30 -07:00
|
|
|
server.Router.Start(":8081")
|
2022-12-04 08:49:17 -08:00
|
|
|
}
|
2024-05-05 10:02:17 -07:00
|
|
|
|
|
|
|
func migrateDatabase(db *sql.DB) error {
|
|
|
|
err := goose.SetDialect("sqlite3")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = goose.Up(db, "../internal/database/migrations")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat("./migrations")
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
err = goose.Up(db, "../internal/database/migrations")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat("../internal/database/migrations")
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
err = goose.Up(db, "../internal/database/migrations")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("failed to find the migration files")
|
|
|
|
}
|