-
Notifications
You must be signed in to change notification settings - Fork 0
/
humarest.go
205 lines (176 loc) · 4.92 KB
/
humarest.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package rest
import (
"fmt"
// L "github.com/fbaube/mlog"
// "github.com/gorilla/mux"
"net/http"
"strconv"
CTX "context"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humago"
)
var sRestPortNr string
type HelloReq struct {
Name string `path:"name" maxLength:"30" example:"world" doc:"Name to greet"`
/* // ADDED STUFF
ID string `path:"id"`
Detail bool `query:"detail" doc:"Show full details"`
Auth string `header:"Authorization"`
Body MyBody
RawBody []byte */
}
type HelloRsp struct {
Body struct {
Message string `json:"message" example:"Hello, world!" doc:"Hello msg"`
}
}
type StaticReq struct {
// Name string `path:"name" maxLength:"30" example:"world" doc:"Name to greet"`
}
type HtmlRsp struct {
ContentType string `header:"Content-Type"`
// LastModified time.Time `header:"Last-Modified"`
// MyHeader int `header:"My-Header"`
Body []byte // not Body struct {..}
}
type HumaHandler[I,O any] func(CTX.Context, *I) (*O, error)
func DoHello/*[HelloReq, HelloRsp]*/(ctx CTX.Context, pReq *HelloReq) (pRsp *HelloRsp, e error) {
println("GET!-DoHello")
pRsp = new(HelloRsp)
pRsp.Body.Message = fmt.Sprintf("Hello, %s!", pReq.Name)
return pRsp, nil
}
type HumaOpSpec[I,O any] struct {
HttpVerb string
UrlPatrn string
InStruct *I
OutStruct *O
// HH HumaHandler
}
/*
var OpSpecs = []HumaOpSpec {
/*HumaOpSpec[HelloReq, HelloRsp]* / {
"GET", "/hello/{name}", *HelloReq, *HelloRsp, DoHello },
}
*/
func RunRest(portNr int) error {
if portNr == 0 { // env.RestPort
return nil
}
println("RUN REST")
sRestPortNr = strconv.Itoa(portNr)
mux := http.NewServeMux()
api := humago.New(mux, huma.DefaultConfig("Derf API", "0.0.1"))
// VERB + URL-PATTERN + IN-STRUCT + OUT+STRUCT + FUNC
huma.Get(api, "/hello/{name}",
func(ctx CTX.Context, I *HelloReq) (*HelloRsp, error) {
println("GET!")
pRsp := new(HelloRsp)
pRsp.Body.Message = fmt.Sprintf("Hello, %s!", I.Name)
return pRsp, nil
})
// OR just load this "ABOUT" into the mux as a normal HTTP Handler ???
huma.Get(api, "/about",
func(ctx CTX.Context, Z *struct{}) (*HtmlRsp, error) {
println("GET-STATIC")
pRsp := new(HtmlRsp)
pRsp.ContentType = "text/html"
sTop, eTop := GetFrag("top")
sMid, eMid := GetFrag("mid")
sAbt, eAbt := GetBody("about")
sBtm, eBtm := GetFrag("btm")
pRsp.Body = []byte(
// "<!DOCTYPE html>\n<html>\n<body>\nABOUT!\n" +
// "</body></html>")
sTop + sMid + sAbt + sBtm)
fmt.Printf("%v %v %v %v \n", eTop, eMid, eAbt, eBtm)
return pRsp, nil
})
// fmt.Printf("API: %+v \n", api)
println("==> Running Huma-REST server on port:", 8888) // sRestPortNr)
// Start the server!
http.ListenAndServe("127.0.0.1:8888", mux)
// http.ListenAndServe("localhost:8888", mux)
return nil
}
/*
// ADMIN
r.HandleFunc("/stc", hdlStcRoot)
// TOPICS, MAPS, DATABASE, STATIC CONTENT
rtrTpc := r.PathPrefix("/tpc").Subrouter()
rtrMap := r.PathPrefix("/map").Subrouter()
rtrDb := r.PathPrefix("/db").Subrouter()
// HOME (incl. "About", etc.)
r.HandleFunc("/", HomeHandler)
// TOPICS
rtrTpc.HandleFunc("/{id}/meta", hdlTopicMeta)
rtrTpc.HandleFunc("/{id}/links", hdlTopicLinks)
rtrTpc.HandleFunc("/{id}", hdlTopic)
rtrTpc.HandleFunc("/", hdlTopicRoot)
// MAPS
rtrMap.HandleFunc("/{id}/meta", hdlMapMeta)
rtrMap.HandleFunc("/{id}/links", hdlMapLinks)
rtrMap.HandleFunc("/{id}", hdlMap)
rtrMap.HandleFunc("/", hdlMapRoot)
// DB (schemas? stats?)
rtrDb.HandleFunc("/fld/{name}", hdlDbField)
rtrDb.HandleFunc("/{name}", hdlDbTable)
rtrDb.HandleFunc("/", hdlDbRoot)
// go func() {
if err := http.ListenAndServe(":"+sRestPortNr, r); err != nil {
L.L.Error("REST server failed: " + err.Error())
}
return nil
}
* /
func HomeHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
var s string
s = r.RequestURI + ": " + fmt.Sprintf("home vars: %+v\n", vars)
/*
println(s)
ssnLog.Println(s)
fmt.Fprintf(w, s)
* /
L.L.Info(s)
}
func TopicRootHdlr(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
var s string
s = r.RequestURI + ": " + fmt.Sprintf("topic vars: %+v\n", vars)
/*
println(s)
ssnLog.Println(s)'
fmt.Fprintf(w, s)
* /
L.L.Info(s)
}
func MapRootHdlr(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
var s string
s = r.RequestURI + ": " + fmt.Sprintf("map vars: %+v\n", vars)
/*
println(s)
ssnLog.Println(s)
fmt.Fprintf(w, s)
* /
L.L.Info(s)
}
func StcRootHdlr(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
var s string
s = r.RequestURI + ": " + fmt.Sprintf("static vars: %+v\n", vars)
/*
println(s)
ssnLog.Println(s)
fmt.Fprintf(w, s)
* /
L.L.Info(s)
// This will serve files under http://localhost:8000/static/<filename>
// r.PathPrefix("/s/").Handler(http.StripPrefix("/s/", http.FileServer(http.Dir(dir))))
}
*/