1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package main
import (
"bytes"
"flag"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"slices"
"strings"
"text/template"
)
func main() {
inputFile := flag.String("i", "", "input file")
outputFile := flag.String("o", "", "output file")
flag.Parse()
if inputFile == nil || *inputFile == "" || outputFile == nil || *outputFile == "" {
flag.PrintDefaults()
os.Exit(1)
}
// Parse the input file
fileSet := token.NewFileSet()
parsedFile, err := parser.ParseFile(fileSet, *inputFile, nil, parser.SkipObjectResolution)
if err != nil {
panic(err)
}
// Find methods in StrictServerInterface
var methods []string
for _, decl := range parsedFile.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
if typeSpec.Name.Name != "StrictServerInterface" {
continue
}
interfaceType, ok := typeSpec.Type.(*ast.InterfaceType)
if !ok {
continue
}
for _, method := range interfaceType.Methods.List {
if len(method.Names) != 0 {
methods = append(methods, method.Names[0].Name)
}
}
}
}
if len(methods) == 0 {
panic("StrictServerInterface not found")
}
slices.Sort(methods)
type TemplateParameter struct {
Name string
RequiresLogin bool
RequiresAdminRole bool
}
templateParameters := make([]TemplateParameter, len(methods))
for i, method := range methods {
templateParameters[i] = TemplateParameter{
Name: method,
RequiresLogin: method != "PostLogin",
RequiresAdminRole: strings.Contains(method, "Admin"),
}
}
// Generate code.
tmpl, err := template.New("code").Parse(templateText)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, templateParameters)
if err != nil {
panic(err)
}
formatted, err := format.Source(buf.Bytes())
if err != nil {
panic(err)
}
err = os.WriteFile(*outputFile, formatted, 0644)
if err != nil {
panic(err)
}
}
const templateText = `// Code generated by go generate; DO NOT EDIT.
package api
import (
"context"
"albatross-2026-backend/config"
"albatross-2026-backend/db"
)
var _ StrictServerInterface = (*HandlerWrapper)(nil)
type HandlerWrapper struct {
impl Handler
}
func NewHandler(queries db.Querier, txm db.TxManager, hub GameHubInterface, auth AuthenticatorInterface, conf *config.Config) *HandlerWrapper {
return &HandlerWrapper{
impl: Handler{
q: queries,
txm: txm,
hub: hub,
auth: auth,
conf: conf,
},
}
}
{{ range . }}
func (h *HandlerWrapper) {{ .Name }}(ctx context.Context, request {{ .Name }}RequestObject) ({{ .Name }}ResponseObject, error) {
{{ if .RequiresLogin -}}
user, ok := GetUserFromContext(ctx)
if !ok {
return {{ .Name }}401JSONResponse{
Message: "Unauthorized",
}, nil
}
{{ if .RequiresAdminRole -}}
if !user.IsAdmin {
return {{ .Name }}403JSONResponse{
Message: "Forbidden",
}, nil
}
{{ end -}}
return h.impl.{{ .Name }}(ctx, request, user)
{{ else -}}
return h.impl.{{ .Name }}(ctx, request)
{{ end -}}
}
{{ end }}
`
|