-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (149 loc) · 3.5 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
175
176
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net/http"
"os/exec"
"github.com/gin-gonic/gin"
)
var stillCommands = []string{
"encoding",
"exif",
"quality",
"awb",
"awbgains",
"timeout",
"exposure",
"metering",
"flicker",
"drc",
"sharpness",
"contrast",
"brightness",
"saturation",
"ISO",
"shutter",
"width",
"height",
}
var stillFlags = []string{
"hflip",
"vflip",
"raw",
}
var yuvCommands = []string{
"awb",
"awbgains",
"timeout",
"exposure",
"metering",
"flicker",
"drc",
"sharpness",
"contrast",
"brightness",
"saturation",
"ISO",
"shutter",
"width",
"height",
}
var yuvFlags = []string{
"rgb",
"bgr",
"luma",
"hflip",
"vflip",
}
var quite bool
func main() {
flag.BoolVar(&quite, "quite", false, "Turn off pi-webcam's output and error logs")
var user string
flag.StringVar(&user, "user", "webcam", "User name to access the camera")
var password string
flag.StringVar(&password, "password", "webcam", "Password to access the camera")
var port int
flag.IntVar(&port, "port", 8080, "Server port")
var tls bool
flag.BoolVar(&tls, "tls", false, "Secured by TLS, needs certFile and keyFile options to be set.")
// HTTPS Settings
var certFile string
var keyFile string
flag.StringVar(&certFile, "certFile", "./cert/certfile.crt", "Path to cert file for TLS")
flag.StringVar(&keyFile, "keyFile", "./cert/keyfile.key", "Path to private key file for TLS")
flag.Parse()
r := gin.New()
r.Use(gin.Recovery())
api := r.Group("/", gin.BasicAuth(gin.Accounts{
user: password,
}))
api.GET("/still", func(c *gin.Context) {
opts := getOptions(c, stillCommands, stillFlags)
cmd := exec.Command("raspistill", opts...)
img, err := cmd.CombinedOutput()
if err != nil {
LogError(err)
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.DataFromReader(http.StatusOK, int64(len(img)), "image/"+c.DefaultQuery("encoding", "jpg"), bytes.NewReader(img), nil)
}
})
api.GET("/yuv", func(c *gin.Context) {
opts := getOptions(c, yuvCommands, yuvFlags)
cmd := exec.Command("raspiyuv", opts...)
img, err := cmd.CombinedOutput()
if err != nil {
LogError(err)
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.DataFromReader(http.StatusOK, int64(len(img)), "image/octet-stream", bytes.NewReader(img), nil)
}
})
api.GET("/shutdown", func(c *gin.Context) {
err := exec.Command("sudo", "shutdown", "-h", "now").Start()
if err != nil {
LogError(err)
c.AbortWithStatus(http.StatusInternalServerError)
} else {
c.String(http.StatusOK, "Going down ...")
}
})
LogInfof("Starting pi-webcam on port:%v\n", port)
if tls {
r.RunTLS(fmt.Sprintf(":%v", port), certFile, keyFile)
} else {
r.Run(fmt.Sprintf(":%v", port))
}
}
func getOptions(c *gin.Context, commands []string, flags []string) (options []string) {
options = []string{"-o", "-"}
for _, command := range commands {
if value, r := c.GetQuery(command); r {
options = append(options, "--"+command, value)
}
}
for _, flag := range flags {
if _, r := c.GetQuery(flag); r {
options = append(options, "--"+flag)
}
}
LogInfof("Options: %s\n", options)
return
}
func LogError(err error) {
if !quite {
log.Println(err.Error())
}
}
func LogInfo(s string) {
if !quite {
log.Println(s)
}
}
func LogInfof(format string, v ...interface{}) {
if !quite {
log.Printf(format, v...)
}
}