You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.1 KiB
Go

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)
}