-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
59 lines (47 loc) · 2.18 KB
/
server.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
package main
// Author: Valentin Kuznetsov <vkuznet [AT] gmain [DOT] com>
// Server module
// Credits:
// 2fa QR code and server handler to verify 2FA code
// https://www.socketloop.com/tutorials/golang-verify-token-from-google-authenticator-app
// How to generate QR code
// https://socketloop.com/tutorials/golang-how-to-generate-qr-codes
// https://www.socketloop.com/tutorials/golang-generate-qr-codes-for-google-authenticator-app-and-fix-cannot-interpret-qr-code-error
// 2FA authentication code
// https://www.thepolyglotdeveloper.com/2017/05/add-two-factor-authentication-golang-restful-api/
// https://www.thepolyglotdeveloper.com/2017/03/authenticate-a-golang-api-with-json-web-tokens/
// https://www.thepolyglotdeveloper.com/2017/05/implement-2fa-time-based-one-time-passwords-node-js-api/
import (
"fmt"
"log"
"net/http"
"github.com/dchest/captcha"
"github.com/gorilla/mux"
)
// HomeHandler represents server home page
// server represents our HTTP server
// TODO: add HTTPs when Config will provide server certs
func server() {
// setup our DB backend
setupDB(Config.DBFile)
router := mux.NewRouter()
router.StrictSlash(true) // to allow /route and /route/ end-points
router.HandleFunc("/authenticate", AuthHandler).Methods("POST")
router.HandleFunc("/verify", VerifyHandler).Methods("POST")
router.HandleFunc("/api", ValidateMiddleware(ApiHandler)).Methods("GET", "POST")
router.HandleFunc("/user", UserHandler).Methods("POST")
router.HandleFunc("/qrcode", QRHandler)
router.HandleFunc("/signup", SignUpHandler).Methods("GET")
router.HandleFunc("/", HomeHandler).Methods("GET")
// this is for displaying the QR code on /qr end point
// and static area which holds user's images
fileServer := http.StripPrefix("/static/", http.FileServer(http.Dir(Config.StaticDir)))
router.PathPrefix("/static/{user:[0-9a-zA-Z-]+}/{file:[0-9a-zA-Z-\\.]+}").Handler(fileServer)
// static css content
router.PathPrefix("/css/{file:[0-9a-zA-Z-\\.]+}").Handler(fileServer)
// add captcha server
captchaServer := captcha.Server(captcha.StdWidth, captcha.StdHeight)
router.PathPrefix("/captcha/").Handler(captchaServer)
addr := fmt.Sprintf(":%d", Config.Port)
log.Fatal(http.ListenAndServe(addr, router))
}