feat: it works
This commit is contained in:
parent
6302dd62d6
commit
778d5ef146
25 changed files with 638 additions and 106 deletions
23
internal/transport/rest/config.go
Normal file
23
internal/transport/rest/config.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
env "github.com/caarlos0/env/v11"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int `env:"PORT" envDefault:"8080"`
|
||||
}
|
||||
|
||||
func NewConfig(servicePrefix string) (*Config, error) {
|
||||
cfg := &Config{}
|
||||
|
||||
if err := env.ParseWithOptions(cfg, env.Options{
|
||||
PrefixTagName: fmt.Sprintf("%s_REST_", servicePrefix),
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
package rest
|
||||
|
||||
type Service interface {
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
api "git.intra.yksa.space/gsn/gsn-proxy/pkg/rest"
|
||||
)
|
||||
|
||||
var (
|
||||
_ api.Handler = (*Handler)(nil)
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
svc Service
|
||||
}
|
||||
|
||||
func (h *Handler) GetSatellites(ctx context.Context) (*api.GetSatellitesOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) GetStations(ctx context.Context) (*api.GetStationsOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) GetSubscriptions(ctx context.Context) (*api.GetSubscriptionsOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) SubscribeSatellite(ctx context.Context, req *api.SubscribeSatelliteReq) (*api.SubscribeSatelliteOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) SubscribeStation(ctx context.Context, req *api.SubscribeStationReq) (*api.SubscribeStationOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) Unsubscribe(ctx context.Context, params api.UnsubscribeParams) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Handler) NewError(ctx context.Context, err error) *api.ErrorStatusCode {
|
||||
return nil
|
||||
}
|
||||
11
internal/transport/rest/handler/deps.go
Normal file
11
internal/transport/rest/handler/deps.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.intra.yksa.space/gsn/gsn-proxy/internal/pkg/ds"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
GetSatellites(ctx context.Context) ([]ds.Satellite, error)
|
||||
}
|
||||
89
internal/transport/rest/handler/handler.go
Normal file
89
internal/transport/rest/handler/handler.go
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.intra.yksa.space/gsn/gsn-proxy/internal/pkg/errcodes"
|
||||
api "git.intra.yksa.space/gsn/gsn-proxy/pkg/rest"
|
||||
)
|
||||
|
||||
var (
|
||||
_ api.Handler = (*Handler)(nil)
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
svc Service
|
||||
}
|
||||
|
||||
func New(svc Service) *Handler {
|
||||
return &Handler{
|
||||
svc: svc,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) GetSatellites(ctx context.Context) (*api.GetSatellitesOK, error) {
|
||||
satellites, err := h.svc.GetSatellites(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret := make([]api.GetSatellitesOKSatellitesItem, 0, len(satellites))
|
||||
for _, val := range satellites {
|
||||
ret = append(ret, api.GetSatellitesOKSatellitesItem{
|
||||
ID: val.ID,
|
||||
DisplayName: val.DisplayName,
|
||||
Status: api.GetSatellitesOKSatellitesItemStatus(val.Status),
|
||||
})
|
||||
}
|
||||
|
||||
return &api.GetSatellitesOK{
|
||||
Satellites: ret,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *Handler) GetStations(ctx context.Context) (*api.GetStationsOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) GetSubscriptions(ctx context.Context) (*api.GetSubscriptionsOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) SubscribeSatellite(ctx context.Context, req *api.SubscribeSatelliteReq) (*api.SubscribeSatelliteOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) SubscribeStation(ctx context.Context, req *api.SubscribeStationReq) (*api.SubscribeStationOK, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *Handler) Unsubscribe(ctx context.Context, params api.UnsubscribeParams) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Handler) NewError(ctx context.Context, err error) *api.ErrorStatusCode {
|
||||
if errcode, ok := err.(*errcodes.ErrorCode); ok {
|
||||
resp := api.Error{
|
||||
Message: errcode.Message,
|
||||
}
|
||||
|
||||
if errcode.Details != "" {
|
||||
resp.Details = api.NewOptString(errcode.Details)
|
||||
}
|
||||
|
||||
return &api.ErrorStatusCode{
|
||||
StatusCode: errcode.StatusCode,
|
||||
Response: resp,
|
||||
}
|
||||
}
|
||||
|
||||
return &api.ErrorStatusCode{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Response: api.Error{
|
||||
Message: fmt.Sprintf("undefined internal error"),
|
||||
Details: api.NewOptString(err.Error()),
|
||||
},
|
||||
}
|
||||
}
|
||||
33
internal/transport/rest/transport.go
Normal file
33
internal/transport/rest/transport.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
handler "git.intra.yksa.space/gsn/gsn-proxy/internal/transport/rest/handler"
|
||||
api "git.intra.yksa.space/gsn/gsn-proxy/pkg/rest"
|
||||
)
|
||||
|
||||
type Transport struct {
|
||||
cfg *Config
|
||||
srv *api.Server
|
||||
}
|
||||
|
||||
func New(handler *handler.Handler, cfg *Config) (*Transport, error) {
|
||||
srv, err := api.NewServer(handler)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Transport{
|
||||
srv: srv,
|
||||
cfg: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *Transport) Run() {
|
||||
if err := http.ListenAndServe(fmt.Sprintf(":%d", t.cfg.Port), t.srv); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue