initial commit
This commit is contained in:
52
main.go
Normal file
52
main.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"astra-users/handler"
|
||||
"astra-users/model"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
newLogger := logger.New(
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
|
||||
logger.Config{
|
||||
SlowThreshold: time.Second, // Slow SQL threshold
|
||||
LogLevel: logger.Info, // Log level
|
||||
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
|
||||
ParameterizedQueries: true, // Don't include params in the SQL log
|
||||
Colorful: false, // Disable color
|
||||
},
|
||||
)
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
|
||||
Logger: newLogger,
|
||||
})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
e := echo.New()
|
||||
|
||||
db.AutoMigrate(&model.User{})
|
||||
|
||||
e.Use(middleware.Logger())
|
||||
|
||||
var h = &handler.Handler{DB: db}
|
||||
|
||||
e.POST("/login", h.Login)
|
||||
e.GET("/me", h.GetCurrentUser)
|
||||
e.POST("/", h.CreateUser)
|
||||
e.GET("/", h.GetUsers)
|
||||
e.GET("/:id", h.GetUsers)
|
||||
e.PATCH("/:id", h.UpdateUser)
|
||||
e.DELETE("/:id", h.DeleteUser)
|
||||
e.GET("/health", h.Health)
|
||||
|
||||
e.Logger.Fatal(e.Start(":8081"))
|
||||
}
|
||||
Reference in New Issue
Block a user