| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package basic
- import (
- "errors"
- "net/http"
- "github.com/gin-gonic/gin"
- "gorm.io/driver/sqlite"
- "gorm.io/gorm"
- )
- var FileDirPath = "/home/ubuntu/prime_server/files/"
- var db *gorm.DB
- func DB() *gorm.DB {
- return db
- }
- var r *gin.Engine
- func R() *gin.Engine {
- return r
- }
- var ErrFriendExist = errors.New("Friend already exists.")
- var ErrForbidden = errors.New("Forbidden.")
- var ErrApplicationAlreadyReplied = errors.New("Already replied.")
- func ErrCode(err error) (code int) {
- switch err {
- case ErrForbidden, ErrApplicationAlreadyReplied:
- code = http.StatusForbidden
- case gorm.ErrRecordNotFound:
- code = http.StatusNotFound
- case ErrFriendExist:
- code = http.StatusConflict
- default:
- code = http.StatusInternalServerError
- }
- return code
- }
- func init() {
- _db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
- if err != nil {
- panic("failed to connect database")
- }
- db = _db
- r = gin.Default()
- }
|