-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
106 lines (91 loc) · 2.37 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
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/addetz/secure-code-go/demo3/handlers"
echojwt "github.com/labstack/echo-jwt/v4"
"github.com/golang-jwt/jwt/v5"
echo "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
const TIMEOUT = 3 * time.Second
type Response struct {
Message string `json:"message"`
}
func main() {
// Read paths to certificate & private key from environment variables
certFile, ok := os.LookupEnv("SERVER_CERT_FILE")
if !ok {
log.Fatal("SERVER_CERT_FILE variable must be set")
}
keyFile, ok := os.LookupEnv("SERVER_KEY_FILE")
if !ok {
log.Fatal("SERVER_KEY_FILE variable must be set")
}
signingKey, ok := os.LookupEnv("SIGNING_KEY")
if !ok {
log.Fatal("SIGNING_KEY variable must be set")
}
// Read port if one is set
port := readPort()
// Set up internal services
userAuthService := handlers.NewUserAuthService(signingKey)
// Initialise echo
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Configure server
s := http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: e,
ReadTimeout: TIMEOUT,
ReadHeaderTimeout: TIMEOUT,
WriteTimeout: TIMEOUT,
IdleTimeout: TIMEOUT,
}
// Set up the root route
e.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, &Response{
Message: "Hello, Gophers!",
})
})
// Set up authentication routes
e.POST("/signup", func(c echo.Context) error {
return userAuthService.SignUp(c)
})
e.POST("/login", func(c echo.Context) error {
return userAuthService.Login(c)
})
// Restricted route only for logged in users
r := e.Group("/restricted")
config := echojwt.Config{
NewClaimsFunc: func(c echo.Context) jwt.Claims {
return new(handlers.JWTCustomClaims)
},
SigningKey: []byte(signingKey),
}
r.Use(echojwt.WithConfig(config))
r.GET("", func(c echo.Context) error {
return userAuthService.RestrictedPath(c)
})
r.GET("/secretNotes/:id", func(c echo.Context) error {
return userAuthService.GetUserNotes(c)
})
r.POST("/secretNotes/:id", func(c echo.Context) error {
return userAuthService.AddUserNote(c)
})
log.Printf("Listening on :%s...\n", port)
if err := s.ListenAndServeTLS(certFile, keyFile); err != http.ErrServerClosed {
log.Fatal(err)
}
}
func readPort() string {
port, ok := os.LookupEnv("SERVER_PORT")
if !ok {
return "1323"
}
return port
}