-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.go
executable file
·168 lines (151 loc) · 4.06 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
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
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
_ "github.com/heroku/x/hmetrics/onload"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/pkg/errors"
"github.com/uclaacm/teach-la-go-backend/db"
"github.com/uclaacm/teach-la-go-backend/handler"
"github.com/urfave/cli/v2"
)
func serve(c *cli.Context) error {
e := echo.New()
e.HideBanner = true
if c.Bool("verbose") {
e.Logger.SetLevel(log.DEBUG)
}
// middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.Gzip())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderContentType},
AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete},
}))
// Check for working credentials in the following partial order:
// - JSON
// - .env
// - TLACFG
jsonPath, dotenvPath := c.String("json"), c.String("dotenv")
var (
d *db.DB
err error
)
switch {
case jsonPath != "":
d, err = db.OpenFromJSON(context.Background(), jsonPath)
case dotenvPath != "":
if err := godotenv.Load(dotenvPath); err != nil {
e.Logger.Error(errors.Wrap(err, "failed to open .env file"))
}
d, err = db.Open(context.Background(), os.Getenv(db.DefaultEnvVar))
default:
d, err = db.Open(context.Background(), os.Getenv(db.DefaultEnvVar))
}
if err != nil {
e.Logger.Fatal(errors.Wrap(err, "failed to open connection to firestore"))
return err
}
defer d.Close()
// Register our database handler to every Echo context.
e.Use(func(nxt echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return nxt(&db.DBContext{
Context: c,
TLADB: d,
})
}
})
// user management
e.GET("/user/get", handler.GetUser)
e.PUT("/user/update", d.UpdateUser)
e.POST("/user/create", handler.CreateUser)
// program management
e.GET("/program/get", handler.GetProgram)
e.PUT("/program/update", d.UpdateProgram)
e.POST("/program/create", handler.CreateProgram)
e.DELETE("/program/delete", handler.DeleteProgram)
// class management
e.POST("/class/get", handler.GetClass)
e.POST("/class/create", d.CreateClass)
e.PUT("/class/join", handler.JoinClass)
e.PUT("/class/leave", d.LeaveClass)
e.POST("/class/members", handler.GetClassMembers)
// collaborative coding management
e.POST("/collab/create", d.CreateCollab)
e.GET("/collab/join/:id", d.JoinCollab)
// check for PORT variable.
var port string
if osPort := os.Getenv("PORT"); osPort != "" {
port = osPort
} else {
port = c.String("port")
}
// server configuration
s := &http.Server{
Addr: ":" + port,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
e.Logger.Fatal(e.StartServer(s))
return nil
}
func main() {
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"V"},
Usage: "Print the version and exit",
}
cli.HelpFlag = &cli.BoolFlag{
Name: "help",
Aliases: []string{"h"},
Usage: "Show help",
}
app := &cli.App{
Name: "Teach LA Go Backend",
Usage: "tlabe [options]",
Description: "Teach LA's editor backend.",
Version: "1.0.0",
HideHelpCommand: true,
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dotenv",
Aliases: []string{"e"},
Required: false,
Usage: "Specify a path to a dotenv file to specify credentials",
},
&cli.StringFlag{
Name: "json",
Aliases: []string{"j"},
Required: false,
Usage: "Specify a path to a JSON file to specify credentials",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "Change the log level used by echo's logger middleware",
},
&cli.StringFlag{
Name: "port",
Aliases: []string{"p"},
Value: "8081",
Usage: "Change the port number",
},
},
Action: serve,
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("Failed to start! %v", err)
}
}