Skip to content

Commit

Permalink
adsfadsf
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmossas committed Sep 6, 2024
1 parent 621ed71 commit adbef2a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
)

require (
github.com/google/uuid v1.6.0
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
Expand Down
13 changes: 10 additions & 3 deletions languages/go/go-server/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func NewApp[TContext any](mux *http.ServeMux, options AppOptions[TContext], crea
onError = func(r *http.Request, t *TContext, err error) {}
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
ctx, ctxErr := app.CreateContext(r)
if ctxErr != nil {
handleError(false, w, r, ctx, ctxErr.ErrorResponse(), onError)
Expand Down Expand Up @@ -184,6 +185,7 @@ func NewApp[TContext any](mux *http.ServeMux, options AppOptions[TContext], crea
})

mux.HandleFunc(defPath, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
ctx, ctxErr := app.CreateContext(r)
if ctxErr != nil {
handleError(false, w, r, ctx, ctxErr.ErrorResponse(), onError)
Expand Down Expand Up @@ -223,7 +225,7 @@ func handleError[TContext any](
return
}
w.WriteHeader(int(err.Code))
body, _ := ToJson(err, KeyCasingCamelCase)
body := err.ToJson()
w.Write(body)
}

Expand Down Expand Up @@ -287,6 +289,7 @@ func rpc[TParams, TResponse, TContext any](app *App[TContext], options *RpcOptio

paramsZero := reflect.Zero(reflect.TypeFor[TParams]())
app.Mux.HandleFunc(rpcSchema.Http.Path, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
ctx, ctxErr := app.CreateContext(r)
if ctxErr != nil {
handleError(false, w, r, nil, ctxErr.ErrorResponse(), app.Options.OnError)
Expand All @@ -308,8 +311,12 @@ func rpc[TParams, TResponse, TContext any](app *App[TContext], options *RpcOptio
}
switch rpcSchema.Http.Method {
case HttpMethodGet:
handleError(false, w, r, ctx, ErrorResponse{Code: 400, Message: "Get requests not yet supported"}, onError)
return
urlValues := r.URL.Query()
fromUrlQueryErr := FromUrlQuery(urlValues, &params, app.Options.KeyCasing)
if fromUrlQueryErr != nil {
handleError(false, w, r, ctx, fromUrlQueryErr.ErrorResponse(), onError)
return
}
default:
b, bErr := io.ReadAll(r.Body)
if bErr != nil {
Expand Down
4 changes: 2 additions & 2 deletions languages/go/go-server/decode_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ type ValidationError struct {
}

func (e ValidationError) Error() string {
return e.Message + " at \"" + e.InstancePath + "\""
return e.Message + " at " + e.InstancePath
}

func (e ValidationError) ErrorResponse() ErrorResponse {
return ErrorResponse{
Code: 400,
Message: e.Message + " at \"" + e.InstancePath + "\"",
Message: e.Message + " at " + e.InstancePath,
Data: Some[any](e),
Stack: None[[]string](),
}
Expand Down
5 changes: 4 additions & 1 deletion languages/go/go-server/decode_url_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func FromUrlQuery[T any](values url.Values, target *T, keyCasing KeyCasing) *Val

}
urlValue := values.Get(key)

isOptional := isOptionalType(fieldType)
if isOptional {
ctx := ctx.copyWith(
Expand All @@ -59,6 +58,10 @@ func FromUrlQuery[T any](values url.Values, target *T, keyCasing KeyCasing) *Val
}
continue
}
hasUrlValue := values.Has(key)
if !hasUrlValue {
return &ValidationError{Message: "missing required field", InstancePath: "/" + key, SchemaPath: "/properties/" + key}
}
ctx := ctx.copyWith(
None[uint32](),
Some(enumValues),
Expand Down
2 changes: 1 addition & 1 deletion languages/go/go-server/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (e ErrorResponse) ErrorResponse() ErrorResponse {

func (e ErrorResponse) ToJson() []byte {
output := []byte{}
output = append(output, "{\"code\":"+strconv.FormatUint(uint64(e.Code), 32)...)
output = append(output, "{\"code\":"+strconv.FormatUint(uint64(e.Code), 10)...)
output = append(output, ",\"message\":"...)
appendNormalizedString(&output, e.Message)
if e.Data.IsSome() {
Expand Down
16 changes: 13 additions & 3 deletions playground/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import (
"log"
"net/http"
"time"

"github.com/google/uuid"
)

type MyCustomContext struct{}
type MyCustomContext struct {
ReqId string
ReqStart time.Time
IsLoggedIn bool
}

func onRequest(r *http.Request, c MyCustomContext) arri.Error {
fmt.Println("NEW REQUEST", r.URL.Path)
Expand All @@ -33,11 +39,15 @@ func main() {
// create the RPC context for each request
// this is generic so users can user whatever struct they want for their context
(func(r *http.Request) (*MyCustomContext, arri.Error) {
return &MyCustomContext{}, nil
ctx := MyCustomContext{
ReqId: uuid.New().String(),
ReqStart: time.Now(),
}
return &ctx, nil
}),
)
// register an RPC
arri.Rpc(&app, GetUser)
arri.RpcWithOptions(&app, arri.RpcOptions{Method: arri.HttpMethodGet}, GetUser)
arri.Rpc(&app, DeleteUser)
// register an RPC with a custom HTTP method and path
arri.RpcWithOptions(&app, arri.RpcOptions{
Expand Down

0 comments on commit adbef2a

Please sign in to comment.