Kepler core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

58 lines
1.5 KiB

package handlers
import (
"net/http"
"time"
log "github.com/Sirupsen/logrus"
xql "github.com/archsh/go.xql"
"cygnux.net/kepler/restlet"
"cygnux.net/kepler/service"
"cygnux.net/kepler/demo2/models"
)
func exampleHandler(ctx restlet.RequestContext, params restlet.Parameters, queries restlet.Parameters, data []byte) (*restlet.RestletResult, error) {
log.Debugln("ExampleHandler:>", params, queries)
for _, c := range ctx.Request().Cookies() {
log.Debugln("ExampleHandler:> Get Cookie:", c.Name, c.Value)
}
v, b := ctx.Get("demo_session")
log.Debugln("ExampleHandler:> CTX:", v, b)
var cookies []*http.Cookie
var ck = http.Cookie{
Name: "demo_session",
Value: time.Now().String(),
Path: "/",
SameSite: http.SameSiteStrictMode,
MaxAge: int(3600 * time.Second),
}
cookies = append(cookies, &ck)
return &restlet.RestletResult{
Code: restlet.SuccessOk,
Data: "OK",
Cookies: cookies,
}, nil
}
type personKind struct {
}
func (personKind) Entity() xql.TableIdentified {
return &models.Person{}
}
func sessionKeeper(next restlet.RequestHandler) restlet.RequestHandler {
return restlet.RequestHandleFunc(func(ctx restlet.RequestContext, w http.ResponseWriter, r *http.Request) error {
_ = ctx.Set("demo_session", "Hello")
return next.Handle(ctx, w, r)
})
}
func init() {
//svr.RegisterRequest()
service.RegisterRequest("example", restlet.RestletFunc(exampleHandler))
service.RegisterRequest("persons", restlet.RestletFunc(restlet.NewCURDHandler("", personKind{}).Handle))
service.SetSessionKeeper(sessionKeeper)
}