-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
46 lines (37 loc) · 1.17 KB
/
base.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
package controllers
import (
"fmt"
"log"
"net/http"
//"time"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
_ "github.com/tstult5/clouty-orderbook-service/docs"
httpSwagger "github.com/swaggo/http-swagger"
//_ "github.com/jinzhu/gorm/dialects/postgres" //postgres database driver - unused
"github.com/tstult5/clouty-orderbook-service/api/models"
)
type Server struct {
DB *gorm.DB
Router *mux.Router
}
func (server *Server) Initialize(Dbdriver, DbUser, DbPassword, DbPort, DbHost, DbName string) {
//DB environment vars unused for sqlite, placeholder for postgres, etc.
var err error
server.DB, err = gorm.Open("sqlite3","file::memory:?cache=shared")
if err != nil {
fmt.Println(err)
fmt.Println("Unable to open DB for initialization")
}
server.DB.Debug().AutoMigrate(&models.Order{}) //database migration
server.Router = mux.NewRouter()
//defer db.Close()
// Swagger
server.Router.PathPrefix("/swagger").Handler(httpSwagger.WrapHandler)
server.initializeRoutes()
}
func (server *Server) Run(addr string) {
fmt.Println("Listening to port 8080")
log.Fatal(http.ListenAndServe(addr, server.Router))
}