Imported project in git, removed bindata dependency
This commit is contained in:
12
pages/api.go
12
pages/api.go
@ -4,9 +4,9 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.binarythought.com/cdramey/qurl/obj"
|
||||
"net"
|
||||
"net/http"
|
||||
"qurl/qurl"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -18,8 +18,8 @@ type apijson struct {
|
||||
|
||||
func (ctx *RootHandler) ServeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
j apijson
|
||||
q *qurl.QURL
|
||||
j apijson
|
||||
q *obj.QURL
|
||||
err error
|
||||
)
|
||||
|
||||
@ -39,11 +39,11 @@ func (ctx *RootHandler) ServeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
// Deal with URLs that already exist in the database
|
||||
if q != nil {
|
||||
j.Exists = true
|
||||
j.URL = fmt.Sprintf("https://qurl.org/%s", qurl.ToString(q.ID))
|
||||
j.URL = fmt.Sprintf("https://qurl.org/%s", obj.ToString(q.ID))
|
||||
goto complete
|
||||
}
|
||||
|
||||
q = &qurl.QURL{
|
||||
q = &obj.QURL{
|
||||
URL: u,
|
||||
Created: time.Now(),
|
||||
}
|
||||
@ -68,7 +68,7 @@ func (ctx *RootHandler) ServeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
goto complete
|
||||
}
|
||||
|
||||
j.URL = fmt.Sprintf("https://qurl.org/%s", qurl.ToString(q.ID))
|
||||
j.URL = fmt.Sprintf("https://qurl.org/%s", obj.ToString(q.ID))
|
||||
|
||||
complete:
|
||||
|
||||
|
@ -4,18 +4,18 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"qurl/static"
|
||||
"qurl/storage"
|
||||
"qurl/qurl"
|
||||
"git.binarythought.com/cdramey/qurl/static"
|
||||
"git.binarythought.com/cdramey/qurl/storage"
|
||||
"git.binarythought.com/cdramey/qurl/obj"
|
||||
)
|
||||
|
||||
type RootHandler struct {
|
||||
Storage storage.Storage
|
||||
|
||||
index *static.StaticContent
|
||||
css *static.StaticContent
|
||||
favi *static.StaticContent
|
||||
usage *static.StaticContent
|
||||
index *StaticContent
|
||||
css *StaticContent
|
||||
favi *StaticContent
|
||||
usage *StaticContent
|
||||
submit *template.Template
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@ func (ctx *RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ctx.ServeAPI(w, r)
|
||||
|
||||
default:
|
||||
id, err := qurl.FromString(fname[1:len(fname)])
|
||||
id, err := obj.FromString(fname[1:len(fname)])
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||
return
|
||||
@ -68,40 +68,24 @@ func (ctx *RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (ctx *RootHandler) Init() error {
|
||||
// Initialize the static content object for the index page
|
||||
index := &static.StaticContent{Content: "index.html"}
|
||||
err := index.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.index = index
|
||||
ctx.index = &StaticContent{Type: "text/html", Content: static.Index_html}
|
||||
ctx.index.Init()
|
||||
|
||||
// Initialize the static content object for the css
|
||||
css := &static.StaticContent{Content: "qurl.css"}
|
||||
err = css.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.css = css
|
||||
ctx.css = &StaticContent{Type: "text/css", Content: static.Qurl_css}
|
||||
ctx.css.Init()
|
||||
|
||||
// Initialize the static content object favicon
|
||||
favi := &static.StaticContent{Content: "favicon.ico"}
|
||||
err = favi.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.favi = favi
|
||||
ctx.favi = &StaticContent{Type: "image/x-icon", Content: static.Favicon_ico}
|
||||
ctx.favi.Init()
|
||||
|
||||
// Initialize the api usage instructions
|
||||
usage := &static.StaticContent{Content: "usage.html"}
|
||||
err = usage.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.usage = usage
|
||||
ctx.usage = &StaticContent{Type: "text/html", Content: static.Usage_html}
|
||||
ctx.usage.Init()
|
||||
|
||||
// Initialize submit page template
|
||||
ctx.submit = template.New("submit.html")
|
||||
_, err = ctx.submit.Parse(string(static.Assets["submit.html"]))
|
||||
_, err := ctx.submit.Parse(string(static.Submit_html))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
79
pages/static.go
Normal file
79
pages/static.go
Normal file
@ -0,0 +1,79 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type StaticContent struct {
|
||||
Type string
|
||||
Content []byte
|
||||
ETag string
|
||||
GZIPContent []byte
|
||||
GZIPETag string
|
||||
}
|
||||
|
||||
func (sc *StaticContent) Init() {
|
||||
// Populate ETag
|
||||
sc.ETag = fmt.Sprintf("%x", md5.Sum(sc.Content))
|
||||
|
||||
// Set a default Content-Type, if needed
|
||||
if sc.Type == "" {
|
||||
sc.Type = "application/octet-stream"
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
gz, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
|
||||
defer gz.Close()
|
||||
|
||||
if _, err := gz.Write(sc.Content); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := gz.Flush(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Using gzip encoding adds a minimum of 24 characters to the HTTP
|
||||
// header, so only accept gzip encoding if we save that much or more
|
||||
if (buf.Len()+24) < len(sc.Content) {
|
||||
sc.GZIPContent = buf.Bytes()
|
||||
sc.GZIPETag = fmt.Sprintf("%x", md5.Sum(sc.GZIPContent))
|
||||
}
|
||||
}
|
||||
|
||||
func (sc *StaticContent) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
gzok := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
|
||||
gzlen := len(sc.GZIPContent)
|
||||
|
||||
// Use the correct etag
|
||||
var localETag string
|
||||
if gzok && gzlen > 0 {
|
||||
localETag = sc.GZIPETag
|
||||
} else {
|
||||
localETag = sc.ETag
|
||||
}
|
||||
|
||||
// Check the etag, maybe we don't need to send content
|
||||
remoteETag := r.Header.Get("If-None-Match")
|
||||
if localETag == remoteETag {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
w.Header().Set("ETag", localETag)
|
||||
w.Header().Set("Content-Type", sc.Type)
|
||||
|
||||
// Finally, write our content
|
||||
if gzok && gzlen > 0 {
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", gzlen))
|
||||
w.Write(sc.GZIPContent)
|
||||
} else {
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(sc.Content)))
|
||||
w.Write(sc.Content)
|
||||
}
|
||||
}
|
@ -3,9 +3,9 @@ package pages
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"git.binarythought.com/cdramey/qurl/obj"
|
||||
"net"
|
||||
"net/http"
|
||||
"qurl/qurl"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -30,9 +30,9 @@ func (ctx *RootHandler) ServeSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if q != nil {
|
||||
pg.Message = "URL already exists."
|
||||
pg.URL = fmt.Sprintf("https://qurl.org/%s", qurl.ToString(q.ID))
|
||||
pg.URL = fmt.Sprintf("https://qurl.org/%s", obj.ToString(q.ID))
|
||||
} else {
|
||||
q = &qurl.QURL{
|
||||
q = &obj.QURL{
|
||||
URL: u,
|
||||
Created: time.Now(),
|
||||
}
|
||||
@ -55,7 +55,7 @@ func (ctx *RootHandler) ServeSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
pg.Message = "URL Added."
|
||||
pg.URL = fmt.Sprintf("https://qurl.org/%s", qurl.ToString(q.ID))
|
||||
pg.URL = fmt.Sprintf("https://qurl.org/%s", obj.ToString(q.ID))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user