This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
79 lines (67 loc) · 2.23 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
package main
import (
"net/http"
"os"
"database/sql"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
_ "github.com/lib/pq"
log "github.com/sirupsen/logrus"
)
var (
db *sql.DB
conf *Conf
)
func main() {
utils := Utils{}
utils.LoadConfig()
// log.Info(conf)
// log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
e := echo.New()
e.Pre(middleware.RemoveTrailingSlash())
// XSS
e.Use(middleware.Secure())
e.Use(middleware.Logger())
e.Use(middleware.Recover())
//CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: conf.APP.CORS_Origins,
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
}))
auth := Auth{}
user := User{}
material := Material{}
var IsLoggedIn = middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte(conf.Secret),
Skipper: auth.skipper,
})
dbInfo := conf.DB
db = getDB(dbInfo.Host, dbInfo.Port, dbInfo.Username, dbInfo.Password, dbInfo.Db)
defer db.Close()
e.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, "Hello! Welcome to CMS!")
})
h := &handler{}
apiGroup := e.Group("/api/" + conf.Version)
apiGroup.Static(conf.APP.StaticPath.Http, conf.APP.StaticPath.Local)
apiGroup.POST("/login", auth.Login)
// 查询所有用户
apiGroup.GET("/users", user.GetAllUsers, IsLoggedIn)
apiGroup.GET("/users/:id", user.GetUser, IsLoggedIn)
apiGroup.POST("/users", user.AddUser, IsLoggedIn)
apiGroup.DELETE("/users/:id", user.DeleteUser, IsLoggedIn)
apiGroup.PUT("/users/:id", user.UpdateUser, IsLoggedIn)
apiGroup.PUT("/users/:id/resetPassword", user.ResetPassword, IsLoggedIn)
apiGroup.GET("/materials/types", material.GetMaterialType, IsLoggedIn)
apiGroup.GET("/materials/types/:id", material.GetMaterialTypeById, IsLoggedIn)
apiGroup.POST("/materials", material.Add, IsLoggedIn)
apiGroup.DELETE("/materials/:id", material.Delete, IsLoggedIn)
apiGroup.PUT("/materials/:id", material.Update, IsLoggedIn)
apiGroup.GET("/materials", material.GetAll, IsLoggedIn)
apiGroup.GET("/materials/:id", material.GetOne, IsLoggedIn)
e.POST("/private", h.Private, IsLoggedIn)
e.GET("/admin", h.Private, IsLoggedIn, isAdmin)
e.Logger.Fatal(e.Start(conf.APP.Addr))
}