-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
33 lines (28 loc) · 829 Bytes
/
main.go
File metadata and controls
33 lines (28 loc) · 829 Bytes
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
package main
import (
"encoding/json"
"log"
"net/http"
)
func main() {
setupApi()
}
func setupApi() {
manager := NewManager()
manager.RegisterEventHandler(EventSendMessage, SendMessage)
http.Handle("GET /", http.FileServer(http.Dir("./frontend")))
http.HandleFunc("GET /otp", getOTP)
http.HandleFunc("GET /ws", manager.serverWebsocket)
log.Fatal(http.ListenAndServeTLS(":8080", "cert.crt", "cert.key", nil))
}
func getOTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
otp, err := issueOTP("admin")
if err != nil {
log.Printf("failed to generate otp: %v", err)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": "failed to generate otp"})
return
}
json.NewEncoder(w).Encode(map[string]string{"otp": otp})
}