Kelly Norton 10 rokov pred
rodič
commit
cd5822dadb
4 zmenil súbory, kde vykonal 196 pridanie a 184 odobranie
  1. 3 3
      Makefile
  2. 2 180
      main.go
  3. 1 1
      web/bindata.go
  4. 190 0
      web/web.go

+ 3 - 3
Makefile

@@ -1,7 +1,7 @@
-ALL: bindata.go
+ALL: web/bindata.go
 
 .build/bin/go-bindata:
 	GOPATH=$(shell pwd)/.build go get github.com/jteeuwen/go-bindata/...
 
-bindata.go: .build/bin/go-bindata $(wildcard pub/**/*)
-	$< -o $@ -pkg main -prefix pub -nomemcopy pub/...
+web/bindata.go: .build/bin/go-bindata $(wildcard pub/**/*)
+	$< -o $@ -pkg web -prefix pub -nomemcopy pub/...

+ 2 - 180
main.go

@@ -1,183 +1,13 @@
 package main
 
 import (
-	"bytes"
-	"encoding/base64"
-	"encoding/binary"
-	"encoding/json"
 	"flag"
-	"fmt"
 	"log"
-	"math/rand"
-	"net/http"
-	"strings"
-	"time"
-
-	"github.com/syndtr/goleveldb/leveldb"
 
 	"github.com/kellegous/go/context"
+	"github.com/kellegous/go/web"
 )
 
-func MakeName() string {
-	var buf bytes.Buffer
-	binary.Write(&buf, binary.LittleEndian, rand.Int63())
-	return base64.URLEncoding.EncodeToString(buf.Bytes())
-}
-
-func ParseName(base, path string) string {
-	t := path[len(base):]
-	ix := strings.Index(t, "/")
-	if ix == -1 {
-		return t
-	} else {
-		return t[:ix]
-	}
-}
-
-func WriteJson(w http.ResponseWriter, data interface{}, status int) {
-	w.Header().Set("Content-Type", "application/json;charset=utf-8")
-	if err := json.NewEncoder(w).Encode(data); err != nil {
-		log.Panic(err)
-	}
-}
-
-func WriteJsonError(w http.ResponseWriter, error string, status int) {
-	WriteJson(w, map[string]interface{}{
-		"error": error,
-	}, status)
-}
-
-func WriteJsonRoute(w http.ResponseWriter, name string, rt *context.Route) {
-	res := struct {
-		Name string    `json:"name"`
-		URL  string    `json:"url"`
-		Time time.Time `json:"time"`
-	}{
-		name,
-		rt.URL,
-		rt.Time,
-	}
-
-	WriteJson(w, &res, http.StatusOK)
-}
-
-func ServeAsset(w http.ResponseWriter, r *http.Request, name string) {
-	n, err := AssetInfo(name)
-	if err != nil {
-		http.NotFound(w, r)
-		return
-	}
-
-	a, err := Asset(name)
-	if err != nil {
-		http.NotFound(w, r)
-		return
-	}
-
-	http.ServeContent(w, r, n.Name(), n.ModTime(), bytes.NewReader(a))
-}
-
-type DefaultHandler struct {
-	ctx *context.Context
-}
-
-func (h *DefaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	p := ParseName("/", r.URL.Path)
-
-	if p == "" {
-		http.Redirect(w, r,
-			fmt.Sprintf("/edit/%s", MakeName()),
-			http.StatusTemporaryRedirect)
-		return
-	}
-
-	rt, err := h.ctx.Get(p)
-	if err == leveldb.ErrNotFound {
-		http.Redirect(w, r,
-			fmt.Sprintf("/edit/%s", p),
-			http.StatusTemporaryRedirect)
-		return
-	} else if err != nil {
-		log.Panic(err)
-	}
-
-	http.Redirect(w, r,
-		rt.URL,
-		http.StatusTemporaryRedirect)
-}
-
-type EditHandler struct {
-	ctx *context.Context
-}
-
-func (h *EditHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	p := ParseName("/edit/", r.URL.Path)
-
-	if p == "" {
-		http.Redirect(w, r,
-			fmt.Sprintf("/edit/%s", MakeName()),
-			http.StatusTemporaryRedirect)
-		return
-	}
-
-	ServeAsset(w, r, "index.html")
-}
-
-type ApiHandler struct {
-	ctx *context.Context
-}
-
-func (h *ApiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	p := ParseName("/api/url/", r.URL.Path)
-	if p == "" {
-		WriteJsonError(w,
-			http.StatusText(http.StatusNotFound),
-			http.StatusNotFound)
-		return
-	}
-
-	if r.Method == "POST" {
-		var req struct {
-			URL string `json:"url"`
-		}
-
-		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
-			WriteJsonError(w, "invalid json", http.StatusBadRequest)
-			return
-		}
-
-		if req.URL == "" {
-			WriteJsonError(w, "url required", http.StatusBadRequest)
-			return
-		}
-
-		rt := context.Route{
-			URL:  req.URL,
-			Time: time.Now(),
-		}
-
-		if err := h.ctx.Put(p, &rt); err != nil {
-			log.Panic(err)
-		}
-
-		WriteJsonRoute(w, p, &rt)
-	} else if r.Method == "GET" {
-		rt, err := h.ctx.Get(p)
-		if err == leveldb.ErrNotFound {
-			WriteJsonError(w, "no such route", http.StatusNotFound)
-			return
-		} else if err != nil {
-			log.Panic(err)
-		}
-
-		WriteJsonRoute(w, p, rt)
-	} else {
-		WriteJsonError(w,
-			http.StatusText(http.StatusMethodNotAllowed),
-			http.StatusMethodNotAllowed)
-	}
-}
-
 func main() {
 	flagData := flag.String("data", "data", "data")
 	flagAddr := flag.String("addr", ":8067", "addr")
@@ -188,13 +18,5 @@ func main() {
 		log.Panic(err)
 	}
 
-	mux := http.NewServeMux()
-	mux.Handle("/", &DefaultHandler{ctx})
-	mux.Handle("/edit/", &EditHandler{ctx})
-	mux.Handle("/api/url/", &ApiHandler{ctx})
-	mux.HandleFunc("/s/", func(w http.ResponseWriter, r *http.Request) {
-		ServeAsset(w, r, r.URL.Path[len("/s/"):])
-	})
-
-	log.Panic(http.ListenAndServe(*flagAddr, mux))
+	log.Panic(web.ListenAndServe(*flagAddr, ctx))
 }

