Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.