list daily weathers api 구현
This commit is contained in:
67
cmd/seven-skies/handler.go
Normal file
67
cmd/seven-skies/handler.go
Normal file
@@ -0,0 +1,67 @@
|
||||
//nolint:ireturn
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/neatflowcv/seven-skies/api"
|
||||
"github.com/neatflowcv/seven-skies/internal/app/flow"
|
||||
)
|
||||
|
||||
var _ api.StrictServerInterface = (*Handler)(nil)
|
||||
|
||||
type Handler struct {
|
||||
flow *flow.Flow
|
||||
}
|
||||
|
||||
func NewHandler(flow *flow.Flow) *Handler {
|
||||
return &Handler{
|
||||
flow: flow,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ForecastsListDaily(
|
||||
ctx context.Context,
|
||||
req api.ForecastsListDailyRequestObject,
|
||||
) (api.ForecastsListDailyResponseObject, error) {
|
||||
timezone := "Asia/Seoul"
|
||||
if req.Params.Timezone != nil {
|
||||
timezone = *req.Params.Timezone
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if req.Params.Now != nil {
|
||||
now = *req.Params.Now
|
||||
}
|
||||
|
||||
weathers, err := h.flow.ListDailyWeathers(ctx, timezone, now)
|
||||
if err != nil {
|
||||
return api.ForecastsListDaily500JSONResponse{ //nolint:nilerr
|
||||
ErrorCode: http.StatusInternalServerError,
|
||||
ErrorMessage: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
var items []api.DailyForecast
|
||||
for _, weather := range weathers {
|
||||
items = append(items, api.DailyForecast{
|
||||
Date: weather.Date,
|
||||
High: api.Celsius(weather.High),
|
||||
Low: api.Celsius(weather.Low),
|
||||
Condition: api.WeatherCondition(weather.Condition),
|
||||
})
|
||||
}
|
||||
|
||||
return api.ForecastsListDaily200JSONResponse{
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *Handler) ForecastsListHourly(
|
||||
ctx context.Context,
|
||||
req api.ForecastsListHourlyRequestObject,
|
||||
) (api.ForecastsListHourlyResponseObject, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
@@ -5,10 +5,14 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/neatflowcv/seven-skies/api"
|
||||
"github.com/neatflowcv/seven-skies/internal/app/flow"
|
||||
"github.com/neatflowcv/seven-skies/internal/pkg/broker/nats"
|
||||
"github.com/neatflowcv/seven-skies/internal/pkg/domain"
|
||||
@@ -98,5 +102,25 @@ func main() {
|
||||
log.Panic("error in subscribe", err)
|
||||
}
|
||||
|
||||
select {}
|
||||
err = serve(service)
|
||||
if err != nil {
|
||||
log.Panic("error in serve", err)
|
||||
}
|
||||
}
|
||||
|
||||
func serve(service *flow.Flow) error {
|
||||
const timeout = 5 * time.Second
|
||||
|
||||
server := &http.Server{ //nolint:exhaustruct
|
||||
ReadHeaderTimeout: timeout,
|
||||
Handler: api.HandlerFromMux(api.NewStrictHandler(NewHandler(service), nil), chi.NewRouter()),
|
||||
Addr: "0.0.0.0:8080",
|
||||
}
|
||||
|
||||
err := server.ListenAndServe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error in listen and serve: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user