first commit
This commit is contained in:
50
api/api.go
Normal file
50
api/api.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
host string
|
||||
port int
|
||||
router *gin.Engine
|
||||
}
|
||||
|
||||
func NewAPI(host string, port int) *API {
|
||||
r := gin.Default()
|
||||
r.Use(cors.New(cors.Config{
|
||||
//AllowOrigins: []string{"http://localhost:9000"}, // frontend origin
|
||||
AllowOrigins: []string{"*"}, // frontend origin
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
|
||||
ExposeHeaders: []string{"Content-Length"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 12 * time.Hour,
|
||||
}))
|
||||
|
||||
apiHandler := NewAPIHandler()
|
||||
|
||||
v1 := r.Group("v1")
|
||||
v1.GET("/members", apiHandler.GetMemberById)
|
||||
v1.GET("/events", apiHandler.GetEventById)
|
||||
v1.GET("/events/new", apiHandler.StartNewEvent)
|
||||
|
||||
v1.POST("/database/open", apiHandler.OpenDatabase)
|
||||
v1.POST("/members/add", apiHandler.AddNewMember)
|
||||
v1.POST("/members/edit", apiHandler.EditMember)
|
||||
v1.POST("/members/delete", apiHandler.DeleteMember)
|
||||
v1.POST("/members/import/csv", apiHandler.ImportCSV)
|
||||
|
||||
return &API{host: host,
|
||||
port: port,
|
||||
router: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (api *API) Run() error {
|
||||
return api.router.Run(fmt.Sprintf("%s:%d", api.host, api.port))
|
||||
}
|
Reference in New Issue
Block a user