
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
github.com/unprofession-al/routing
routing
... lets you decribe the structure of your (restful) API as a struct. The goal is to then generate an openapi/swagger documentation on the fly.
Do not use this thing yet!
Setup your server:
package main
import (
// ...
"github.com/gorilla/mux"
"github.com/justinas/alice"
)
type Server struct {
listener string
handler http.Handler
}
func NewServer(listener string) Server {
s := Server{listener: listener}
r := mux.NewRouter().StrictSlash(true)
routes := s.sitesRoutes()
routes.Populate(r, "sites")
s.handler = alice.New().Then(r)
return s
}
func (s Server) Run() {
fmt.Printf("Serving at http://%s\nPress CTRL-c to stop...\n", s.listener)
log.Fatal(http.ListenAndServe(s.listener, s.handler))
}
func (s Server) respond(res http.ResponseWriter, req *http.Request, code int, data interface{}) {
// ...
}
Define your routing:
package main
import (
r "github.com/unprofession-al/routing"
)
func (s Server) sitesRoutes() r.Route {
return r.Route{
H: r.Handlers{"GET": r.Handler{F: s.SitesHandler, Q: []*r.QueryParam{formatParam}}},
R: r.Routes{
"{site}": {
R: r.Routes{
"status": {H: r.Handlers{"GET": r.Handler{F: s.StatusHandler, Q: []*r.QueryParam{formatParam}}}},
"publish": {H: r.Handlers{"PUT": r.Handler{F: s.PublishHandler, Q: []*r.QueryParam{formatParam}}}},
"update": {H: r.Handlers{"PUT": r.Handler{F: s.UpdateHandler, Q: []*r.QueryParam{formatParam}}}},
"files": {
H: r.Handlers{"GET": r.Handler{F: s.TreeHandler, Q: []*r.QueryParam{formatParam}}},
R: r.Routes{
"*": {
H: r.Handlers{
"GET": {F: s.FileHandler, Q: []*r.QueryParam{mdParam}},
"POST": {F: s.FileWriteHandler, Q: []*r.QueryParam{mdParam}},
},
},
},
},
},
},
},
}
}
var formatParam = &r.QueryParam{
N: "f",
D: "json",
Desc: "format of the output, can be 'yaml' or 'json'",
}
var mdParam = &r.QueryParam{
N: "o",
D: "all",
Desc: `define if only one part of the markdown file is requested,
can be 'fm' for frontmatter, 'md' for markdown, all for everything`,
}
Prepare the handlers:
package main
import (
"net/http"
)
func (s Server) SitesHandler(res http.ResponseWriter, req *http.Request) {
s.respond(res, req, http.StatusOK, "Hello world!")
}
// ...
And fire off:
package main
func main() {
s := NewServer("127.0.0.1:8080")
s.Run()
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.