-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (135 loc) · 3.22 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"database/sql"
"github.com/dgrijalva/jwt-go"
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
_ "github.com/mattn/go-sqlite3"
)
var testingMode bool
type server struct {
db *sql.DB
router *chi.Mux
token *jwtauth.JWTAuth
lc *ldapConn
env *env
}
type env struct {
dbFile string
listen string
apiKey string
ekey string
jwtSecret string
ldapBase string
ldapServer string
ldapPort string
ldapBindDN string
ldapBindPassword string
}
func newServer(e *env) *server {
// New server
s := server{
router: chi.NewRouter(),
db: newDb(*e),
token: jwtauth.New("HS256", []byte(e.jwtSecret), nil),
lc: newLdapConn(*e),
env: e,
}
s.router.Routes() // register handlers
return &s
}
func newLdapConn(e env) *ldapConn {
lc := ldapConn{
Base: e.ldapBase,
Server: e.ldapServer,
Port: e.ldapPort,
BindDN: e.ldapBindDN,
BindPassword: e.ldapBindPassword,
}
return &lc
}
func newEnv(
dbFile string,
listen string,
apiKey string,
ekey string,
jwtSecret string,
ldapBase string,
ldapServer string,
ldapPort string,
ldapBindDN string,
ldapBindPassword string) *env {
if ldapPort == "" {
ldapPort = "636"
}
if listen == "" {
listen = "0.0.0.0:8081"
}
if ldapServer == "" {
log.Fatalf("Could not parse env API_LDAP_SERVER %s", ldapServer)
}
if ldapBindDN == "" {
log.Fatalf("Could not parse env API_LDAP_BIND_DN %s", ldapBindDN)
}
if ldapBindPassword == "" {
log.Fatalf("Could not parse env API_LDAP_BIND_PASSWORD %s", ldapBindPassword)
}
if dbFile == "" {
dbFile = "/data/otu-ldap/otu.db"
}
if ldapBase == "" {
ldapBase = "cn=accounts,dc=pasientsky,dc=no"
}
e := env{
dbFile: dbFile,
listen: listen,
apiKey: apiKey,
ekey: ekey,
jwtSecret: jwtSecret,
ldapBase: ldapBase,
ldapServer: ldapServer,
ldapPort: ldapPort,
ldapBindDN: ldapBindDN,
ldapBindPassword: ldapBindPassword,
}
log.Printf("Server started with env: %+v", e)
return &e
}
func newDb(e env) *sql.DB {
// Setup db conn
db, err := sql.Open("sqlite3", e.dbFile)
if err != nil {
log.Fatalf("Could not open db: %q", err)
}
log.Printf("Connected to DB at %s", e.dbFile)
return db
}
func main() {
s := newServer(
newEnv(os.Getenv("API_DB_FILE"),
os.Getenv("API_LISTEN"),
os.Getenv("API_KEY"),
os.Getenv("API_ENCRYPTION_KEY"),
os.Getenv("API_JWT_SECRET"),
os.Getenv("API_LDAP_BASE"),
os.Getenv("API_LDAP_SERVER"),
os.Getenv("API_LDAP_PORT"),
os.Getenv("API_LDAP_BIND_DN"),
os.Getenv("API_LDAP_BIND_PASSWORD"),
),
)
log.Printf("Started REST API on %s with db %s", s.env.listen, s.env.dbFile)
// Create temp token
_, ts, _ := s.token.Encode(jwt.MapClaims{"user_id": "apiTest", "exp": jwtauth.ExpireIn(3600 * time.Minute)})
// Start rand seed
rand.Seed(time.Now().UnixNano())
log.Printf("Started with temp token: %s", ts)
log.Printf("Check API status by visiting: http://localhost:%s/api/v1/ping", strings.Split(s.env.listen, ":")[1])
log.Fatal(http.ListenAndServe(s.env.listen, s.routes()))
}