-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
39 lines (32 loc) · 886 Bytes
/
Copy pathmain.go
File metadata and controls
39 lines (32 loc) · 886 Bytes
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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the incident Academy API!")
}
var db *sql.DB
func main() {
var err error
db, err = sql.Open("postgres", "user=zainmobarik dbname=course_database sslmode=disable")
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
r := mux.NewRouter()
r.HandleFunc("/courses", courseGet).Methods("GET")
r.HandleFunc("/courses", coursePost).Methods("POST")
r.HandleFunc("/courses/{ID}", specificCourseGet).Methods("GET")
r.HandleFunc("/courses/{ID}", specificCoursePut).Methods("PUT")
r.HandleFunc("/courses/{ID}", specificCourseDelete).Methods("DELETE")
fmt.Println("Server starting on http://localhost:8080")
http.ListenAndServe(":8080", r)
}