-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
76 lines (61 loc) · 1.62 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/itsjamie/gin-cors"
"labix.org/v2/mgo"
"time"
)
var DB *mgo.Session
func GetDbConnection() *mgo.Session {
return DB
}
func main() {
version := "1"
setupServer(nil)
if DB == nil {
fmt.Printf("DB connection failed: %s", DB)
}
engine := gin.Default()
engine.Use(cors.Middleware(cors.Config{
Origins: "http://localhost:8000, https://gallifrey.sklirg.io, http://despina.128.no, https://despina.128.no",
Methods: "GET, PUT, POST, DELETE",
RequestHeaders: "Origin, Authorization, Content-Type",
ExposedHeaders: "",
MaxAge: 50 * time.Second,
Credentials: true,
ValidateHeaders: false,
}))
AddRoutes(version, engine)
engine.Static("/static", "./static")
loadDefaultTemplates("templates/")
engine.Run()
}
func setupServer(db *mgo.Session) {
if db == nil {
DB = ConnectToDb()
} else {
DB = db
}
}
const enable_access_control = false
func AddRoutes(version string, engine *gin.Engine) {
oauth2_routes := engine.Group("oauth2")
{
oauth2_routes.GET("/callback", OAuth2Callback)
}
public_routes := engine.Group(fmt.Sprintf("api/v%s", version))
{
public_routes.GET("/template/", GetAllTemplates)
public_routes.GET("/template/:template_name", GetTemplate)
}
template_restricted := engine.Group(fmt.Sprintf("api/v%s", version))
if enable_access_control {
template_restricted.Use(ValidateRequest("write"))
}
{
template_restricted.POST("/template/", InsertTemplate)
template_restricted.PUT("/template/:template_name", GetTemplate)
template_restricted.DELETE("/template/:template_name", DeleteTemplate)
}
}