-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_api.go
97 lines (79 loc) · 2.22 KB
/
web_api.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
package main
import (
"embed"
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/danomagnum/admin"
)
//go:embed static/*
var staticFiles embed.FS
var NOHTTPS = flag.Bool("nohttps", false, "Disable https server")
var NOHTTP = flag.Bool("nohttp", false, "Disable http server")
var portflag = flag.Int("port", 8000, "Port to listen for http connections on")
var sslportflag = flag.Int("sslport", 8001, "Port to listen for https connections on")
var Admin *admin.Admin
func web_startup() {
mux := http.NewServeMux()
// Here we set up all the http targets and assign them to functions.
//mux.HandleFunc("/", api_version)
mux.HandleFunc("/read/{driver}/{tag...}", api_read)
mux.HandleFunc("/read_multi/", api_read_multi)
mux.HandleFunc("/write/{driver}/{tag...}", api_write)
mux.HandleFunc("/view/{screen}", api_view)
mux.Handle("/static/", http.FileServerFS(staticFiles))
mux.HandleFunc("/", home)
mux.Handle("/admin/", Admin)
for _, d := range drivers {
Admin.RegisterStruct(d.Name(), d)
}
var handler http.Handler = mux
if *basicHTTPAuthEnabled {
a := basicHTTPAuth{
User: *basicUser,
Pass: *basicPass,
}
handler = a.Handler(handler)
}
if !*NOHTTP {
port := fmt.Sprintf(":%v", *portflag)
// And finally this starts the server
go func() {
http_server := &http.Server{
Addr: port,
ReadHeaderTimeout: time.Minute,
Handler: handler,
}
err := http_server.ListenAndServe()
if err != nil {
log.Printf("Problem with http server. %v", err)
}
}()
} else {
log.Printf("Not starting HTTP server (NOHTTP = true)")
}
if !*NOHTTPS {
go func() {
tls_port := fmt.Sprintf(":%v", *sslportflag)
certFile := "cert.crt"
keyFile := "key.pem"
https_server := &http.Server{
Addr: tls_port,
ReadHeaderTimeout: time.Minute,
Handler: handler,
}
log.Printf("Starting HTTPS server on %s", tls_port)
err := https_server.ListenAndServeTLS(certFile, keyFile)
if err != nil {
log.Printf("Problem with https server. %v", err)
}
}()
} else {
log.Printf("Not starting HTTPS server (NOHTTPS = true)")
}
}
func home(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/view/home", http.StatusSeeOther)
}