-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (57 loc) · 1.6 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
package main
import (
"fmt"
"goapi/apis"
"goapi/app"
"goapi/daos"
"goapi/services"
"log"
"net/http"
"os"
"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
func main() {
// INFO: load app configs here
if err := app.LoadConfig("./config"); err != nil {
panic(fmt.Errorf("Invalid application configuration: %s", err))
}
// TODO: Connect to database, app level
// TODO: Connect redis store, app level
// TODO: Create logger instance? if required
logger := logrus.New()
// start the server
address := fmt.Sprintf(":%v", app.Config.ServerPort)
log.Printf("server %v is started at %v\n", app.Version, address)
panic(http.ListenAndServe(address, buildRoutes(logger)))
}
func buildRoutes(l *logrus.Logger) *mux.Router {
// create instance of mux router
r := mux.NewRouter()
// All middleware goes here
// r.Use(MIDDLEWARE_FUNCTION)
// Initialize not found handler
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("Rosource not found"))
})
// Create Logger Instance
logger := logrus.New()
// Replace os.Stdout to some other io.Writer type
logger.Out = os.Stdout
// Set log level
logger.SetLevel(logrus.DebugLevel)
// set all logger configs here
// log to file or stdout
// Creates request scope
r.Use(app.Init(logger))
// Set path prefix/route group
r.PathPrefix("v1")
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "Hello World")
}).Methods("GET")
userDao := daos.NewUserDao()
apis.ServeUserResource(r, services.NewUserService(userDao))
return r
}