Theodore Baschak

BOFH. Open Source Guru. Founder/Operator of Hextet Systems, AS395089 and Network Architect for Daemon Defense Systems, AS55101.

ipquail-go

Mon, 14 Dec 2015 00:04:24 -0600 » Nerd Projects, CLI, API, IPv6, Programming, System Administration

I’ve been waiting to add some features to ipquail.com for a while, but the way in which I was handling API endpoints at the moment needed to change before I could accommodate anything fancier – at the moment I was using simple Server Side Includes (“SSI” in 90’s Apache web server terminology) and some mime-type modifications to fake API endpoints. This needed to change.

I’d seen some code for how to handle this type of request using pilu/traffic, and thought “this should work” to myself. Several hours later I had pushed a new project to my github, ipquail-go – a go rewrite of ipquail.com.

The main functionality of the web application is handled thru the nginx configs mentioned in the README.md. This basically sets up nginx to handle all files in the ipquail/public directory as the webroot, and then nginx passes a few more specific URLs (/ip, /ptr, and /api/*) on to the new Go backend.

The main() function in the backend sets up the 4 API endpoints which will be passed on to it, and which handlers will be taking care of each endpoint.

  • /ip - echos straight IP
  • /ptr - echos PTR if exists, “none” otherwise
  • /api/ip - echos { “ip”: “" } w/ proper JSON headers
  • /api/ptr - echos { “ptr”: “" } w/ proper JSON headers
func main() {
  router := traffic.New()

  // add a route for each page you add to the site
  // make sure you create a route handler for it
  router.Get("/ip", ipHandler)
  router.Get("/ptr", ptrHandler)
  router.Get("/api/ip", ipapiHandler)
  router.Get("/api/ptr", ptrapiHandler)
  router.Run()
}

An example handler, used to print just the IP that the request came from. Having request IP as a header other than Host lets IPv4/IPv6 termination be handled by nginx, and the backend just simply print it out.

func ipHandler(w traffic.ResponseWriter, r *traffic.Request) {
  traffic.Logger().Print( r.Header.Get("X-Forwarded-For") ) 
  w.Header().Add("Content-type", "text/plain")
  w.WriteText( r.Header.Get("X-Forwarded-For") )
}
© Theodore Baschak - https://github.com/tbaschak - Powered by Jekyll.
Powered by Les.net.
CiscoDude.net is a personal website. Opinions expressed are not necessarily those of his employer.