init db, init gin

This commit is contained in:
celogeek 2022-01-29 18:43:07 +01:00
parent b604f157ab
commit 87ee1cc6c6
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
14 changed files with 327 additions and 82 deletions

16
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
// Pointez pour afficher la description des attributs existants.
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "./cmd/photos-api",
"cwd": "${workspaceFolder}"
}
]
}

93
cmd/photos-api/main.go Normal file
View File

@ -0,0 +1,93 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strconv"
gomysql "github.com/go-sql-driver/mysql"
"gitlab.celogeek.com/photos/api/internal/photos/api"
)
var (
mapEnv = map[string]string{}
mapCurrentEnv = map[string]string{}
listen = fmt.Sprintf("%s:%d", getEnv("LISTEN_HOST", "[::]"), getEnvUInt("LISTEN_PORT", 8000))
mysqlAddr = fmt.Sprintf("%s:%d", getEnv("MYSQL_HOST", "localhost"), getEnvUInt("MYSQL_PORT", 3306))
mysqlUser = getEnv("MYSQL_USER", "photos")
mysqlPassword = getEnv("MYSQL_PASSWD", "photos")
mysqlDatabase = getEnv("MYSQL_DB", "photos")
logger = log.New(os.Stderr, "[Photos] ", log.LstdFlags)
getEnvConfig = false
getCurrentEnvConfig = false
)
func getEnv(key, fallback string) string {
mapEnv[key] = fallback
mapCurrentEnv[key] = fallback
if value, ok := os.LookupEnv(key); ok {
mapCurrentEnv[key] = value
return value
}
return fallback
}
func getEnvUInt(key string, fallback uint) uint {
mapEnv[key] = fmt.Sprint(fallback)
mapCurrentEnv[key] = fmt.Sprint(fallback)
if value, ok := os.LookupEnv(key); ok {
uvalue, err := strconv.Atoi(value)
if err != nil {
log.Fatal(err)
}
if uvalue <= 0 {
log.Fatalf("env %s need to greater than 0", key)
}
mapCurrentEnv[key] = fmt.Sprint(uvalue)
return uint(uvalue)
}
return fallback
}
func GetOrderedKeys(o map[string]string) []string {
keys := make([]string, 0, len(o))
for k := range o {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func main() {
config := &api.ServiceConfig{Mysql: gomysql.NewConfig()}
flag.StringVar(&config.Listen, "listen", listen, "Listen address")
flag.StringVar(&config.Mysql.Addr, "mysql-addr", mysqlAddr, "Mysql addr")
flag.StringVar(&config.Mysql.User, "mysql-user", mysqlUser, "Mysql user")
flag.StringVar(&config.Mysql.Passwd, "mysql-password", mysqlPassword, "Mysql password")
flag.StringVar(&config.Mysql.DBName, "mysql-database", mysqlDatabase, "Mysql database")
flag.BoolVar(&getEnvConfig, "generate-config", false, "Generate an env config")
flag.BoolVar(&getCurrentEnvConfig, "generate-current-config", false, "Generate an env config with current env")
flag.Parse()
if getEnvConfig {
for _, k := range GetOrderedKeys(mapEnv) {
fmt.Printf("%s=%q\n", k, mapEnv[k])
}
return
}
if getCurrentEnvConfig {
for _, k := range GetOrderedKeys(mapCurrentEnv) {
fmt.Printf("%s=%q\n", k, mapCurrentEnv[k])
}
return
}
if err := api.New(logger, config).Run(); err != nil {
logger.Fatal(err)
}
}

14
docker-compose.yml Normal file
View File

@ -0,0 +1,14 @@
version: '3.1'
services:
db:
image: arm64v8/mariadb:latest
restart: always
environment:
MYSQL_DATABASE: "photos"
MYSQL_USER: "photos"
MYSQL_PASSWORD: "photos"
MYSQL_RANDOM_ROOT_PASSWORD: "true"
ports:
- "3306:3306"

11
go.mod
View File

@ -2,19 +2,24 @@ module gitlab.celogeek.com/photos/api
go 1.17
require (
github.com/gin-gonic/gin v1.7.7
github.com/go-sql-driver/mysql v1.6.0
gorm.io/driver/mysql v1.2.3
gorm.io/gorm v1.22.5
)
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.7.7 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/jinzhu/gorm v1.9.16 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-sqlite3 v1.14.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect

32
go.sum
View File

@ -1,13 +1,11 @@
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
@ -15,46 +13,40 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o=
github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.3/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -63,7 +55,13 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gorm.io/driver/mysql v1.2.3 h1:cZqzlOfg5Kf1VIdLC1D9hT6Cy9BgxhExLj/2tIgUe7Y=
gorm.io/driver/mysql v1.2.3/go.mod h1:qsiz+XcAyMrS6QY+X3M9R6b/lKM1imKmcuK9kac5LTo=
gorm.io/gorm v1.22.4/go.mod h1:1aeVC+pe9ZmvKZban/gW4QPra7PRoTEssyc922qCAkk=
gorm.io/gorm v1.22.5 h1:lYREBgc02Be/5lSCTuysZZDb6ffL2qrat6fg9CFbvXU=
gorm.io/gorm v1.22.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=

47
internal/photos/api/db.go Normal file
View File

@ -0,0 +1,47 @@
package api
import (
"time"
"gitlab.celogeek.com/photos/api/internal/photos/models"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func (s *Service) Migrate() {
tx := s.DB.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
tx.AutoMigrate(&models.Account{})
}
func (s *Service) DBConfig() {
s.Config.Mysql.Params = map[string]string{
"charset": "utf8mb4",
}
s.Config.Mysql.Collation = "utf8mb4_bin"
s.Config.Mysql.ParseTime = true
s.Config.Mysql.Loc = time.UTC
s.Config.Mysql.Net = "tcp"
s.Config.Mysql.InterpolateParams = true
mysql.CreateClauses = []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"}
}
func (s *Service) DBConnect() {
db, err := gorm.Open(mysql.Open(s.Config.Mysql.FormatDSN()), &gorm.Config{
Logger: s.GormLogger,
SkipDefaultTransaction: true,
PrepareStmt: true,
})
if err != nil {
s.Logger.Fatal(err)
}
s.DB = db
}
func (s *Service) SetupDB() {
s.DBConfig()
s.DBConnect()
s.Migrate()
}

View File

@ -0,0 +1,19 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (s *Service) Recovery(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
s.Logger.Println("[PANIC]", err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "an unexpected error occur",
})
}
}()
c.Next()
}

