2022-06-19 22:02:44 -07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2022-12-10 12:17:25 -08:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
_ "github.com/lib/pq"
|
2022-07-12 15:28:31 -07:00
|
|
|
httpSwagger "github.com/swaggo/http-swagger"
|
2022-06-19 22:02:44 -07:00
|
|
|
|
|
|
|
"github.com/jtom38/newsbot/collector/database"
|
2023-01-21 16:08:12 -08:00
|
|
|
"github.com/jtom38/newsbot/collector/dto"
|
2022-06-19 22:02:44 -07:00
|
|
|
"github.com/jtom38/newsbot/collector/services/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Router *chi.Mux
|
2022-07-12 15:28:31 -07:00
|
|
|
Db *database.Queries
|
2023-01-21 16:08:12 -08:00
|
|
|
dto dto.DtoClient
|
2022-07-12 15:28:31 -07:00
|
|
|
ctx *context.Context
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-21 12:20:33 -08:00
|
|
|
const (
|
|
|
|
HeaderContentType = "Content-Type"
|
|
|
|
|
|
|
|
ApplicationJson = "application/json"
|
|
|
|
)
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
var (
|
2022-07-12 15:28:31 -07:00
|
|
|
ErrIdValueMissing string = "id value is missing"
|
|
|
|
ErrValueNotUuid string = "a value given was expected to be a uuid but was not correct."
|
|
|
|
ErrNoRecordFound string = "no record was found."
|
2022-06-19 22:02:44 -07:00
|
|
|
ErrUnableToConvertToJson string = "Unable to convert to json"
|
|
|
|
)
|
|
|
|
|
2023-01-21 16:08:12 -08:00
|
|
|
func NewServer(ctx context.Context, db *database.Queries) *Server {
|
2022-06-19 22:02:44 -07:00
|
|
|
s := &Server{
|
|
|
|
ctx: &ctx,
|
2023-01-21 16:08:12 -08:00
|
|
|
Db: db,
|
|
|
|
dto: dto.NewDtoClient(db),
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-21 16:08:12 -08:00
|
|
|
//db, err := openDatabase(ctx)
|
|
|
|
//if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
//}
|
|
|
|
//s.Db = db
|
2022-06-19 22:02:44 -07:00
|
|
|
|
|
|
|
s.Router = chi.NewRouter()
|
|
|
|
s.MountMiddleware()
|
|
|
|
s.MountRoutes()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func openDatabase(ctx context.Context) (*database.Queries, error) {
|
|
|
|
_env := config.New()
|
|
|
|
connString := _env.GetConfig(config.Sql_Connection_String)
|
|
|
|
db, err := sql.Open("postgres", connString)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
queries := database.New(db)
|
|
|
|
return queries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) MountMiddleware() {
|
|
|
|
s.Router.Use(middleware.Logger)
|
|
|
|
s.Router.Use(middleware.Recoverer)
|
2022-07-12 15:28:31 -07:00
|
|
|
//s.Router.Use(middleware.Heartbeat())
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) MountRoutes() {
|
|
|
|
s.Router.Get("/swagger/*", httpSwagger.Handler(
|
2022-11-06 17:17:46 -08:00
|
|
|
httpSwagger.URL("doc.json"), //The url pointing to API definition
|
2022-06-19 22:02:44 -07:00
|
|
|
))
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2023-01-21 16:08:12 -08:00
|
|
|
s.Router.Mount("/api/articles", s.GetArticleRouter())
|
2022-12-10 12:17:25 -08:00
|
|
|
s.Router.Mount("/api/queue", s.GetQueueRouter())
|
2023-01-21 17:38:17 -08:00
|
|
|
s.Router.Mount("/api/discord/webhooks", s.DiscordWebHookRouter())
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
s.Router.Get("/api/settings", s.getSettings)
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2022-12-10 12:17:25 -08:00
|
|
|
s.Router.Mount("/api/sources", s.GetSourcesRouter())
|
|
|
|
s.Router.Mount("/api/subscriptions", s.GetSubscriptionsRouter())
|
|
|
|
}
|
|
|
|
|
2022-12-11 09:46:50 -08:00
|
|
|
type ApiStatusModel struct {
|
|
|
|
StatusCode int `json:"status"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApiError struct {
|
|
|
|
*ApiStatusModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int) {
|
|
|
|
e := ApiError{
|
|
|
|
ApiStatusModel: &ApiStatusModel{
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
Message: errMessage,
|
|
|
|
},
|
2022-12-10 12:17:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(b)
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|