Skip to content

Instantly share code, notes, and snippets.

@cpliakas
Last active August 29, 2015 14:08
Show Gist options
  • Save cpliakas/1868f7b4323cc8db656c to your computer and use it in GitHub Desktop.
Save cpliakas/1868f7b4323cc8db656c to your computer and use it in GitHub Desktop.
Martini snippet, read SSH key w/ command line args
package main
import (
"flag"
"io/ioutil"
"os/user"
"code.google.com/p/go.crypto/ssh"
"github.com/cpliakas/test/hmac"
"github.com/go-martini/martini"
)
func main() {
usr, _ := user.Current()
homeDir := usr.HomeDir + "/.ssh/id_rsa"
wordPtr := flag.String("i", homeDir, "Path to the SSH key")
flag.Parse()
m := martini.Classic()
// Set up Martini
m.Use(martini.Recovery())
m.Use(martini.Logger())
m.Get("/", func() string {
return "Hello world!"
})
m.Get("/ssh", func() string {
readKeyFile(*wordPtr)
return "Key File: " + *wordPtr
})
m.Get("/hello/:name", hmac.Auth, func(params martini.Params) string {
return "Hello " + params["name"] + "!"
})
m.Run()
}
func readKeyFile(file string) (key ssh.Signer, err error) {
buf, err := ioutil.ReadFile(file)
if err != nil {
return
}
key, err = ssh.ParsePrivateKey(buf)
if err != nil {
return
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment