Change some logigs

master
kreativmonkey 8 years ago
parent 9da90186e7
commit 984fa97c4c

@ -0,0 +1,78 @@
package main
import (
"net/http"
"encoding/json"
"fmt"
"text/template"
)
type Page struct {
Host string
Title string
Body string
}
// Load the main Page with an Imput field for the URL to short.
func index(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("template/index.gohtml")
t.Execute(w, Page{Title: "Shrt"})
}
// Redirect to the URL behind the requested token.
func redirect(w http.ResponseWriter, r *http.Request){
if redirect, ok := short.Get(r.URL.Path[1:]); ok{
http.Redirect(w, r, string(redirect), 301)
fmt.Printf("Token: %s Redirect to: %s \n", string(r.URL.Path[1:]), redirect)
} else {
http.Redirect(w, r, "/", 301)
}
}
//
func shorten(w http.ResponseWriter, r *http.Request){
var test string
token, err := short.Add(r.FormValue("url"), &test)
if err != nil {
panic(err)
}
P := Page{Title: "Url:", Body: r.Host + "/" + token}
t, _ := template.ParseFiles("template/short.gohtml")
t.Execute(w, P)
}
// GET: http://example.com/api/action/shorten?key=API_KEY_HERE&url=https://google.com&custom_ending=CUSTOM_ENDING
// Response: {"action": "shorten","result": "https://example.com/5kq"}
func shortenJSON(w http.ResponseWriter, r *http.Request){
decoder := json.NewDecoder(r.Body)
var response shrt
err := decoder.Decode(&response)
if err != nil {
panic(err)
}
defer r.Body.Close()
fmt.Println(response)
/*response := map[string]string{
"action": "shorten",
"result": "",
}*/
}
// GET: http://example.com/api/action/lookup?key=API_KEY_HERE&url_ending=5kq
// Response: {"action":"lookup","result": {"long_url": "https:\/\/google.com","created_at": {"date":"2016-02-12 15:20:34.000000","timezone_type":3,"timezone":"UTC"},"clicks":"0"}}
func lookup(w http.ResponseWriter, r *http.Request){
/*response := map[string]interface{}{
"action": "lookup",
"result": map[string]string{
"long_url": "",
"created_at": "",
"clicks": "",
}
}*/
}
func all(w http.ResponseWriter, r *http.Request){
t, _ := template.New("all").Parse("{{.}}")
t.Execute(w, short.All())
}

@ -0,0 +1,51 @@
package main
import (
"net/http"
"github.com/gorilla/mux"
)
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
type Routes []Route
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}
return router
}
var routes = Routes{
Route{
"Index",
"GET",
"/",
index,
},
Route{
"TodoIndex",
"POST",
"/shorten",
shorten,
},
Route{
"TodoShow",
"GET",
"/{token}",
lookup,
},
}

