First working version without redirect
parent
d46a70bf59
commit
aac46dea60
@ -0,0 +1,77 @@
|
||||
package shrt
|
||||
|
||||
import (
|
||||
//"github.com/asaskevich/govalidator"
|
||||
"fmt"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"io"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
Token map[string]string `json: tokens`
|
||||
Url map[string]shrt `json: urls`
|
||||
}
|
||||
|
||||
type shrt struct {
|
||||
URL string
|
||||
Token string
|
||||
count int
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("Url not Found")
|
||||
ErrCreateToken = errors.New("There are some problems while creating Token")
|
||||
)
|
||||
|
||||
func Open() (*Storage, error){
|
||||
// Open db ore create if not exist!
|
||||
s := Storage{Token: make(map[string]string), Url: make(map[string]shrt)}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func StreamToByte(stream io.Reader) []byte {
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(stream)
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func (s *Storage) Add(URL string) (string, error) {
|
||||
// if the URL a valid URL?
|
||||
/*if !govalidator.IsURL(URL) {
|
||||
return "", errors.New("invalid url")
|
||||
} */
|
||||
|
||||
// Create a sha256 Hash from the URL
|
||||
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(URL)))
|
||||
|
||||
// Test if the URL alraedy exist and return the key
|
||||
if val, ok := s.Url[hash]; ok {
|
||||
return val.Token, nil
|
||||
}
|
||||
// Iterate to the length of hash to get the shortest output
|
||||
for hashShortestLen := 1; hashShortestLen <= 32; hashShortestLen++ {
|
||||
// Test if the Token not exist and return the new generated token
|
||||
if _, ok := s.Token[hash[:hashShortestLen]]; !ok {
|
||||
token := hash[:hashShortestLen]
|
||||
s.Token[token] = hash
|
||||
s.Url[hash] = shrt{URL: URL, Token: token}
|
||||
return token, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", ErrCreateToken
|
||||
}
|
||||
|
||||
func (s *Storage) Remove(URL string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get returns the URL for the given token
|
||||
func (s *Storage) Get(token string) (string, error) {
|
||||
if val, ok := s.Token[token]; ok {
|
||||
return s.Url[val].URL, nil
|
||||
}
|
||||
return "", ErrNotFound
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"fmt"
|
||||
"log"
|
||||
"html/template"
|
||||
"github.com/kreativmonkey/shrt/lib"
|
||||
)
|
||||
|
||||
type Page struct {
|
||||
Url string
|
||||
Token string
|
||||
Count int
|
||||
}
|
||||
|
||||
var (
|
||||
short *shrt.Storage
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
short, err = shrt.Open()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("method:", r.Method) //get request method
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
t, _ := template.ParseFiles("template/index.gohtml")
|
||||
t.Execute(w, nil)
|
||||
case "POST":
|
||||
r.ParseForm()
|
||||
token, err := short.Add(ToString(r.Form["url"]))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
P := Page{Url: ToString(r.Form["url"]), Token: token}
|
||||
t, _ := template.ParseFiles("template/index.gohtml")
|
||||
t.Execute(w, P)
|
||||
fmt.Println("url:", P)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func handelPost(w http.ResponseWriter, r *http.Request){
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", index) // setting router rule
|
||||
err := http.ListenAndServe(":9090", nil) // setting listening port
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServe: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ToString convert the input to a string.
|
||||
func ToString(obj interface{}) string {
|
||||
res := fmt.Sprintf("%s", obj)
|
||||
return string(res)
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package shrt
|
||||
|
||||
type Storage struct {
|
||||
Shrt []shrt
|
||||
}
|
||||
|
||||
type shrt struct{
|
||||
URL string
|
||||
key string
|
||||
count int
|
||||
}
|
||||
|
||||
type Site struct {
|
||||
Host string
|
||||
db *Bolt.db
|
||||
}
|
||||
|
||||
func New(URL string) (string, error) {
|
||||
// if the URL a valid URL?
|
||||
if !govalidator.IsURL(URL) {
|
||||
return "", errors.New("invalid url")
|
||||
}
|
||||
|
||||
// Create a md5 Hash from the URL
|
||||
hash := fmt.Sprintf("%x", md5.Sum([]byte(url)))
|
||||
|
||||
// Open up the Bold.db
|
||||
db, err := bolt.Open("my.db", 0600, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// check if the URL exist in the DB and return the Key
|
||||
|
||||
|
||||
// Iterate to the length of hash to get the shortest output
|
||||
for hashShortestLen := 1; hashShortestLen <= 32; hashShortestLen++ {
|
||||
|
||||
}
|
||||
if shortest == "" {
|
||||
return "", errors.New("url shortening failed")
|
||||
}
|
||||
|
||||
add := shrt{ URL: URL, key; key count: 0 }
|
||||
db.Shrt := appand{db.Shrt, add}
|
||||
return key, nil
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form id="shorten" action="./" method="post">
|
||||
<input type="text" name="url" placeholder="Url to shorten!" required="">
|
||||
<button class="button" type="submit" name="submit" value="short">Kürzen</button>
|
||||
</form>
|
||||
|
||||
<p>Current URL: {{.}}</p>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue