forked from Eore/sismadak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (65 loc) · 2.15 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 (
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/eore/sismadak/drivers"
"github.com/eore/sismadak/handlers"
"github.com/eore/sismadak/helpers"
"github.com/eore/sismadak/models"
"github.com/eore/sismadak/repositories"
"github.com/eore/sismadak/routes"
)
const (
portServer = ":9000"
)
func inisiasiRouter(con *sql.DB) *http.ServeMux {
log.Println("Inisiasi router...")
router := http.NewServeMux()
routes.RouterPokja(router, handlers.InisiasiHandlerPokja(repositories.InisiasiRepoPokja(con)))
routes.RouterKategori(router, handlers.InisiasiHandlerKategori(repositories.InisiasiRepoKategori(con)))
log.Println("Inisiasi router selesai")
return router
}
func inisiasiModel(con *sql.DB) {
log.Println("Inisiasi model...")
helpers.InisiasiModel(con, "pokja", models.Pokja{})
helpers.InisiasiModel(con, "dokumen", models.Dokumen{})
helpers.InisiasiModel(con, "kategori", models.Kategori{})
log.Println("Inisiasi model selesai")
}
func main() {
var host, port, database, user, pass string
flag.StringVar(&host, "dbhost", "", "host database")
flag.StringVar(&port, "dbport", "", "port database")
flag.StringVar(&database, "database", "", "nama database")
flag.StringVar(&user, "dbusername", "", "username database")
flag.StringVar(&pass, "dbpassword", "", "password database")
flag.Parse()
if host == "" || port == "" || database == "" || user == "" || pass == "" {
fmt.Fprintln(os.Stdout, "SISMADAK v1.0.0")
fmt.Fprint(os.Stdout, "Input host database: ")
fmt.Scan(&host)
fmt.Fprint(os.Stdout, "Input port database: ")
fmt.Scan(&port)
fmt.Fprint(os.Stdout, "Input nama database: ")
fmt.Scan(&database)
fmt.Fprint(os.Stdout, "Input username database: ")
fmt.Scan(&user)
fmt.Fprint(os.Stdout, "Input password database: ")
fmt.Scan(&pass)
}
log.Println("Koneksi ke database...")
con := drivers.KoneksiDatabase("postgres", host, port, database, user, pass)
log.Println("Koneksi ke database berhasil")
inisiasiModel(con)
router := inisiasiRouter(con)
log.Printf("Server berjalan di port%s", portServer)
err := http.ListenAndServe(portServer, router)
if err != nil {
log.Fatalln("Gagal memulai server")
}
}