41 lines
910 B
Go
41 lines
910 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"log"
|
||
|
|
||
|
"gorm.io/driver/mysql"
|
||
|
"gorm.io/gorm"
|
||
|
"sketchlib.bing89.com/backend/api"
|
||
|
"sketchlib.bing89.com/backend/server"
|
||
|
)
|
||
|
|
||
|
func initDatabase(db *gorm.DB)error{
|
||
|
return db.AutoMigrate(&api.Doc{}, &api.Group{}, &api.User{})
|
||
|
}
|
||
|
|
||
|
func main(){
|
||
|
var uri, dir, addr string
|
||
|
var init bool
|
||
|
flag.BoolVar(&init, "init", false, "init database")
|
||
|
flag.StringVar(&uri, "uri", "root:Hello2022@tcp(192.168.0.134:3306)/sketchlib?charset=utf8mb4&parseTime=True&loc=Local", "db uri")
|
||
|
flag.StringVar(&dir, "dir", "/opt/sketchlib", "file dir")
|
||
|
flag.StringVar(&addr, "addr", "0.0.0.0:8080", "addr to listen")
|
||
|
flag.Parse()
|
||
|
db, err := gorm.Open(mysql.Open(uri), &gorm.Config{})
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
if init {
|
||
|
err = initDatabase(db)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
svr := server.NewServer(dir, db)
|
||
|
err = svr.Run(addr)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|