-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (49 loc) · 1.7 KB
/
main.go
File metadata and controls
61 lines (49 loc) · 1.7 KB
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
package main
import (
_ "InfoLinkAPI/docs"
"InfoLinkAPI/src/models"
"InfoLinkAPI/src/routes"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"net/http"
)
func main() {
// データベース接続文字列
dsn := "root:root@(mysql-container:3306)/demo?charset=utf8&parseTime=True&loc=Local"
// データベースへの接続
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
panic("データベースへの接続に失敗しました" + err.Error())
}
// モデルのマイグレーション
db.AutoMigrate(&models.SyllabusBaseInfo{})
router := gin.Default()
router.Use(func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
})
// Swaggerの設定
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// ルートとコントローラーを設定
routes.SyllabusRoutes(router, db)
routes.SyllabusRandomRoutes(router, db)
routes.SyllabusFacultyRoutes(router, db)
routes.SyllabusTeacherRoutes(router, db)
routes.SyllabusCourseRoutes(router, db)
// ....
// ....
router.Run(":8080")
}