This projects aims to provide a small and flexible url shortner
Flexible: Supports several routers such as gorilla/mux
, julienschmidt/httprouter
and the default http.ServeMux
server.
go get github.com/realfake/shrtie
Have a look in the examples/
folder.
package main
import (
"log"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/realfake/shrtie"
backend "github.com/realfake/shrtie/backend/redis"
redis "gopkg.in/redis.v4"
)
func main() {
b, err := backend.New(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
if err != nil {
log.Fatal(err)
}
s := shrtie.New(b)
server := httprouter.New()
// Get RedirectHandler and warp it
// into a julienschmidt/httprouter compatible handler function
server.GET("/s/:id", s.RedirectHandler().Httprouter())
server.POST("/s", s.SaveHandler().Httprouter())
server.GET("/info/:id", s.InfoHandler().Httprouter())
// Start server
log.Print(http.ListenAndServe(":9999", server))
}