This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE IF NOT EXISTS `go-mysql-crud` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */; | |
USE `go-mysql-crud`; | |
/* Create table for `posts` */ | |
DROP TABLE IF EXISTS `posts`; | |
CREATE TABLE `posts` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL, | |
`content` longtext COLLATE utf8_unicode_ci NOT NULL, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pid aux | |
kill -9 <pid> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func init() { | |
router = chi.NewRouter() | |
router.Use(middleware.Recoverer) | |
dbSource := fmt.Sprintf("root:%s@tcp(%s:%s)/%s?charset=utf8", dbPass, dbHost, dbPort, dbName) | |
var err error | |
db, err = sql.Open("mysql", dbSource) | |
catch(err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CreatePost create a new post | |
func CreatePost(w http.ResponseWriter, r *http.Request) { | |
var post Post | |
json.NewDecoder(r.Body).Decode(&post) | |
query, err := db.Prepare("Insert posts SET title=?, content=?") | |
catch(err) | |
_, er := query.Exec(post.Title, post.Content) | |
catch(er) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UpdatePost update a spesific post | |
func UpdatePost(w http.ResponseWriter, r *http.Request) { | |
var post Post | |
id := chi.URLParam(r, "id") | |
json.NewDecoder(r.Body).Decode(&post) | |
query, err := db.Prepare("Update posts set title=?, content=? where id=?") | |
catch(err) | |
_, er := query.Exec(post.Title, post.Content, id) | |
catch(er) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"time" | |
) | |
func catch(err error) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
routers() | |
http.ListenAndServe(":8005", Logger()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"os" | |
"github.com/go-chi/chi" | |
"github.com/go-chi/chi/middleware" | |
"github.com/s1s1ty/go-mysql-crud/driver" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package driver | |
import ( | |
"database/sql" | |
"fmt" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
// DB ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package handler | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"strconv" | |
"github.com/go-chi/chi" | |
"github.com/s1s1ty/go-mysql-crud/driver" |
OlderNewer