1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Added HTTPS session support

This commit is contained in:
Zealotree 2017-06-23 09:40:35 -04:00
parent 68ecbdd595
commit 2101b706d3
3 changed files with 39 additions and 4 deletions

View File

@ -35,6 +35,16 @@ cowyo
and it will start a server listening on `0.0.0.0:8050`. To view it, just go to http://localhost:8050 (the server prints out the local IP for your info if you want to do LAN networking). You can change the port with `-port X`, and you can listen *only* on localhost using `-host localhost`. and it will start a server listening on `0.0.0.0:8050`. To view it, just go to http://localhost:8050 (the server prints out the local IP for your info if you want to do LAN networking). You can change the port with `-port X`, and you can listen *only* on localhost using `-host localhost`.
### Running with TLS
Specify a matching pair of SSL Certificate and Key to run cowyo using https. Cowyo will now run in a secure session.
*N.B. Let's Encrypt is a CA that signs free and signed certificates.*
```
cowyo --cert "/path/to/server.crt" --key "/p/t/server.key"
```
## Usage ## Usage
*cowyo* is straightforward to use. Here are some of the basic features: *cowyo* is straightforward to use. Here are some of the basic features:

View File

@ -12,7 +12,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func serve(host, port string) { func serve(host, port, crt_path, key_path string, TLS bool) {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
router := gin.Default() router := gin.Default()
router.HTMLRender = loadTemplates("index.tmpl") router.HTMLRender = loadTemplates("index.tmpl")
@ -32,7 +32,11 @@ func serve(host, port string) {
router.DELETE("/oldlist", handleClearOldListItems) router.DELETE("/oldlist", handleClearOldListItems)
router.DELETE("/listitem", deleteListItem) router.DELETE("/listitem", deleteListItem)
router.Run(host + ":" + port) if TLS {
http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router)
} else {
router.Run(host + ":" + port)
}
} }
func loadTemplates(list ...string) multitemplate.Render { func loadTemplates(list ...string) multitemplate.Render {

25
main.go
View File

@ -24,11 +24,22 @@ func main() {
pathToData = c.GlobalString("data") pathToData = c.GlobalString("data")
os.MkdirAll(pathToData, 0755) os.MkdirAll(pathToData, 0755)
host := c.GlobalString("host") host := c.GlobalString("host")
crt_f := c.GlobalString("cert") // crt flag
key_f := c.GlobalString("key") // key flag
if host == "" { if host == "" {
host = GetLocalIP() host = GetLocalIP()
} }
fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port")) TLS := false
serve(c.GlobalString("host"), c.GlobalString("port")) if crt_f != "" && key_f != "" {
TLS = true
}
if TLS {
fmt.Printf("\nRunning cowyo server (version %s) at https://%s:%s\n\n", version, host, c.GlobalString("port"))
} else {
fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port"))
}
fmt.Println(TLS)
serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS)
return nil return nil
} }
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
@ -52,6 +63,16 @@ func main() {
Value: "8050", Value: "8050",
Usage: "port to use", Usage: "port to use",
}, },
cli.StringFlag{
Name: "cert",
Value: "",
Usage: "Absolute Path to SSL Public Cert",
},
cli.StringFlag{
Name: "key",
Value: "",
Usage: "Aboslute Path to corresponding private key",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "debug, d", Name: "debug, d",
Usage: "turn on debugging", Usage: "turn on debugging",