This repository was archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaka.go
More file actions
90 lines (71 loc) · 1.75 KB
/
aka.go
File metadata and controls
90 lines (71 loc) · 1.75 KB
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
package main
import (
"log"
"net/http"
"time"
"github.com/STRATBOX/aka/company"
"github.com/STRATBOX/aka/db"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
"github.com/koding/multiconfig"
mgo "gopkg.in/mgo.v2"
)
// const (
// // app | service name
// appname string = "stratbox.aka.srv.companies"
// )
// Server type for config
type Server struct {
Port string
}
// Database type for config
type Database struct {
Name string
URL string
}
// Config type for app level settings
type Config struct {
Server Server
Database Database
}
func main() {
var c Config
// set config file path directly
conf := multiconfig.NewWithPath("aka.toml")
// read config file
conf.MustLoad(&c)
r := chi.NewRouter()
// middleware
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.URLFormat)
r.Use(render.SetContentType(render.ContentTypeJSON))
repository, _ := db.NewRepository(c.Database.Name, getSession(c.Database.URL))
companyservice := company.NewService(repository)
companies := company.NewHandler(companyservice)
r.Mount("/companies", companies.Routes())
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Addr: c.Server.Port,
Handler: r,
}
err := srv.ListenAndServe()
if err != nil {
log.Fatal(err.Error())
}
}
// GetMongoSession Creates a new session.
// if mgoSession is nil i.e there is no active mongo session.
// If there is an active mongo session it will return a Clone
func getSession(url string) *mgo.Session {
session, err := mgo.Dial(url)
if err != nil {
log.Fatal("Failed to start the Mongo session")
}
session.SetMode(mgo.Monotonic, true)
return session
}