Socket
Socket
Sign inDemoInstall

github.com/unprofession-al/routing

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/unprofession-al/routing


Version published
Created
Source

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.

Project status

Do not use this thing yet!

Usage

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

Package last updated on 27 Jun 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc