configurable sqlite dsn
This commit is contained in:
		
							
								
								
									
										27
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								main.go
									
									
									
									
									
								
							| @@ -20,35 +20,38 @@ import ( | ||||
| var ( | ||||
| 	addr   = os.Getenv("SHOTGUN_HTTP") | ||||
| 	public = os.Getenv("SHOTGUN_PUBLIC") | ||||
| 	dsn    = os.Getenv("SHOTGUN_DB") | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| 	sessiondb, err := sq.Open("sqlite", ":memory:") | ||||
| 	db, err := sq.Open("sqlite", dsn) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	sessions, err := sessiondb.Conn(context.Background()) | ||||
| 	if err != nil { | ||||
| 	if err := db.Ping(context.Background()); err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	if err := player.InitSessions(context.Background(), sessions); err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
| 	if err := player.InitUsers(context.Background(), sessions); err != nil { | ||||
| 		panic(err) | ||||
| 	if len(os.Args) > 1 && os.Args[1] == "init" { | ||||
| 		if err := player.InitSessions(context.Background(), db); err != nil { | ||||
| 			panic(err) | ||||
| 		} | ||||
| 		if err := player.InitUsers(context.Background(), db); err != nil { | ||||
| 			panic(err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 	s := Server{ | ||||
| 		l:        lobby.New(), | ||||
| 		creds:    sessions, | ||||
| 		sessions: sessions, | ||||
| 		creds:    db, | ||||
| 		sessions: db, | ||||
| 		pp:       map[player.ID]*websocket.Conn{}, | ||||
| 	} | ||||
|  | ||||
| 	r := chi.NewRouter() | ||||
| 	r.Post("/user/register", s.Register) | ||||
| 	r.Post("/user/login", s.Login) | ||||
| 	r.With(serve.WithSession(sessions)).Get("/user/me", s.Me) | ||||
| 	r.With(serve.WithSession(sessions)).Get("/queue", s.Queue) | ||||
| 	r.With(serve.WithSession(db)).Get("/user/me", s.Me) | ||||
| 	r.With(serve.WithSession(db)).Get("/queue", s.Queue) | ||||
| 	r.Method("GET", "/*", http.FileServer(http.Dir(public))) | ||||
| 	slog.Info("listening", "addr", addr, "public", public) | ||||
| 	http.ListenAndServe(addr, r) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user