mirror of https://github.com/JustKato/FreePad.git
parent
e508d753ce
commit
0d71bf3650
|
@ -4,7 +4,8 @@ CREATE TABLE IF NOT EXISTS `t_posts` (
|
|||
`content` MEDIUMTEXT NOT NULL COLLATE 'latin1_swedish_ci',
|
||||
`ts` DATETIME NOT NULL DEFAULT current_timestamp(),
|
||||
`ts_updated` DATETIME NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `name` (`name`) USING BTREE
|
||||
)
|
||||
COLLATE='latin1_swedish_ci'
|
||||
ENGINE=InnoDB
|
||||
|
|
|
@ -11,11 +11,6 @@ var postList []*Post = []*Post{}
|
|||
var postMap map[string]Post = make(map[string]Post)
|
||||
|
||||
func GetPostList() []*Post {
|
||||
|
||||
db := database.GetConn()
|
||||
|
||||
println(db)
|
||||
|
||||
return postList
|
||||
}
|
||||
|
||||
|
@ -43,6 +38,23 @@ func Create(name string, content string) (*Post, error) {
|
|||
postMap[name] = myPost
|
||||
|
||||
// Add the post to the database
|
||||
db, err := database.GetConn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
sql := `REPLACE INTO freepad.t_posts (name, content) VALUES (?, ?)`
|
||||
s, err := db.Prepare(sql)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = s.Exec(myPost.Name, myPost.Content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return the post
|
||||
return &myPost, nil
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package helper
|
||||
|
||||
import "os"
|
||||
|
||||
func GetDomainBase() string {
|
||||
domainBase, domainExists := os.LookupEnv("DOMAIN_BASE")
|
||||
|
||||
if !domainExists {
|
||||
os.Setenv("DOMAIN_BASE", "http://localhost:8080")
|
||||
domainBase = "http://localhost:8080"
|
||||
}
|
||||
|
||||
return domainBase
|
||||
}
|
|
@ -9,7 +9,7 @@ import (
|
|||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func GetConn() *sql.DB {
|
||||
func GetConn() (*sql.DB, error) {
|
||||
|
||||
user := os.Getenv("MYSQL_USER")
|
||||
password := os.Getenv("MYSQL_PASSWORD")
|
||||
|
@ -18,7 +18,7 @@ func GetConn() *sql.DB {
|
|||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/%s", user, password, dbname))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set options
|
||||
|
@ -26,5 +26,5 @@ func GetConn() *sql.DB {
|
|||
db.SetMaxOpenConns(10)
|
||||
db.SetMaxIdleConns(10)
|
||||
|
||||
return db
|
||||
return db, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/JustKato/FreePad/controllers/post"
|
||||
"github.com/JustKato/FreePad/helper"
|
||||
"github.com/JustKato/FreePad/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
@ -30,7 +34,11 @@ func ApiRoutes(route *gin.RouterGroup) {
|
|||
})
|
||||
}
|
||||
|
||||
ctx.JSON(200, myPost)
|
||||
ctx.JSON(200, gin.H{
|
||||
"message": "Post succesfully created",
|
||||
"post": myPost,
|
||||
"link": fmt.Sprintf("%s/%s", helper.GetDomainBase(), url.QueryEscape(myPost.Name)),
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in New Issue