init: first steps
This commit is contained in:
12
router/controller/controller.go
Normal file
12
router/controller/controller.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/go-andiamo/chioas"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Controller interface {
|
||||
New() Controller
|
||||
Group(r chi.Router)
|
||||
Documentate() (*chioas.Paths, *chioas.Components)
|
||||
}
|
||||
57
router/controller/ping/controller.go
Normal file
57
router/controller/ping/controller.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package ping
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/log"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
||||
"github.com/go-andiamo/chioas"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
}
|
||||
|
||||
func (Controller) New() controller.Controller {
|
||||
return &Controller{}
|
||||
}
|
||||
|
||||
func (c *Controller) Group(r chi.Router) {
|
||||
|
||||
r.Get("/ping", c.PingHandler)
|
||||
}
|
||||
|
||||
func (c *Controller) PingHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
_, err := w.Write([]byte("pong"))
|
||||
if err != nil {
|
||||
log.Global.Get(log.SERVER).Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) Documentate() (*chioas.Paths, *chioas.Components) {
|
||||
return &chioas.Paths{
|
||||
"/ping": chioas.Path{
|
||||
Methods: map[string]chioas.Method{
|
||||
http.MethodGet: {
|
||||
Handler: c.PingHandler,
|
||||
Responses: chioas.Responses{
|
||||
http.StatusOK: {
|
||||
ContentType: "plain/text",
|
||||
Schema: chioas.Schema{Type: "string"},
|
||||
Examples: chioas.Examples{
|
||||
{
|
||||
|
||||
Name: "200 OK",
|
||||
Value: "pong",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Tag: "service",
|
||||
Comment: "Route for check service is alive",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
106
router/controller/service/controller.go
Normal file
106
router/controller/service/controller.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/log"
|
||||
http2 "git.ostiwe.com/ostiwe-com/status/pkg/http"
|
||||
"git.ostiwe.com/ostiwe-com/status/repository"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
||||
"git.ostiwe.com/ostiwe-com/status/transform"
|
||||
"github.com/go-andiamo/chioas"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
serviceRepository repository.Service
|
||||
}
|
||||
|
||||
func (c Controller) New() controller.Controller {
|
||||
return &Controller{
|
||||
serviceRepository: repository.NewServiceRepository(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) Group(r chi.Router) {
|
||||
c.public(r)
|
||||
c.internal(r)
|
||||
}
|
||||
|
||||
func (c *Controller) public(r chi.Router) {
|
||||
r.Get("/api/public/service", c.GetAllServicesPublic)
|
||||
}
|
||||
|
||||
func (c *Controller) internal(r chi.Router) {
|
||||
r.Get("/api/v1/service", c.GetAllServices)
|
||||
}
|
||||
|
||||
func (c *Controller) GetAllServicesPublic(w http.ResponseWriter, r *http.Request) {
|
||||
limit, offset, errReady := http2.ExtractLimitOffset(r)
|
||||
if errReady != nil {
|
||||
err := errReady.Send(w)
|
||||
if err != nil {
|
||||
log.Global.Get(log.SERVER).Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
items, err := c.serviceRepository.All(r.Context(), limit, offset, true)
|
||||
if err != nil {
|
||||
log.Global.Get(log.SERVER).Error(err)
|
||||
|
||||
writeErr := http2.NewResponseErrBuilder().
|
||||
WithMessage("Fetch service error").
|
||||
WithStatusCode(http.StatusInternalServerError).
|
||||
Send(w, r)
|
||||
if writeErr != nil {
|
||||
log.Global.Get(log.SERVER).Error(writeErr)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = http2.JSON(w, transform.PublicServices(items...), http.StatusOK)
|
||||
if err != nil {
|
||||
log.Global.Get(log.SERVER).Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) GetAllServices(w http.ResponseWriter, r *http.Request) {
|
||||
limit, offset, errReady := http2.ExtractLimitOffset(r)
|
||||
if errReady != nil {
|
||||
err := errReady.Send(w)
|
||||
if err != nil {
|
||||
log.Global.Get(log.SERVER).Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
items, err := c.serviceRepository.All(r.Context(), limit, offset, false)
|
||||
if err != nil {
|
||||
log.Global.Get(log.SERVER).Error(err)
|
||||
|
||||
writeErr := http2.NewResponseErrBuilder().
|
||||
WithMessage("Fetch service error").
|
||||
WithStatusCode(http.StatusInternalServerError).
|
||||
Send(w, r)
|
||||
if writeErr != nil {
|
||||
log.Global.Get(log.SERVER).Error(writeErr)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = http2.JSON(w, items, http.StatusOK)
|
||||
if err != nil {
|
||||
log.Global.Get(log.SERVER).Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) Documentate() (*chioas.Paths, *chioas.Components) {
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user