newsbot-api/routes/server.go
James Tombleson ada453e08a
Features/delete source and first dto (#36)
* updated db, added dto for ListSources, and added delete source

* updated from model > models

* updated to models

* sources now sends back a standard message

* updated subscription routes to have beter logid and swagger details

* moved the dto objects back to modles given they are not bound to the database

* cleaned up how we return the error

* cleaned up swag and updated models to take from the base apistatusmodel. less human errors this way

* cleaned up swag and updated models

* swag updated

* updated queue to return a router and also renamed it as it will hold all queue info later on

* removed config tag

* added subscription details route

* article routes have been moved to support dto

* updated discordwebhooks to use dto

* updated discordwebhookqueue to return details on the items via dto

* removed the example routes

* updated sources to use dto

* subscriptions moved to dto

* generated swag
2023-01-22 10:12:55 -08:00

115 lines
2.5 KiB
Go

package routes
import (
"context"
"database/sql"
"encoding/json"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/lib/pq"
httpSwagger "github.com/swaggo/http-swagger"
"github.com/jtom38/newsbot/collector/database"
"github.com/jtom38/newsbot/collector/dto"
"github.com/jtom38/newsbot/collector/services/config"
)
type Server struct {
Router *chi.Mux
Db *database.Queries
dto dto.DtoClient
ctx *context.Context
}
const (
HeaderContentType = "Content-Type"
ApplicationJson = "application/json"
)
var (
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."
ErrUnableToConvertToJson string = "Unable to convert to json"
)
func NewServer(ctx context.Context, db *database.Queries) *Server {
s := &Server{
ctx: &ctx,
Db: db,
dto: dto.NewDtoClient(db),
}
//db, err := openDatabase(ctx)
//if err != nil {
// panic(err)
//}
//s.Db = db
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)
//s.Router.Use(middleware.Heartbeat())
}
func (s *Server) MountRoutes() {
s.Router.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("doc.json"), //The url pointing to API definition
))
s.Router.Mount("/api/articles", s.GetArticleRouter())
s.Router.Mount("/api/queue", s.GetQueueRouter())
s.Router.Mount("/api/discord/webhooks", s.DiscordWebHookRouter())
//s.Router.Get("/api/settings", s.getSettings)
s.Router.Mount("/api/sources", s.GetSourcesRouter())
s.Router.Mount("/api/subscriptions", s.GetSubscriptionsRouter())
}
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,
},
}
b, err := json.Marshal(e)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write(b)
}