newsbot-api/cmd/server.go

61 lines
1.4 KiB
Go
Raw Normal View History

package main
import (
"context"
"database/sql"
"fmt"
"net/http"
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"
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
2024-04-26 16:02:14 -07:00
"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"
)
// @title NewsBot collector
// @version 0.1
// @BasePath /api
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)
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)
}
err = goose.SetDialect("sqlite3")
if err != nil {
panic(err)
}
err = goose.Up(db, "../internal/database/migrations")
if err != nil {
panic(err)
}
queries := database.New(db)
2024-04-23 07:15:38 -07:00
c := cron.NewScheduler(ctx)
c.Start()
2024-04-26 16:02:14 -07:00
server := v1.NewServer(ctx, queries, configs, db)
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)
err = http.ListenAndServe(":8081", server.Router)
if err != nil {
panic(err)
}
}