-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
51 lines (41 loc) · 927 Bytes
/
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
package main
import (
"database/sql"
"net/http"
"runtime"
// "strconv"
"time"
"log"
_ "github.com/lib/pq"
)
var (
db *sql.DB
)
func main() {
initObject()
//in old go compiler, it is a must to enable multithread processing
runtime.GOMAXPROCS(runtime.NumCPU())
//http.HandleFunc(pattern string, handler func(ResponseWriter, *Request))
http.HandleFunc(`/v1/cats/`, catHandler)
s := &http.Server{
Addr: ":8080",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
log.Fatal(s.ListenAndServe())
}
// init the various object and inject the database object to the modules
func initObject() {
//the postgresql connection string
connectStr := "host=localhost" +
" port=5432" +
" dbname=demo_db" +
" user=demo_user" +
" password='user_password'" +
" sslmode=disable"
var err error = nil
db, err = sql.Open("postgres", connectStr)
if err != nil {
log.Panic(err)
}
}