/
main.go
118 lines (95 loc) · 2.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
gormPostgres "gorm.io/driver/postgres"
"gorm.io/gorm"
"database/sql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
)
const (
DB_CONNECTION = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
)
type Article struct {
ID int `json:"id" gorm:"primarykey"`
Title string `json:"title"`
Description string `json:"description"`
}
func migrateDatabase() {
dbConnection, err := sql.Open("postgres", DB_CONNECTION)
if err != nil {
log.Fatal(err)
}
driver, err := postgres.WithInstance(dbConnection, &postgres.Config{})
if err != nil {
log.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://./migrations",
"postgres", driver)
if err != nil {
log.Fatal(err)
}
log.Printf("> preparing to migrate database")
m.Up() // or m.Step(2) if you want to explicitly set the number of migrations to run
log.Printf(">> database migration done")
}
func sendOk(w http.ResponseWriter, value interface{}) {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(value)
}
func main() {
migrateDatabase()
db, err := gorm.Open(gormPostgres.Open(DB_CONNECTION), &gorm.Config{})
if err != nil {
log.Fatal(err)
}
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
})
//.Queries("q", "{q:[0-9]*}") ----- required query parameter
router.Name("getArticleById").Methods(http.MethodGet).Path("/articles/{id:[0-9]+}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
queries := r.URL.Query()
fmt.Printf("Get Article By Id, id=%v, q=%v, queries=%v!\n", params["id"], params["q"], queries)
var article Article
err := db.Where("id = ?", params["id"]).First(&article).Error
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
sendOk(w, article)
})
router.Name("createArticle").Methods(http.MethodPost).Path("/articles").HeadersRegexp("Content-Type", "application/json").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var newArticle Article
err := json.NewDecoder(r.Body).Decode(&newArticle)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = db.Create(&newArticle).Error
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sendOk(w, newArticle)
})
srv := &http.Server{
Handler: router,
Addr: "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Printf("> starting server...")
log.Fatal(srv.ListenAndServe())
}