-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (68 loc) · 2.02 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/shlason/kaigon/configs"
"github.com/shlason/kaigon/docs"
"github.com/shlason/kaigon/middlewares"
_ "github.com/shlason/kaigon/models"
"github.com/shlason/kaigon/routes"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/sync/errgroup"
)
// @title Kaigon API
// @version 1.0
// @description This is a forum server.
// @contact.name API Support
// @contact.url https://github.com/shlason/kaigon
// @contact.email [email protected]
// @license.name MIT
// @license.url https://github.com/shlason/kaigon/blob/main/LICENSE
// @host kaigon.sidesideeffect.io
// @BasePath /api
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
func main() {
var g errgroup.Group
docs.SwaggerInfo.Schemes = []string{"https"}
r := gin.Default()
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// Common Middlewares
if os.Getenv("CODE_RUN_ENV") == "prod" {
r.Use(middlewares.CORS())
}
r.Use(gin.Recovery())
// Public API
public := r.Group("/api")
// Private API
private := r.Group("/api")
private.Use(middlewares.JWT)
routes.RegisteAccountRoutes(public, private)
routes.RegisteAuthRoutes(public, private)
routes.RegisteForumAndPostRoutes(public, private)
routes.RegisteTopicRoutes(public, private)
routes.RegisteImageRoutes(private)
// TODO: 記得改回 private route
routes.RegisteChatRoutes(public)
routes.RegisteSearchRoutes(public)
routes.RegisteDevelopUtilsRoutes(public)
if os.Getenv("CODE_RUN_ENV") == "prod" {
g.Go(func() error {
return http.ListenAndServe(":http", http.RedirectHandler(fmt.Sprintf("https://%s", configs.Server.Host), http.StatusSeeOther))
})
g.Go(func() error {
return http.Serve(autocert.NewListener(configs.Server.Host), r)
})
if err := g.Wait(); err != nil {
log.Fatal(err)
}
} else {
r.Run()
}
}