Skip to content

Commit

Permalink
create helper functions to parse server && client versions
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJenicek committed Oct 7, 2024
1 parent de79923 commit c3ec55a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 29 deletions.
39 changes: 38 additions & 1 deletion _examples/golang-basics/example.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 13 additions & 25 deletions _examples/golang-basics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,26 @@ func (rpc *ExampleServiceRPC) Status(ctx context.Context) (bool, error) {
}

func (rpc *ExampleServiceRPC) Version(ctx context.Context) (*Version, error) {
req := RequestFromContext(ctx)
writer := ResponseWriterFromContext(ctx)

if writer.Header().Get("Webrpc") == "" {
return nil, fmt.Errorf("server webrpc header is missing")
}

serverVersions, err := ParseWebRPCGenVersions(writer.Header().Get("Webrpc"))
serverVersions, err := ParseServerVersions(ctx)
if err != nil {
return nil, fmt.Errorf("parse server webrpc gen versions: %w", err)
}

var clientVersions *GenVersions
if req.Header.Get("Webrpc") != "" {
parsedVersions, err := ParseWebRPCGenVersions(req.Header.Get("Webrpc"))
if err != nil {
return nil, fmt.Errorf("parse client webrpc gen versions: %w", err)
}

clientVersions = &GenVersions{
WebRPCGenVersion: parsedVersions.WebRPCGenVersion,
TmplTarget: parsedVersions.TmplTarget,
TmplVersion: parsedVersions.TmplVersion,
SchemaVersion: parsedVersions.SchemaVersion,
}
clientVersions, err := ParseClientVersions(ctx)
if err != nil {
return nil, fmt.Errorf("parse client webrpc gen versions: %w", err)
}

return &Version{
WebrpcVersion: WebRPCVersion(),
SchemaVersion: WebRPCSchemaVersion(),
SchemaHash: WebRPCSchemaHash(),
ClientGenVersion: clientVersions,
WebrpcVersion: WebRPCVersion(),
SchemaVersion: WebRPCSchemaVersion(),
SchemaHash: WebRPCSchemaHash(),
ClientGenVersion: &GenVersions{
WebRPCGenVersion: clientVersions.WebRPCGenVersion,
TmplTarget: clientVersions.TmplTarget,
TmplVersion: clientVersions.TmplVersion,
SchemaVersion: clientVersions.SchemaVersion,
},
ServerGenVersion: &GenVersions{
WebRPCGenVersion: serverVersions.WebRPCGenVersion,
TmplTarget: serverVersions.TmplTarget,
Expand Down
40 changes: 39 additions & 1 deletion _examples/golang-imports/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 39 additions & 1 deletion main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ package {{get $opts "pkg"}}

{{template "imports" dict "Types" .Types "Opts" $opts }}

const WebRPCVersionHeader = "Webrpc"

// WebRPC description and code-gen version
func WebRPCVersion() string {
return "{{.WebrpcVersion}}"
Expand Down Expand Up @@ -110,7 +112,43 @@ func WebRPCHeader() string {
return "webrpc@{{.WebrpcGenVersion}};{{.TmplTarget}}@{{ if eq .TmplVersion "" }}unknown{{ else }}{{.TmplVersion}}{{end}};{{.SchemaName}}@{{.SchemaVersion}}"
}

func ParseWebRPCGenVersions(header string) (*WebRPCGenVersions, error) {
func ParseServerVersions(ctx context.Context) (*WebRPCGenVersions, error) {
writer := ResponseWriterFromContext(ctx)

header := writer.Header().Get(WebRPCVersionHeader)
if header == "" {
return nil, fmt.Errorf("webrpc header is missing")
}

versions, err := parseWebRPCGenVersions(header)
if err != nil {
return nil, fmt.Errorf("webrpc header is invalid: %w", err)
}

return versions, nil
}

func ParseClientVersions(ctx context.Context) (*WebRPCGenVersions, error) {
req := RequestFromContext(ctx)
if req == nil {
return nil, fmt.Errorf("request is nil")
}

header := req.Header.Get(WebRPCVersionHeader)
if header == "" {
return nil, fmt.Errorf("webrpc header is missing")
}

versions, err := parseWebRPCGenVersions(header)
if err != nil {
return nil, fmt.Errorf("webrpc header is invalid: %w", err)
}

return versions, nil
}


func parseWebRPCGenVersions(header string) (*WebRPCGenVersions, error) {
versions := strings.Split(header, ";")
if len(versions) < 3 {
return nil, fmt.Errorf("expected at least 3 parts while parsing webrpc header: %s", header)
Expand Down
2 changes: 1 addition & 1 deletion server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}()

w.Header().Set("Webrpc", WebRPCHeader())
w.Header().Set(WebRPCVersionHeader, WebRPCHeader())

ctx := r.Context()
ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w)
Expand Down

0 comments on commit c3ec55a

Please sign in to comment.