2021-09-28 23:57:59 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-12-19 15:30:21 +07:00
|
|
|
"io"
|
2021-12-22 14:38:22 +07:00
|
|
|
"log"
|
2021-12-19 15:30:21 +07:00
|
|
|
"net"
|
2021-09-28 23:57:59 +07:00
|
|
|
"os"
|
2022-04-28 23:31:09 +07:00
|
|
|
"time"
|
2021-09-28 23:57:59 +07:00
|
|
|
|
2021-12-19 15:30:21 +07:00
|
|
|
"github.com/hhhapz/codequest/api"
|
|
|
|
"github.com/hhhapz/codequest/db"
|
2021-12-22 14:38:22 +07:00
|
|
|
"github.com/hhhapz/codequest/models"
|
2021-09-28 23:57:59 +07:00
|
|
|
"github.com/peterbourgon/ff/v3"
|
2021-12-19 15:30:21 +07:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2021-12-22 14:38:22 +07:00
|
|
|
|
|
|
|
// questions
|
|
|
|
_ "github.com/hhhapz/codequest/question/q01"
|
|
|
|
_ "github.com/hhhapz/codequest/question/q02"
|
2022-04-28 23:31:09 +07:00
|
|
|
_ "github.com/hhhapz/codequest/question/q03"
|
2022-04-29 06:56:26 +07:00
|
|
|
_ "github.com/hhhapz/codequest/question/q04"
|
|
|
|
_ "github.com/hhhapz/codequest/question/q05"
|
2021-09-28 23:57:59 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func run() error {
|
2021-12-19 15:30:21 +07:00
|
|
|
fs := flag.NewFlagSet("codequest", flag.ExitOnError)
|
|
|
|
port := fs.Int("port", 10000, "GRPC Server Port")
|
2021-09-28 23:57:59 +07:00
|
|
|
secretFile := fs.String("secret", "client.secret.json", "Path to google oauth2 secret credentials JSON file.")
|
2022-04-28 23:31:09 +07:00
|
|
|
redirect := fs.String("redirect", "http://playcode.quest/authorize", "redirect uri")
|
2021-09-28 23:57:59 +07:00
|
|
|
dbFile := fs.String("db", "db.sqlite", "Path to sqlite3 database file")
|
2021-12-23 03:09:24 +07:00
|
|
|
debug := fs.Bool("debug", false, "debug sql queries")
|
2022-04-28 23:31:09 +07:00
|
|
|
|
|
|
|
start := fs.Int64("start", -1, "competition start time")
|
|
|
|
end := fs.Int64("end", -1, "competition start time")
|
|
|
|
|
|
|
|
if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("CQ")); err != nil {
|
2021-09-28 23:57:59 +07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-28 23:31:09 +07:00
|
|
|
api.Start = time.Unix(*start, 0)
|
|
|
|
api.End = time.Unix(*end, 0)
|
|
|
|
|
|
|
|
fmt.Println("start:", api.Start)
|
|
|
|
fmt.Println("end:", api.End)
|
|
|
|
|
2021-09-28 23:57:59 +07:00
|
|
|
if *secretFile == "" {
|
|
|
|
return fmt.Errorf("location to secret credentials file not provided.\nTry -h")
|
|
|
|
}
|
|
|
|
|
|
|
|
database, err := db.NewDB(*dbFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open db: %v", err)
|
|
|
|
}
|
2021-12-23 03:09:24 +07:00
|
|
|
if *debug {
|
|
|
|
models.SetLogger(log.Printf)
|
|
|
|
}
|
2021-09-28 23:57:59 +07:00
|
|
|
|
2022-04-28 23:31:09 +07:00
|
|
|
oaStore, err := db.NewOAuthState(*secretFile, *redirect)
|
2021-09-28 23:57:59 +07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create oauth config: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-28 23:31:09 +07:00
|
|
|
cdStore := db.NewCooldowns()
|
|
|
|
|
|
|
|
server, err := api.NewServer(oaStore, database, database, cdStore)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create server: %w", err)
|
|
|
|
}
|
2021-09-28 23:57:59 +07:00
|
|
|
|
2021-12-19 15:30:21 +07:00
|
|
|
log := grpclog.NewLoggerV2(os.Stderr, io.Discard, os.Stderr)
|
|
|
|
grpclog.SetLoggerV2(log)
|
2021-09-28 23:57:59 +07:00
|
|
|
|
2021-12-19 15:30:21 +07:00
|
|
|
addr := fmt.Sprintf("0.0.0.0:%d", *port)
|
|
|
|
lis, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to listen: %v", err)
|
2021-09-28 23:57:59 +07:00
|
|
|
}
|
|
|
|
|
2021-12-19 15:30:21 +07:00
|
|
|
log.Info("Serving gRPC on http://", addr)
|
|
|
|
return server.Serve(lis)
|
2021-09-28 23:57:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := run(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|