maps-service/server.go

84 lines
2.0 KiB
Go
Raw Normal View History

2024-11-26 01:22:45 +00:00
package main
import (
"context"
"log"
"net/http"
2024-11-26 22:03:47 +00:00
"git.itzana.me/strafesnet/maps-service/api"
"git.itzana.me/strafesnet/maps-service/internal/controller/submissions"
"git.itzana.me/strafesnet/maps-service/internal/controller/users"
2024-11-26 01:22:45 +00:00
)
type apiServer struct {
2024-11-26 22:03:47 +00:00
submissions *submissions.Submissions
users *users.Users
2024-11-26 01:22:45 +00:00
}
// GetUserRank implements api.Handler.
2024-11-26 22:03:47 +00:00
func (m *apiServer) GetSubmission(ctx context.Context, params api.GetSubmissionParams) (*api.Submission, error) {
2024-11-26 22:10:30 +00:00
return m.submissions.Get(ctx,params)
2024-11-26 01:22:45 +00:00
}
// NewError implements api.Handler.
func (m *apiServer) NewError(ctx context.Context, err error) *api.ErrorStatusCode {
return &api.ErrorStatusCode{
StatusCode: 500,
Response: api.Error{Message: err.Error()},
}
}
func main() {
// new grpc client
conn, err := grpc.Dial("localhost:9000", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatal(err)
}
svc := &apiServer{
client: conn,
}
srv, err := api.NewServer(
svc,
api.WithPathPrefix("/v2"),
)
if err != nil {
log.Fatal(err)
}
if err := http.ListenAndServe(":8080", srv); err != nil {
log.Fatal(err)
}
}
func convertTime(t *times.TimeResponse) api.Time {
return api.Time{
ID: api.NewOptInt64(t.ID),
Time: api.NewOptInt64(t.Time),
User: api.NewOptUser(api.User{
ID: api.NewOptInt64(t.User.ID),
Username: api.NewOptString(t.User.Username),
StateID: api.NewOptInt32(t.User.StateID),
}),
Map: api.NewOptMap(api.Map{
ID: api.NewOptInt64(t.Map.ID),
DisplayName: api.NewOptString(t.Map.DisplayName),
Creator: api.NewOptString(t.Map.Creator),
Date: api.NewOptInt64(t.Map.Date),
}),
Date: api.NewOptInt64(t.Date),
StyleID: api.NewOptInt32(t.StyleID),
ModeID: api.NewOptInt32(t.ModeID),
GameID: api.NewOptInt32(t.GameID),
}
}
func convertTimes(timeList []*times.TimeResponse) []api.Time {
times := make([]api.Time, len(timeList))
for i, t := range timeList {
times[i] = convertTime(t)
}
return times
}