@ -0,0 +1,82 @@
body {
margin: 0px;
}
main {
height: 85vh;
display: flex;
flex-direction: column;
justify-content: center;
}
#container{
max-width: 1170px;
width: 100%;
margin: 0 auto;
text-align: center;
}
footer {
position: absolute;
bottom: 2em;
font-size: 1.2em;
text-align: center;
width: 100%;
}
h1 {
font-size: 5em;
line-height: 1;
margin: 21px 0 10px 0;
}
form {
display: block;
margin-top: 0em;
}
form input {
padding: 7px;
font-size: 1em;
display: block;
width: 100%;
magin: 1em -7px;
}
form button {
display: inline-block;
margin-bottom: 0;
font-size: 1em;
font-weight: normal;
line-height: 1.428571429;
text-align: center;
white-space: nowrap;
vertical-align: middle;
background-image: none;
border: 1px solid transparent;
border-radius: 0;
color: #fff;
padding: 14px 28px;
background-color: #39b3d7;
border-color: #269abc;
}
input {
padding: 0.7em;
font-size: 1em;
display: block;
width: 100%;
margin-bottom: 1em;
}
button {
display: inline-block;
margin-bottom: 0;
font-size: 1em;
font-weight: normal;
line-height: 1.428571429;
text-align: center;
white-space: nowrap;
vertical-align: middle;
background-image: none;
border: 1px solid transparent;
border-radius: 0;
color: #fff;
padding: 14px 28px;
background-color: #39b3d7;
border-color: #269abc;
}
footer img {
height: 2em;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="42.607086mm"
height="41.554932mm"
viewBox="0 0 42.607086 41.554932"
version="1.1"
id="svg4878"
inkscape:version="0.92.1 r"
sodipodi:docname="github.svg">
<defs
id="defs4872" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="3.959798"
inkscape:cx="64.663541"
inkscape:cy="76.392043"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
inkscape:window-width="1920"
inkscape:window-height="1025"
inkscape:window-x="0"
inkscape:window-y="27"
inkscape:window-maximized="1" />
<metadata
id="metadata4875">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Ebene 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-67.142885,-128.81182)">
<path
d="m 88.446429,128.81182 c -11.76408,0 -21.303544,9.53735 -21.303544,21.30355 0,9.41211 6.104114,17.39759 14.568663,20.21451 1.064684,0.19721 1.455561,-0.46213 1.455561,-1.02481 0,-0.50765 -0.01976,-2.18617 -0.02893,-3.96629 -5.926664,1.2887 -7.177261,-2.51354 -7.177261,-2.51354 -0.96908,-2.46203 -2.365375,-3.11714 -2.365375,-3.11714 -1.932869,-1.32221 0.145697,-1.29505 0.145697,-1.29505 2.139245,0.15028 3.265664,2.19569 3.265664,2.19569 1.900061,3.25649 4.983692,2.31493 6.199364,1.77059 0.191558,-1.37654 0.743303,-2.31669 1.35255,-2.84868 -4.731808,-0.53834 -9.705975,-2.36537 -9.705975,-10.5283 0,-2.32551 0.832203,-4.22628 2.194983,-5.71817 -0.221191,-0.53658 -0.950383,-2.70334 0.206728,-5.63774 0,0 1.788936,-0.57221 5.859639,2.18404 1.69933,-0.47201 3.52178,-0.70873 5.332236,-0.71719 1.810455,0.008 3.634316,0.24518 5.336823,0.71719 4.066116,-2.75625 5.852581,-2.18404 5.852581,-2.18404 1.159577,2.9344 0.430387,5.10116 0.2092,5.63774 1.365957,1.49189 2.192157,3.39266 2.192157,5.71817 0,8.18268 -4.983688,9.98397 -9.727492,10.51137 0.764117,0.6611 1.444979,1.95756 1.444979,3.94511 0,2.85045 -0.02434,5.14456 -0.02434,5.84624 0,0.56726 0.383117,1.23119 1.462969,1.02235 8.460314,-2.82011 14.556664,-10.80276 14.556664,-20.21205 0,-11.7662 -9.53805,-21.30355 -21.303541,-21.30355"
style="fill:#1b1817;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:0.35277775"
id="path28"
inkscape:connector-curvature="0" />
<path
d="m 75.211762,159.39878 c -0.04692,0.10619 -0.213431,0.13794 -0.365125,0.0653 -0.15487,-0.0698 -0.2413,-0.21414 -0.191206,-0.32032 0.04586,-0.10866 0.212372,-0.13899 0.366889,-0.0663 0.154517,0.0695 0.242711,0.2152 0.189442,0.32138"
style="fill:#1b1817;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:0.35277775"
id="path32"
inkscape:connector-curvature="0" />
<path
d="m 76.074761,160.36134 c -0.1016,0.0942 -0.300567,0.0504 -0.434975,-0.0984 -0.1397,-0.14852 -0.165453,-0.34749 -0.06244,-0.44274 0.104775,-0.0942 0.297392,-0.0501 0.436739,0.0984 0.1397,0.15029 0.166864,0.34714 0.06068,0.44274"
style="fill:#1b1817;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:0.35277775"
id="path36"
inkscape:connector-curvature="0" />
<path
d="m 76.914689,161.58812 c -0.130528,0.091 -0.344311,0.006 -0.47625,-0.18344 -0.130528,-0.1898 -0.130528,-0.41698 0.0032,-0.508 0.131939,-0.091 0.342547,-0.009 0.47625,0.17886 0.130175,0.19261 0.130175,0.4198 -0.0032,0.51258"
style="fill:#1b1817;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:0.35277775"
id="path40"
inkscape:connector-curvature="0" />
<path
d="m 78.065346,162.7736 c -0.11677,0.12876 -0.365478,0.0942 -0.547511,-0.0818 -0.186267,-0.17145 -0.238125,-0.41522 -0.121356,-0.54399 0.118533,-0.12911 0.368653,-0.0928 0.552097,0.0815 0.184856,0.17145 0.240948,0.41699 0.11677,0.54434"
style="fill:#1b1817;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:0.35277775"
id="path44"
inkscape:connector-curvature="0" />
<path
d="m 79.652846,163.4619 c -0.05186,0.16687 -0.291042,0.24236 -0.532342,0.17145 -0.240947,-0.073 -0.398639,-0.26811 -0.349955,-0.43674 0.05009,-0.16792 0.290688,-0.24694 0.533752,-0.17109 0.240595,0.0727 0.398639,0.2667 0.348545,0.43638"
style="fill:#1b1817;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:0.35277775"
id="path48"
inkscape:connector-curvature="0" />
<path
d="m 81.396413,163.5894 c 0.006,0.17568 -0.198614,0.32138 -0.451909,0.32455 -0.254705,0.006 -0.46108,-0.13652 -0.463902,-0.30938 0,-0.17745 0.200377,-0.32138 0.45473,-0.32597 0.253295,-0.005 0.461081,0.13652 0.461081,0.3108"
style="fill:#1b1817;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:0.35277775"
id="path52"
inkscape:connector-curvature="0" />
<path
d="m 83.018698,163.31342 c 0.03034,0.1711 -0.145698,0.34713 -0.397228,0.39405 -0.247297,0.0455 -0.47625,-0.0607 -0.507647,-0.23036 -0.03069,-0.17569 0.148166,-0.35172 0.395464,-0.39723 0.251883,-0.0437 0.477308,0.0593 0.509411,0.23354"
style="fill:#1b1817;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:0.35277775"
id="path56"
inkscape:connector-curvature="0" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 5.9 KiB

