mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-07 04:32:24 +00:00
Initial commit: not functional
This commit is contained in:
@@ -14,20 +14,19 @@
|
||||
package general_server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
"github.com/akyaiy/GoSally-mvp/core/config"
|
||||
"github.com/akyaiy/GoSally-mvp/core/utils"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// serversApiVer is a type alias for string, used to represent API version strings in the GeneralServer.
|
||||
type serversApiVer string
|
||||
|
||||
/*
|
||||
// GeneralServerApiContract defines the interface for servers that can be registered
|
||||
type GeneralServerApiContract interface {
|
||||
// GetVersion returns the API version of the server.
|
||||
@@ -37,9 +36,13 @@ type GeneralServerApiContract interface {
|
||||
Handle(w http.ResponseWriter, r *http.Request)
|
||||
HandleList(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
*/
|
||||
|
||||
type GeneralServerApiContract interface {
|
||||
GetVersion() string
|
||||
Handle(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
// GeneralServerContarct extends the GeneralServerApiContract with a method to append new servers.
|
||||
// This interface is only for general server initialization and does not need to be implemented by individual servers.
|
||||
type GeneralServerContarct interface {
|
||||
GeneralServerApiContract
|
||||
// AppendToArray adds a new server to the GeneralServer's internal map.
|
||||
@@ -61,7 +64,7 @@ type GeneralServer struct {
|
||||
|
||||
// GeneralServerInit structure only for initialization general server.
|
||||
type GeneralServerInit struct {
|
||||
Log slog.Logger
|
||||
Log *slog.Logger
|
||||
Config *config.Conf
|
||||
}
|
||||
|
||||
@@ -70,7 +73,7 @@ func InitGeneral(o *GeneralServerInit, servers ...GeneralServerApiContract) *Gen
|
||||
general := &GeneralServer{
|
||||
servers: make(map[serversApiVer]GeneralServerApiContract),
|
||||
cfg: o.Config,
|
||||
log: o.Log,
|
||||
log: *o.Log,
|
||||
}
|
||||
|
||||
// register the provided servers
|
||||
@@ -95,6 +98,62 @@ func (s *GeneralServer) AppendToArray(server GeneralServerApiContract) error {
|
||||
return errors.New("server with this version is already exist")
|
||||
}
|
||||
|
||||
func (s *GeneralServer) Handle(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
WriteRouterError(w, &RouterError{
|
||||
Status: "error",
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Payload: map[string]any{
|
||||
"Message": IssueMethod,
|
||||
},
|
||||
})
|
||||
s.log.Info("invalid request received", slog.String("issue", IssueMethod), slog.String("requested-method", r.Method))
|
||||
return
|
||||
}
|
||||
var payload RawPettiEnvelope
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
WriteRouterError(w, &RouterError{
|
||||
Status: "error",
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Payload: map[string]any{
|
||||
"Message": IssueToReadBody,
|
||||
},
|
||||
})
|
||||
s.log.Info("invalid request received", slog.String("issue", IssueToReadBody))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
WriteRouterError(w, &RouterError{
|
||||
Status: "error",
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Payload: map[string]any{
|
||||
"Message": InvalidProtocol,
|
||||
},
|
||||
})
|
||||
s.log.Info("invalid request received", slog.String("issue", InvalidProtocol))
|
||||
return
|
||||
}
|
||||
|
||||
server, ok := s.servers[serversApiVer(payload.PettiVer)]
|
||||
if !ok {
|
||||
WriteRouterError(w, &RouterError{
|
||||
Status: "error",
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Payload: map[string]any{
|
||||
"Message": InvalidProtovolVersion,
|
||||
},
|
||||
})
|
||||
s.log.Info("invalid request received", slog.String("issue", InvalidProtovolVersion), slog.String("requested-version", payload.PettiVer))
|
||||
return
|
||||
}
|
||||
|
||||
server.Handle(w, r)
|
||||
}
|
||||
|
||||
/*
|
||||
// Handle processes incoming HTTP requests, routing them to the appropriate server based on the API version.
|
||||
// It checks if the requested version is registered and handles the request accordingly.
|
||||
func (s *GeneralServer) Handle(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -188,3 +247,4 @@ func (s *GeneralServer) HandleList(w http.ResponseWriter, r *http.Request) {
|
||||
s.log.Error("Failed to write JSON", slog.String("err", err.Error()))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
8
core/general_server/issue.go
Normal file
8
core/general_server/issue.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package general_server
|
||||
|
||||
const (
|
||||
IssueMethod = "invalid request method"
|
||||
IssueToReadBody = "unable to read body"
|
||||
InvalidProtocol = "unknown protocol or missing PettiVer field"
|
||||
InvalidProtovolVersion = "unsupported PETTI version"
|
||||
)
|
||||
49
core/general_server/protocol.go
Normal file
49
core/general_server/protocol.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package general_server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type RawPettiEnvelope struct {
|
||||
PettiVer string `json:"PettiVer"`
|
||||
}
|
||||
|
||||
type RouterError struct {
|
||||
Status string `json:"Status"`
|
||||
StatusCode int `json:"StatusCode"`
|
||||
Payload map[string]any `json:"Payload"`
|
||||
}
|
||||
|
||||
func WriteRouterError(w http.ResponseWriter, e *RouterError) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(e.StatusCode)
|
||||
|
||||
data, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write(data)
|
||||
return err
|
||||
}
|
||||
|
||||
// func WriteRouterError(w http.ResponseWriter, e *RouterError) error {
|
||||
// resp := RouterError{
|
||||
// Status: e.Status,
|
||||
// StatusCode: e.StatusCode,
|
||||
// Payload: e.Payload,
|
||||
// }
|
||||
// b, err := json.Marshal(resp)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// var formatted bytes.Buffer
|
||||
// if err := json.Indent(&formatted, b, "", " "); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// w.Header().Set("Content-Type", "application/json")
|
||||
// w.WriteHeader(e.StatusCode)
|
||||
// formatted.WriteTo(w)
|
||||
// return nil
|
||||
// }
|
||||
Reference in New Issue
Block a user