|
|
|
@ -2,65 +2,62 @@ package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"fmt"
|
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
|
|
"github.com/kreativmonkey/shrt/short"
|
|
|
|
|
//"github.com/spf13/viper"
|
|
|
|
|
shorty "github.com/kreativmonkey/shrt/short"
|
|
|
|
|
"log"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Page struct {
|
|
|
|
|
Host string
|
|
|
|
|
Title string
|
|
|
|
|
Body string
|
|
|
|
|
}
|
|
|
|
|
const version = "0.01"
|
|
|
|
|
const port = "9090"
|
|
|
|
|
const db = "./test.db"
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
short *shrt.Storage
|
|
|
|
|
short *shorty.Storage
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
var err error
|
|
|
|
|
short, err = shrt.Open()
|
|
|
|
|
short, err = shorty.Open(db)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
viper.SetDefault("ContentDir", "content")
|
|
|
|
|
viper.SetDefault("Token", map[string]string{"url": "url", "token": "token"})
|
|
|
|
|
|
|
|
|
|
func index(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
switch r.Method {
|
|
|
|
|
case "GET":
|
|
|
|
|
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 {
|
|
|
|
|
t, _ := template.ParseFiles("template/index.gohtml")
|
|
|
|
|
t.Execute(w, Page{Title: "Shrt"})
|
|
|
|
|
}
|
|
|
|
|
case "POST":
|
|
|
|
|
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/index.gohtml")
|
|
|
|
|
t.Execute(w, P)
|
|
|
|
|
viper.SetConfigName("config") // name of config file (without expression)
|
|
|
|
|
viper.AddConfigPath("/etc/appname/") // path to look for the config file in
|
|
|
|
|
viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths
|
|
|
|
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
|
|
|
|
err = viper.ReadInConfig() // Find and read the config file
|
|
|
|
|
if err != nil { // Handle errors reading the config file
|
|
|
|
|
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func all(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
t, _ := template.New("all").Parse("{{.}}")
|
|
|
|
|
t.Execute(w, short.All())
|
|
|
|
|
type shrt struct {
|
|
|
|
|
Token string `bson:"token" json:"token"`
|
|
|
|
|
URL string `bson:"url" json:"url"`
|
|
|
|
|
Date string `bson:"url_ending" json:"url_ending"`
|
|
|
|
|
Count int64 `bson:"click" json:"click"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
http.HandleFunc("/", index) // setting router rule
|
|
|
|
|
http.HandleFunc("/all", all) // setting router rule
|
|
|
|
|
http.HandleFunc("/put", index) // setting router rule
|
|
|
|
|
err := http.ListenAndServe(":9090", nil) // setting listening port
|
|
|
|
|
http.HandleFunc("/", index) // render form for Input
|
|
|
|
|
http.HandleFunc("/{token}", redirect) // request token for redirect URL
|
|
|
|
|
http.HandleFunc("/shorten", shorten) // render Page for shorten URL
|
|
|
|
|
http.HandleFunc("/all", all) // render all in a json
|
|
|
|
|
http.HandleFunc("/api/action/shorten", shortenJSON) // setting router rule
|
|
|
|
|
http.HandleFunc("/api/action/lookup", lookup) // setting router rule
|
|
|
|
|
|
|
|
|
|
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("template/css"))))
|
|
|
|
|
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("template/img"))))
|
|
|
|
|
err := http.ListenAndServe(":"+port, nil) // setting listening port
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("Shrt %s started on Port: %s", version, port)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|