@ -3,61 +3,26 @@
<head>
<meta charset="UTF-8">
<title>Shrt</title>
<style>
body {
height: 80vh;
display: flex;
flex-direction: column;
justify-content: center;
}
#main {
width: 1170px;
margin: 0 auto;
text-align: center;
}
h1 {
font-size: 67px;
line-height: 1;
margin: 21px 0 10px 0;
}
form {
display: block;
margin-top: 0em;
}
form input {
padding: 7px;
font-size: 12px;
display: block;
width: 100%;
margin-bottom: 10px;
}
form button {
display: inline-block;
margin-bottom: 0;
font-size: 15px;
font-weight: normal;
line-height: 1.428571429;
text-align: center;
white-space: nowrap;
vertical-align: middle;
background-image: none;
border: 1px solid transparent;
border-radius: 0;
color: #fff;
padding: 14px 28px;
background-color: #39b3d7;
border-color: #269abc;
}
</style>
<link rel="stylesheet" type="text/css" href="/css/app.css" \>
</head>
<body>
<div id="main">
<main id="main">
<div id="container">
<h1>{{.Title}}</h1>
<form id="shorten" action="./" method="post">
<input type="url" name="url" pattern="^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?" placeholder="Url to shorten!" required="" autofocus value="{{if .Body}} {{.Body}} {{end}}">
<form id="shorten" action="./shorten" method="post">
<input type="url" name="url" placeholder="http(s)://" required="" autofocus value="{{if .Body}} {{.Body}} {{end}}">
<button class="button" type="submit" name="submit" value="short">Kürzen</button>
</form>
</div>
</main>
<footer id="footer">
<a href="https://git.calyrium.org/kreativmonkey/shrt">
<picture>
<source srcset="/img/github.svg" type="image/svg+xml">
<img src="/img/github.png" alt="Github" />
</picture>
</a>
</footer>
</body>
</html>

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Shrt</title>
<link rel="stylesheet" type="text/css" href="/css/app.css" \>
</head>
<body>
<main id="main">
<div id="container">
<h1>{{.Title}}</h1>
<input type="url" name="url" placeholder="http(s)://" required="" autofocus value="{{if .Body}} {{.Body}} {{end}}">
<a href="/"><button class="button" type="submit" name="submit" value="short">Nächste</button></a>
</div>
</main>
<footer>
<a href="https://git.calyrium.org/kreativmonkey/shrt">
<picture>
<source srcset="img/github.svg" type="image/svg+xml">
<img src="img/github.png" alt="running rabbits" />
</picture>
</a>
</footer>
</body>
</html>
Loading…
Cancel
Save