Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.30 KB | None | 0 0
  1. package DataBaseType
  2.  
  3. type UserPassport struct {
  4.     UserID      int    `sql:"type:bigint;AUTO_INCREMENT"        gorm:"primary_key"`
  5.     UserLogin   string `sql:"type:varchar(50)  ;not null"       gorm:"unique_index"`
  6.     UserPass    string `sql:"type:varchar(200) ;not null"`
  7.     UserSaltInt int    `sql:"type:bigint       ;not null"`
  8. }
  9.  
  10. type UseApi struct {
  11.     Id       int    `sql:"AUTO_INCREMENT"                gorm:"primary_key"`
  12.     ApiKey   string `sql:"type:varchar(13)   ;not null"  gorm:"unique_index"`
  13.     Salt     string `sql:"type:text          ;not null"`
  14.     Domen    string `sql:"type:text          ;not null"`
  15.     Location string `sql:"type:text          ;not null"`
  16. }
  17.  
  18. type Token struct {
  19.     Id     int    `sql:"AUTO_INCREMENT"                gorm:"primary_key"`
  20.     UserId int    `sql:"type:bigint       ;not null"   gorm:"index"`
  21.     token  string `sql:"type:text         ;not null"`
  22. }
  23.  
  24. package main
  25.  
  26. import (
  27.     _ "github.com/go-sql-driver/mysql"
  28.     "github.com/gorilla/mux"
  29.     "github.com/jinzhu/gorm"
  30.     "login/DataBaseType"
  31.     //_ "github.com/lib/pq"
  32.     //_ "github.com/mattn/go-sqlite3"
  33.     "crypto/md5"
  34.     "html/template"
  35.     "io"
  36.     "log"
  37.     "net/http"
  38. )
  39.  
  40. var db gorm.DB
  41.  
  42. func TemplateHtml(templateName string, p map[string]string, w http.ResponseWriter) {
  43.     t, err := template.ParseFiles("html/" + templateName)
  44.     if err != nil {
  45.         return
  46.     }
  47.     t.Execute(w, p)
  48.     w.Header().Set("Content-Type", "text/html")
  49. }
  50.  
  51. func templateAuth(w http.ResponseWriter, r *http.Request) {
  52.     vars := mux.Vars(r)
  53.     row := db.Table("use_api").Where("api_key  = ?", vars["keyApi"]).Select("salt").Row()
  54.  
  55.     h := md5.New()
  56.     io.WriteString(h, vars["keyApi"])
  57.     io.WriteString(h, row.Scan(&salt))
  58.  
  59.     if h.Sum(nil) == vars["hash"] {
  60.         p := map[string]string{
  61.             "title":  "Авторизация",
  62.             "domen":  "localhost",
  63.             "keyApi": vars["keyApi"],
  64.             "hash":   vars["hash"],
  65.         }
  66.  
  67.         TemplateHtml("login.html", p, w)
  68.     } else {
  69.         p := map[string]string{
  70.             "err":   "Неверный хеш",
  71.             "domen": "localhost",
  72.         }
  73.  
  74.         TemplateHtml("err.html", p, w)
  75.     }
  76. }
  77.  
  78. func auth(w http.ResponseWriter, r *http.Request) {
  79.  
  80. }
  81.  
  82. func templateReg(w http.ResponseWriter, r *http.Request) {
  83.     p := map[string]string{
  84.         "title": "Авторизация",
  85.         "domen": "localhost",
  86.     }
  87.  
  88.     TemplateHtml("reg.html", p, w)
  89. }
  90.  
  91. func reg(w http.ResponseWriter, r *http.Request) {
  92.  
  93. }
  94.  
  95. func migration(w http.ResponseWriter, r *http.Request) {
  96.     db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DataBaseType.UserPassport{})
  97.     db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DataBaseType.UseApi{})
  98.     db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&DataBaseType.Token{})
  99. }
  100.  
  101. func main() {
  102.     dbConnect, err := gorm.Open("mysql", "root:parsh888@/login?charset=utf8&parseTime=True&loc=Local")
  103.     dbConnect.DB()
  104.     dbConnect.DB().Ping()
  105.     dbConnect.DB().SetMaxIdleConns(10)
  106.     dbConnect.DB().SetMaxOpenConns(100)
  107.     dbConnect.SingularTable(true)
  108.     if err != nil {
  109.         log.Fatal(err)
  110.     }
  111.     db = dbConnect
  112.  
  113.     r := mux.NewRouter()
  114.     r.HandleFunc("/migration", migration).Methods("GET")
  115.  
  116.     r.HandleFunc("/{keyApi}/{hash}", templateAuth).Methods("GET")
  117.     r.HandleFunc("/{keyApi}/{hash}", auth).Methods("POST")
  118.  
  119.     r.HandleFunc("/reg", templateReg).Methods("GET")
  120.     r.HandleFunc("/reg", reg).Methods("POST")
  121.  
  122.     log.Fatal(http.ListenAndServe(":8080", r))
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement