Skip to content

Commit

Permalink
birl
Browse files Browse the repository at this point in the history
  • Loading branch information
gleicon committed Jul 27, 2016
1 parent 8df4974 commit a12164c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
53 changes: 42 additions & 11 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,59 @@ import (
"fmt"
"net/http"
"net/url"
"path/filepath"
)

type HTTPResources struct {
uu *UURL
serverMux *http.ServeMux
uu *UURL
serverMux *http.ServeMux
staticFileHandler *http.Handler
}

func NewHTTPResources(uu *UURL) *HTTPResources {
func NewHTTPResources(uu *UURL, staticFiles string) *HTTPResources {
sm := http.NewServeMux()
hr := HTTPResources{uu: uu, serverMux: sm}
sf := http.FileServer(http.Dir(staticFiles))
hr := HTTPResources{uu: uu, serverMux: sm, staticFileHandler: &sf}

hr.serverMux.HandleFunc("/", hr.RootHandler)
hr.serverMux.HandleFunc("/api/v1/url", hr.URLApiHandler)
hr.serverMux.HandleFunc("/api/v1/url/", hr.URLApiHandler)
hr.serverMux.HandleFunc("/api/v1/quicklink", hr.QuickLinkHandler)
hr.serverMux.HandleFunc("/api/v1/stats/", hr.StatsHandler)
return &hr
}

func (hr *HTTPResources) RootHandler(w http.ResponseWriter, r *http.Request) {
var redirURL string
var err error
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
uid := r.URL.Path[len("/"):]
ext := filepath.Ext(uid)
// serve static file
if len(uid) < 1 || ext != "" {
f := *hr.staticFileHandler
f.ServeHTTP(w, r)
return
}
// serve redirect
ref := r.Header.Get("REFERER")
if redirURL, err = hr.uu.GetURLByUID(uid, r.RemoteAddr, ref); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if redirURL == "" {
http.Error(w, "Not found", http.StatusNotFound)
return
}

http.Redirect(w, r, redirURL, http.StatusSeeOther)
return

}

// /api/v1/url GET/POST
func (hr *HTTPResources) URLApiHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
Expand All @@ -36,8 +72,9 @@ func (hr *HTTPResources) URLApiHandler(w http.ResponseWriter, r *http.Request) {
return
}
uid := appendix[1:]
ref := r.Header.Get("REFERER")

if redirURL, err = hr.uu.GetURLByUID(uid); err != nil {
if redirURL, err = hr.uu.GetURLByUID(uid, r.RemoteAddr, ref); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand All @@ -46,12 +83,6 @@ func (hr *HTTPResources) URLApiHandler(w http.ResponseWriter, r *http.Request) {
return
}

ref := r.Header.Get("REFERER")
if err = hr.uu.UpdateEncodedURLData(uid, r.RemoteAddr, ref); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

http.Redirect(w, r, redirURL, http.StatusSeeOther)
return
break
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func main() {
redisServer := flag.String("r", "localhost:6379", "Redis server addr:port")
redisDB := flag.String("d", "1", "Redis DB number to be used with SELECT (default 1)")
staticFiles := flag.String("f", "./static", "Static files folder")
flag.Usage = func() {
fmt.Println("Usage: uurl -r localhost:6379 -d db")
os.Exit(1)
Expand All @@ -21,7 +22,7 @@ func main() {
ss := fmt.Sprintf("%s db=%s", *redisServer, *redisDB)
db := redis.New(ss)
uu := NewUURL(db)
hr := NewHTTPResources(uu)
hr := NewHTTPResources(uu, *staticFiles)
http.Handle("/", HTTPLogger(hr.serverMux))
log.Println("Starting server")
log.Printf("db addr: %s db number: %s\n", *redisServer, *redisDB)
Expand Down
1 change: 1 addition & 0 deletions static/css/oi.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
iiii
1 change: 1 addition & 0 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oi
12 changes: 9 additions & 3 deletions uurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ func (uu *UURL) GetUUID() (int, error) {
return u, e
}

func (uu *UURL) GetURLByUID(uid string) (string, error) {
func (uu *UURL) GetURLByUID(uid, ip, ref string) (string, error) {
var err error
var url string

k := fmt.Sprintf(ENCODED_URL_MASK, uid)
url, err := uu.db.Get(k)
if err != nil {

if url, err = uu.db.Get(k); err != nil {
return "", err
}
if err = uu.UpdateEncodedURLData(uid, ip, ref); err != nil {
return "", err
}
return url, nil
Expand Down

0 comments on commit a12164c

Please sign in to comment.