101
internal/photos/api/main.go Normal file
View File

@ -0,0 +1,101 @@
package api
import (
"context"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-gonic/gin"
gomysql "github.com/go-sql-driver/mysql"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
)
type Service struct {
Gin *gin.Engine
DB *gorm.DB
Config *ServiceConfig
Logger *log.Logger
GormLogger gormlogger.Interface
}
type ServiceConfig struct {
Listen string
Mysql *gomysql.Config
}
func New(logger *log.Logger, config *ServiceConfig) *Service {
return &Service{
Gin: gin.New(),
Config: config,
Logger: logger,
GormLogger: gormlogger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
gormlogger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: gormlogger.Error, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: true, // Disable color
},
),
}
}
func (s *Service) SetupRoutes() {
s.Gin.Use(gin.Logger())
s.Gin.Use(s.Recovery)
s.Gin.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"error": "this route doesn't exists",
})
})
}
func (s *Service) Run() error {
rand.Seed(time.Now().UnixNano())
s.SetupRoutes()
s.SetupDB()
srv := &http.Server{
Addr: s.Config.Listen,
Handler: s.Gin,
}
quit := make(chan os.Signal, 1)
var runError error
go func() {
// service connections
if runError = srv.ListenAndServe(); runError != nil {
quit <- os.Interrupt
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
signal.Notify(quit, os.Interrupt)
<-quit
s.Logger.Println("shutdown ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
return err
}
db, err := s.DB.DB()
if err != nil {
return err
}
s.Logger.Println("closing database ...")
if err := db.Close(); err != nil {
return err
}
s.Logger.Println("exiting")
return runError
}

View File

@ -0,0 +1,14 @@
package models
import (
"time"
)
type Account struct {
ID uint32 `gorm:"primary_key"`
Login string `gorm:"unique;size:64;not null"`
Password string `gorm:"-"`
EncryptedPassword string `gorm:"size:28;not null"`
CreatedAt time.Time
UpdatedAt time.Time
}

View File

@ -1,13 +0,0 @@
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
)
// GET /
func Root(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}

View File

@ -1,22 +0,0 @@
package main
import (
"flag"
"github.com/gin-gonic/gin"
"gitlab.celogeek.com/photos/api/src/controllers"
"gitlab.celogeek.com/photos/api/src/models"
)
func main() {
dbFilename := flag.String("db", "test.db", "database filename")
flag.Parse()
r := gin.Default()
models.ConnectDatabase(dbFilename)
r.GET("/", controllers.Root)
r.Run()
}

View File

@ -1,20 +0,0 @@
package models
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var DB *gorm.DB
func ConnectDatabase(filename *string) {
database, err := gorm.Open("sqlite3", *filename)
if err != nil {
panic("Failed to connect to database!")
}
database.AutoMigrate(&User{})
DB = database
}

View File

@ -1,7 +0,0 @@
package models
type User struct {
ID uint `json:"id" gorm:"primary_key"`
Login string `json:"login"`
Password string `json:"password"`
}

Binary file not shown.