-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (57 loc) · 1.84 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
// Entry point
package main
import (
"fmt"
"log"
"net/http"
_ "jikko-golang/docs"
"jikko-golang/arrays"
"jikko-golang/users"
"jikko-golang/repository"
"github.com/gorilla/mux"
"github.com/swaggo/http-swagger"
)
// @title Jikko-Golang REST API
// @version 1.0
// @description This is an example Web server that sorts arrays and displays user info
// @host localhost:8080
// @BasePath /
// Handler view for the root resource
func homeHandler(writer http.ResponseWriter, request *http.Request) {
array := []int {9, 8, 7, 6, 5, 4, 3, 2, 1}
fmt.Fprintf(writer, "<b>Welcome to the Home Page! 🐲</b>\n")
fmt.Fprintf(writer, "<p>Unordered: %v</p>\n", array)
arrays.Mergesort(array)
fmt.Fprintf(writer, "<p>Ordered: %v</p>\n", array)
}
// Registers resource mappings to Mux router and starts the server
func route(usersHandler *users.UsersHandler) {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", homeHandler)
router.HandleFunc("/arrays", arrays.ArraysHandler).Methods("POST")
router.HandleFunc("/users", usersHandler.UsersRootHandler)
router.HandleFunc("/users/{id}", usersHandler.UserDetailHandler)
router.PathPrefix("/swagger").Handler(httpSwagger.Handler(
httpSwagger.URL("http://localhost:8080/swagger/doc.json"),
))
router.NotFoundHandler = http.HandlerFunc(usersHandler.NotFoundHandler)
fmt.Println("Started server on port 8080")
log.Fatal(http.ListenAndServe(":8080", router))
}
// Entry point
func main() {
dbConn, err := repository.ConnectToDatabase()
defer dbConn.Close()
if err != nil {
fmt.Println("Could not create connection object")
panic(err)
}
err = dbConn.Ping()
if err != nil {
fmt.Println("Couldn't connect to the database. 'users' won't work properly")
panic(err)
}
fmt.Println("Connection to database was successful!")
usersHandler := users.UsersHandler{DbConn: dbConn}
route(&usersHandler)
}