123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- package main
- import (
- "database/sql"
- "encoding/json"
- "fmt"
- "html/template"
- "io/ioutil"
- "log"
- _ "github.com/go-sql-driver/mysql"
- )
- type ConnectionProperties struct {
- User string
- Pass string
- Host string
- Db string
- }
- type connection struct {
- dBOpenStr string
- connProperties ConnectionProperties
- }
- type Torrent struct {
- ID int64
- Hash string
- Name string
- OwnerID string
- Time string
- Deleted bool
- }
- type Folder struct {
- Uuid string
- ParentUuid string
- Type int
- Title string
- Snake string
- }
- type ForumMessage struct {
- Author string
- Time string
- Text string
- }
- func (c *connection) Init(filepath string) {
- if NOMYSQL {
- return
- }
- b, err := ioutil.ReadFile(filepath)
- if err != nil {
- fmt.Print(err)
- }
- propJson := string(b)
- json.Unmarshal([]byte(propJson), &c.connProperties)
- //c.connProperties.Db = "nosite"
- fmt.Printf("Connection data:\n%s\n%s\n%s\n%s\n", c.connProperties.User, c.connProperties.Pass, c.connProperties.Host, c.connProperties.Db)
- c.dBOpenStr = fmt.Sprintf("%s:%s@tcp(%s)/%s", c.connProperties.User, c.connProperties.Pass, c.connProperties.Host, c.connProperties.Db)
- fmt.Printf("Connecting with:\n%s\n", c.dBOpenStr)
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- panic(err)
- }
- if err = db.Ping(); err != nil {
- db.Close()
- logger.Println("Fatal : Error with connection to database!")
- } else {
- fmt.Println("Connection succesfull!")
- return
- }
- logger.Print("Trying to connect to DB-server...")
- c.dBOpenStr = fmt.Sprintf("%s:%s@tcp(%s)/", c.connProperties.User, c.connProperties.Pass, c.connProperties.Host)
- fmt.Printf("Connecting with:\n%s\n", c.dBOpenStr)
- db, err = sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- panic(err)
- }
- if err = db.Ping(); err != nil {
- db.Close()
- logger.Print("Fatal : Error with connection to database server!")
- return
- } else {
- }
- c.databaseInitialization()
- }
- /* создает на сервере необходимую бд и таблицы */
- func (c *connection) databaseInitialization() {
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- panic(err)
- }
- defer db.Close()
- var counter int
- var act_query string
- logger.Printf("Checking for existence of database '%s' on server...", c.connProperties.Db)
- act_query = "SELECT count(*) FROM information_schema.tables WHERE TABLE_SCHEMA = '" + c.connProperties.Db + "';"
- db.QueryRow(act_query).Scan(&counter)
- fmt.Print(counter)
- if counter != 0 {
- logger.Print("Server already has the specified database")
- } else {
- logger.Print("The server does not have the specified database")
- logger.Printf("Creating database '%s'...", c.connProperties.Db)
- act_query = "CREATE SCHEMA " + c.connProperties.Db + " DEFAULT CHARACTER SET utf8 ;"
- result, err := db.Exec(act_query)
- if err != nil {
- panic(err)
- }
- rowsCount, _ := result.RowsAffected()
- fmt.Printf("Lines changed: %d\n", rowsCount)
- if rowsCount == 1 {
- logger.Print("Succesfull!")
- }
- }
- logger.Print("Checking for existence of table 'users' on server...")
- act_query = "SELECT count(*) FROM information_schema.tables WHERE TABLE_NAME = 'users' AND TABLE_SCHEMA = '" + c.connProperties.Db + "';"
- db.QueryRow(act_query).Scan(&counter)
- fmt.Print(counter)
- if counter != 0 {
- logger.Print("Server already has the specified table!")
- } else {
- logger.Print("The server does not have the specified table")
- logger.Printf("Creating table '%s'.'users'...", c.connProperties.Db)
- act_query = "CREATE TABLE `" + c.connProperties.Db + "`.`users` ( `idusers` INT NOT NULL AUTO_INCREMENT,`username` VARCHAR(45) NOT NULL, `password` VARCHAR(45) NOT NULL, PRIMARY KEY(`idusers`), UNIQUE INDEX `idusers_UNIQUE` (`idusers` ASC), UNIQUE INDEX `username_UNIQUE` (`username` ASC)) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 ;"
- fmt.Print(act_query)
- _, err := db.Exec(act_query)
- if err != nil {
- panic(err)
- }
- logger.Print("Succesfull!")
- }
- /*
- CREATE TABLE `gosite`.`torrents` (
- `id` INT NOT NULL AUTO_INCREMENT,
- `hash` VARCHAR(45) NOT NULL,
- `name` VARCHAR(45) NOT NULL,
- `ownerid` VARCHAR(45) NOT NULL,
- `time` TIMESTAMP NOT NULL,
- `deleted` TINYINT NULL,
- UNIQUE INDEX `idtorrents_UNIQUE` (`id` ASC) VISIBLE,
- PRIMARY KEY (`hash`),
- UNIQUE INDEX `hash_UNIQUE` (`hash` ASC) VISIBLE);
- */
- }
- func (c connection) LogIn(username string, password string) bool {
- fmt.Printf("\n\nLogIn\nConnecting with:\n%s\n", c.dBOpenStr)
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- panic(err)
- }
- defer db.Close()
- var counter int
- //fmt.Printf("%s\n%s\n", username, password)
- act_query := fmt.Sprintf("SELECT count(*) FROM %s.users WHERE username='%s' AND password=SHA('%s');", c.connProperties.Db, username, password)
- db.QueryRow(act_query).Scan(&counter)
- fmt.Println("we have", counter, "rows")
- if counter == 0 {
- return false
- }
- return true
- }
- func (c connection) IsNameUsed(username string) bool {
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- panic(err)
- }
- defer db.Close()
- var counter int
- act_query := fmt.Sprintf("SELECT count(*) FROM %s.users WHERE username='%s';", c.connProperties.Db, username)
- db.QueryRow(act_query).Scan(&counter)
- if counter == 0 {
- fmt.Printf("Username unused\n")
- return false
- }
- fmt.Printf("Username used\n")
- return true
- }
- func (c connection) SigInUser(username string, password string) bool {
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- panic(err)
- }
- defer db.Close()
- act_query := fmt.Sprintf("INSERT INTO %s.users (username, password) VALUES ('%s', SHA('%s'))", c.connProperties.Db, username, password)
- result, err := db.Exec(act_query)
- if err != nil {
- panic(err)
- }
- rowsCount, _ := result.RowsAffected()
- if rowsCount == 1 {
- fmt.Printf("Lines changed: %d\n", rowsCount)
- return true
- } else {
- return false
- }
- }
- func (c connection) SubmitScore(score int) {
- fmt.Printf("Submiting score %d", score)
- }
- func (c connection) TakeTorrent(hash string) (Torrent, error) {
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- panic(err)
- }
- defer db.Close()
- act_query := fmt.Sprintf("SELECT * FROM `%s`.`torrents` WHERE `hash`='%s'", c.connProperties.Db, hash)
- fmt.Println(act_query)
- rows, err := db.Query(act_query)
- if err != nil {
- panic(err)
- }
- defer rows.Close()
- var torrent Torrent
- for rows.Next() {
- if err := rows.Scan(
- &torrent.ID,
- &torrent.Hash,
- &torrent.Name,
- &torrent.OwnerID,
- &torrent.Time,
- &torrent.Deleted,
- ); err != nil {
- log.Fatal(err)
- }
- fmt.Println(torrent)
- return torrent, nil
- }
- return torrent, fmt.Errorf("Torrent not found")
- }
- // Takes 'amount' latest torrents in order of Time desc
- // (torrents[0] is a newest)
- // If amount is 0 returns all torrents in same order as usual
- func (c connection) GetLastTorrents(amount int64) (torrents []Torrent, count int64, err error) {
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- return
- }
- defer db.Close()
- var act_query string
- act_query = fmt.Sprintf("SELECT * FROM `%s`.`torrents`", c.connProperties.Db)
- act_query += " ORDER BY `time` DESC"
- if amount != 0 {
- act_query += fmt.Sprintf(" LIMIT %d", amount)
- }
-
-
- fmt.Println(act_query)
- rows, err := db.Query(act_query)
- if err != nil {
- return
- }
- defer rows.Close()
- for rows.Next() {
- var torrent Torrent
- if err := rows.Scan(
- &torrent.ID,
- &torrent.Hash,
- &torrent.Name,
- &torrent.OwnerID,
- &torrent.Time,
- &torrent.Deleted,
- ); err != nil {
- log.Fatal(err)
- }
- torrents = append(torrents, torrent)
- count++
- }
- return
- }
- // Takes 'amount' latest news for headline in order of Time desc
- // (news[0] is a newest)
- // If amount is 0 returns all news in same order as usual
- func (c connection) GetLastNews(amount int64) (news []NewsItem, count int64, err error) {
- newsTopicUuid := "557adf6b-6988-4dfe-89d1-85e56947e067"
-
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- return
- }
- defer db.Close()
- var act_query string
- act_query = fmt.Sprintf("SELECT text, users.username, msg.time FROM %s.messages as msg INNER JOIN %s.folders as folders ON msg.parent_uuid = folders.uuid INNER JOIN %s.users as users ON msg.author = users.idusers WHERE folders.parent_uuid = '%s' ORDER BY msg.time DESC", c.connProperties.Db, c.connProperties.Db, c.connProperties.Db, newsTopicUuid)
- if amount != 0 {
- act_query += fmt.Sprintf(" LIMIT %d", amount)
- }
- fmt.Println(act_query)
- rows, err := db.Query(act_query)
- if err != nil {
- return
- }
- defer rows.Close()
- for rows.Next() {
- var templateStr string
- var item NewsItem
- if err := rows.Scan(
- &templateStr,
- &item.Author,
- &item.Time,
- ); err != nil {
- log.Fatal(err)
- }
- item.NewsText = template.HTML(templateStr)
- news = append(news, item)
- count++
- }
- return
- }
- func (c connection) TakeChildFolders(parentUuid string, amount int64) (folders []Folder, err error){
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- return
- }
- defer db.Close()
- var act_query string
- act_query = fmt.Sprintf("SELECT * FROM `%s`.`folders` as `folders`", c.connProperties.Db)
- act_query += fmt.Sprintf(" WHERE `folders`.`parent_uuid` = '%s'", parentUuid)
- act_query += " ORDER BY `id` DESC"
- if amount != 0 {
- act_query += fmt.Sprintf(" LIMIT %d", amount)
- }
- fmt.Println(act_query)
- rows, err := db.Query(act_query)
- if err != nil {
- return
- }
- defer rows.Close()
- for rows.Next() {
- var folder Folder
- var id interface{}
- if err := rows.Scan(
- &id,
- &folder.Uuid,
- &folder.ParentUuid,
- &folder.Type,
- &folder.Title,
- &folder.Snake,
- ); err != nil {
- log.Fatal(err)
- }
- folders = append(folders, folder)
- }
- return
- }
- func (c connection) TakeChildFolderBySnake(parentUuid string, snake string) (folder Folder, err error){
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- return
- }
- defer db.Close()
- var act_query string
- act_query = fmt.Sprintf("SELECT * FROM `%s`.`folders` as `folders`", c.connProperties.Db)
- act_query += fmt.Sprintf(" WHERE `folders`.`parent_uuid` = '%s'", parentUuid)
- act_query += fmt.Sprintf(" AND `folders`.`snake` = '%s'", snake)
- fmt.Println(act_query)
- rows, err := db.Query(act_query)
- if err != nil {
- return
- }
- defer rows.Close()
- for rows.Next() {
- var id interface{}
- if err := rows.Scan(
- &id,
- &folder.Uuid,
- &folder.ParentUuid,
- &folder.Type,
- &folder.Title,
- &folder.Snake,
- ); err != nil {
- log.Fatal(err)
- }
- }
- return
- }
- func (c connection) TakeTopicMessages(parentUuid string, amount int64) (messages []ForumMessage, err error) {
- db, err := sql.Open("mysql", c.dBOpenStr)
- if err != nil {
- return
- }
- defer db.Close()
- var act_query string
- act_query = fmt.Sprintf("SELECT `users`.`username`, `messages`.`time`, `messages`.`text` FROM `%s`.`messages` as `messages`", c.connProperties.Db)
- act_query += fmt.Sprintf(" INNER JOIN `%s`.`users` as `users` ON `users`.`idusers` = `messages`.`author`", c.connProperties.Db)
- act_query += fmt.Sprintf(" WHERE `messages`.`parent_uuid` = '%s'", parentUuid)
- if amount != 0 {
- act_query += fmt.Sprintf(" LIMIT %d", amount)
- }
- fmt.Println(act_query)
- rows, err := db.Query(act_query)
- if err != nil {
- return
- }
- defer rows.Close()
- for rows.Next() {
- var message ForumMessage
- if err := rows.Scan(
- &message.Author,
- &message.Time,
- &message.Text,
- ); err != nil {
- log.Fatal(err)
- }
- messages = append(messages, message)
- }
- return
- }
- /*
- func main() {
- var dBConnector connection
- dBConnector.Init("config.json")
- if (dBConnector.LogIn("Alex", "09Alex09")) {
- fmt.Printf("Succesfull logIn\n")
- } else {
- fmt.Printf("logIn error\n")
- }
- if (dBConnector.LogIn("Alax", "09Alex09")) {
- fmt.Printf("Succesfull logIn\n")
- } else {
- fmt.Printf("logIn error\n")
- }
- }
- */
|