-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
62 lines (55 loc) · 1.14 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
60
61
62
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
// Function: Security
//
// Description: This function supplies a middleware for the gin
//
// server to check if the request is allowed or not.
// This is to help keep the server safe from hacking.
func Security() gin.HandlerFunc {
return func(c *gin.Context) {
clientIP := c.ClientIP()
if strings.HasPrefix(clientIP, "127.0.0.1") {
//
// It's okay to serve.
//
c.Next()
} else {
//
// Cancel the request.
//
c.Abort()
}
}
}
func backend(app *App, ctx context.Context) {
//
// This will have the web server backend for EmailIt
//
r := gin.Default()
r.Use(gin.Recovery())
r.Use(Security())
//
// Get the user's home directory for sending files from.
//
hmdir, _ := os.UserHomeDir()
//
// this sets up a static server for the user's home directory. The
// frontend will use it to get pictures and such.
//
r.Use(static.Serve("/filesys", static.LocalFile(hmdir, false)))
//
// Run the server.
//
err := r.Run(":9998")
if err != nil {
fmt.Print("\n Server error:\n", err.Error())
}
}