newsbot-api/cmd/server.go

63 lines
1.4 KiB
Go

package main
import (
"context"
"database/sql"
"fmt"
"net/http"
_ "github.com/glebarez/go-sqlite"
"github.com/pressly/goose/v3"
"git.jamestombleson.com/jtom38/newsbot-api/docs"
v1 "git.jamestombleson.com/jtom38/newsbot-api/internal/handler/v1"
"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() {
ctx := context.Background()
cfg := services.NewConfig()
configs := services.GetEnvConfig()
address := cfg.GetConfig(services.ServerAddress)
docs.SwaggerInfo.Host = fmt.Sprintf("%v:8081", address)
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)
}
//queues := services.NewQueues(db)
//queues.RssIndex.Send(ctx, goqite.Message{
// Body: []byte("hello world"),
//})
c := cron.NewScheduler(ctx, db)
c.Start()
server := v1.NewServer(ctx, configs, db)
fmt.Println("API is online and waiting for requests.")
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)
}
}