+ 1 - 1
bindata.go → web/bindata.go

@@ -5,7 +5,7 @@
 // pub/index.js
 // DO NOT EDIT!
 
-package main
+package web
 
 import (
 	"bytes"

+ 190 - 0
web/web.go

@@ -0,0 +1,190 @@
+package web
+
+import (
+	"bytes"
+	"encoding/base64"
+	"encoding/binary"
+	"encoding/json"
+	"fmt"
+	"log"
+	"math/rand"
+	"net/http"
+	"strings"
+	"time"
+
+	"github.com/kellegous/go/context"
+	"github.com/syndtr/goleveldb/leveldb"
+)
+
+func makeName() string {
+	var buf bytes.Buffer
+	binary.Write(&buf, binary.LittleEndian, rand.Int63())
+	return base64.URLEncoding.EncodeToString(buf.Bytes())
+}
+
+func parseName(base, path string) string {
+	t := path[len(base):]
+	ix := strings.Index(t, "/")
+	if ix == -1 {
+		return t
+	}
+	return t[:ix]
+}
+
+func writeJSON(w http.ResponseWriter, data interface{}, status int) {
+	w.Header().Set("Content-Type", "application/json;charset=utf-8")
+	if err := json.NewEncoder(w).Encode(data); err != nil {
+		log.Panic(err)
+	}
+}
+
+func writeJSONError(w http.ResponseWriter, error string, status int) {
+	writeJSON(w, map[string]interface{}{
+		"error": error,
+	}, status)
+}
+
+func writeJSONRoute(w http.ResponseWriter, name string, rt *context.Route) {
+	res := struct {
+		Name string    `json:"name"`
+		URL  string    `json:"url"`
+		Time time.Time `json:"time"`
+	}{
+		name,
+		rt.URL,
+		rt.Time,
+	}
+
+	writeJSON(w, &res, http.StatusOK)
+}
+
+func serveAsset(w http.ResponseWriter, r *http.Request, name string) {
+	n, err := AssetInfo(name)
+	if err != nil {
+		http.NotFound(w, r)
+		return
+	}
+
+	a, err := Asset(name)
+	if err != nil {
+		http.NotFound(w, r)
+		return
+	}
+
+	http.ServeContent(w, r, n.Name(), n.ModTime(), bytes.NewReader(a))
+}
+
+type apiHandler struct {
+	ctx *context.Context
+}
+
+func (h *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	p := parseName("/api/url/", r.URL.Path)
+	if p == "" {
+		writeJSONError(w,
+			http.StatusText(http.StatusNotFound),
+			http.StatusNotFound)
+		return
+	}
+
+	if r.Method == "POST" {
+		var req struct {
+			URL string `json:"url"`
+		}
+
+		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+			writeJSONError(w, "invalid json", http.StatusBadRequest)
+			return
+		}
+
+		if req.URL == "" {
+			writeJSONError(w, "url required", http.StatusBadRequest)
+			return
+		}
+
+		rt := context.Route{
+			URL:  req.URL,
+			Time: time.Now(),
+		}
+
+		if err := h.ctx.Put(p, &rt); err != nil {
+			log.Panic(err)
+		}
+
+		writeJSONRoute(w, p, &rt)
+	} else if r.Method == "GET" {
+		rt, err := h.ctx.Get(p)
+		if err == leveldb.ErrNotFound {
+			writeJSONError(w, "no such route", http.StatusNotFound)
+			return
+		} else if err != nil {
+			log.Panic(err)
+		}
+
+		writeJSONRoute(w, p, rt)
+	} else {
+		writeJSONError(w,
+			http.StatusText(http.StatusMethodNotAllowed),
+			http.StatusMethodNotAllowed)
+	}
+}
+
+type defaultHandler struct {
+	ctx *context.Context
+}
+
+func (h *defaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	p := parseName("/", r.URL.Path)
+
+	if p == "" {
+		http.Redirect(w, r,
+			fmt.Sprintf("/edit/%s", makeName()),
+			http.StatusTemporaryRedirect)
+		return
+	}
+
+	rt, err := h.ctx.Get(p)
+	if err == leveldb.ErrNotFound {
+		http.Redirect(w, r,
+			fmt.Sprintf("/edit/%s", p),
+			http.StatusTemporaryRedirect)
+		return
+	} else if err != nil {
+		log.Panic(err)
+	}
+
+	http.Redirect(w, r,
+		rt.URL,
+		http.StatusTemporaryRedirect)
+}
+
+type editHandler struct {
+	ctx *context.Context
+}
+
+func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	p := parseName("/edit/", r.URL.Path)
+
+	if p == "" {
+		http.Redirect(w, r,
+			fmt.Sprintf("/edit/%s", makeName()),
+			http.StatusTemporaryRedirect)
+		return
+	}
+
+	serveAsset(w, r, "index.html")
+}
+
+// ListenAndServe ...
+func ListenAndServe(addr string, ctx *context.Context) error {
+	mux := http.NewServeMux()
+
+	mux.Handle("/", &defaultHandler{ctx})
+	mux.Handle("/edit/", &editHandler{ctx})
+	mux.Handle("/api/url/", &apiHandler{ctx})
+	mux.HandleFunc("/s/", func(w http.ResponseWriter, r *http.Request) {
+		serveAsset(w, r, r.URL.Path[len("/s/"):])
+	})
+
+	return http.ListenAndServe(addr, mux)
+}