main server
This commit is contained in:
1 parent
9138e7fddc
commit
2f06ef0188
4 files changed
+50
-11
No files matched your search
+2
-1
@@ -15,7 +15,8 @@ COPY --from=builder /src/javtube-server .
|
|||||||
|
|
||||||
ENV GIN_MODE=release
|
ENV GIN_MODE=release
|
||||||
ENV PORT=8080
|
ENV PORT=8080
|
||||||
|
ENV TOKEN=""
|
||||||
ENV DSN=""
|
ENV DSN=""
|
||||||
ENV AUTOMIGRATE=true
|
ENV AUTO_MIGRATE=1
|
||||||
|
|
||||||
CMD ["/javtube-server"]
|
CMD ["/javtube-server"]
|
||||||
+43
-10
@@ -1,36 +1,69 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
goflag "flag"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/javtube/javtube-sdk-go/engine"
|
"github.com/javtube/javtube-sdk-go/engine"
|
||||||
"github.com/javtube/javtube-sdk-go/route"
|
"github.com/javtube/javtube-sdk-go/route"
|
||||||
"github.com/javtube/javtube-sdk-go/route/validator"
|
"github.com/javtube/javtube-sdk-go/route/validator"
|
||||||
|
"github.com/peterbourgon/ff/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
var (
|
||||||
|
opts = new(options)
|
||||||
|
flag = goflag.NewFlagSet("", goflag.ExitOnError)
|
||||||
|
)
|
||||||
|
|
||||||
|
type options struct {
|
||||||
|
bind string
|
||||||
|
port string
|
||||||
|
token string
|
||||||
|
dsn string
|
||||||
|
autoMigrate bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// gin initiate
|
||||||
gin.DisableConsoleColor()
|
gin.DisableConsoleColor()
|
||||||
|
|
||||||
app, err := engine.New(&engine.Options{
|
// flag parsing
|
||||||
DSN: "",
|
flag.StringVar(&opts.bind, "bind", "", "Bind address of server")
|
||||||
Timeout: 30 * time.Second,
|
flag.StringVar(&opts.port, "port", "8080", "Port number of server")
|
||||||
})
|
flag.StringVar(&opts.token, "token", "", "Token to access server")
|
||||||
|
flag.StringVar(&opts.dsn, "dsn", "", "Database Service Name")
|
||||||
|
flag.BoolVar(&opts.autoMigrate, "auto-migrate", false, "Database auto migration")
|
||||||
|
ff.Parse(flag, os.Args[1:], ff.WithEnvVarNoPrefix())
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app, err := engine.New(&engine.Options{
|
||||||
|
DSN: opts.dsn,
|
||||||
|
Timeout: 2 * time.Minute,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := app.AutoMigrate(); err != nil {
|
if err = app.AutoMigrate(opts.autoMigrate); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
token := validator.Token("token")
|
var token validator.Validator
|
||||||
|
if opts.token != "" {
|
||||||
|
token = validator.Token(opts.token)
|
||||||
|
}
|
||||||
|
|
||||||
router := route.New(app, token)
|
var (
|
||||||
|
addr = net.JoinHostPort(opts.bind, opts.port)
|
||||||
if err := router.Run(); err != nil {
|
router = route.New(app, token)
|
||||||
|
)
|
||||||
|
if err = http.ListenAndServe(addr, router); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,7 @@ require (
|
|||||||
github.com/iancoleman/orderedmap v0.2.0
|
github.com/iancoleman/orderedmap v0.2.0
|
||||||
github.com/lib/pq v1.10.6
|
github.com/lib/pq v1.10.6
|
||||||
github.com/nlnwa/whatwg-url v0.1.1
|
github.com/nlnwa/whatwg-url v0.1.1
|
||||||
|
github.com/peterbourgon/ff/v3 v3.1.2
|
||||||
github.com/stretchr/testify v1.7.1
|
github.com/stretchr/testify v1.7.1
|
||||||
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2
|
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2
|
||||||
gorm.io/datatypes v1.0.6
|
gorm.io/datatypes v1.0.6
|
||||||
|
|||||||
@@ -230,6 +230,9 @@ github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3P
|
|||||||
github.com/nlnwa/whatwg-url v0.1.0/go.mod h1:L97nLsTBZQV+fZTyMl1z6RdDhqgGzZTMmrpTkZDEdts=
|
github.com/nlnwa/whatwg-url v0.1.0/go.mod h1:L97nLsTBZQV+fZTyMl1z6RdDhqgGzZTMmrpTkZDEdts=
|
||||||
github.com/nlnwa/whatwg-url v0.1.1 h1:ggC0QwxF7RBpBLIKgO3rpziqGYnzfwtH+f8UCA9JHuQ=
|
github.com/nlnwa/whatwg-url v0.1.1 h1:ggC0QwxF7RBpBLIKgO3rpziqGYnzfwtH+f8UCA9JHuQ=
|
||||||
github.com/nlnwa/whatwg-url v0.1.1/go.mod h1:b0r+dEyM/KztLMDSVY6ApcO9Fmzgq+e9+Ugq20UBYck=
|
github.com/nlnwa/whatwg-url v0.1.1/go.mod h1:b0r+dEyM/KztLMDSVY6ApcO9Fmzgq+e9+Ugq20UBYck=
|
||||||
|
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
|
||||||
|
github.com/peterbourgon/ff/v3 v3.1.2 h1:0GNhbRhO9yHA4CC27ymskOsuRpmX0YQxwxM9UPiP6JM=
|
||||||
|
github.com/peterbourgon/ff/v3 v3.1.2/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
|
||||||
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
|
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
|
||||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
@@ -414,6 +417,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
|
|||||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||||
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
|
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
|||||||
Reference in new issue
Block a user