forked from tildetown/bbj2
		
	start adding db stuff
This commit is contained in:
		
							parent
							
								
									9549c1cd22
								
							
						
					
					
						commit
						75c3e67f41
					
				| @ -2,6 +2,7 @@ package main | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"database/sql" | 	"database/sql" | ||||||
|  | 	_ "embed" | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"flag" | 	"flag" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| @ -12,6 +13,9 @@ import ( | |||||||
| 	_ "github.com/mattn/go-sqlite3" | 	_ "github.com/mattn/go-sqlite3" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | //go:embed schema.sql | ||||||
|  | var schemaSQL string | ||||||
|  | 
 | ||||||
| type Config struct { | type Config struct { | ||||||
| 	Admins       []string | 	Admins       []string | ||||||
| 	Port         int | 	Port         int | ||||||
| @ -88,6 +92,11 @@ func _main(opts *Opts) error { | |||||||
| 	} | 	} | ||||||
| 	defer teardown() | 	defer teardown() | ||||||
| 
 | 
 | ||||||
|  | 	err = ensureSchema(*opts, "1.0.0") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	setupAPI(*opts) | 	setupAPI(*opts) | ||||||
| 
 | 
 | ||||||
| 	// TODO TLS or SSL or something | 	// TODO TLS or SSL or something | ||||||
| @ -99,6 +108,17 @@ func _main(opts *Opts) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func ensureSchema(opts Opts, version string) error { | ||||||
|  | 	// TODO make idempotent | ||||||
|  | 	// TODO actually respect version | ||||||
|  | 	_, err := opts.DB.Exec(schemaSQL) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return fmt.Errorf("failed to initialize database schema: %w", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func handler(opts Opts, f http.HandlerFunc) http.HandlerFunc { | func handler(opts Opts, f http.HandlerFunc) http.HandlerFunc { | ||||||
| 	// TODO make this more real | 	// TODO make this more real | ||||||
| 	return func(w http.ResponseWriter, req *http.Request) { | 	return func(w http.ResponseWriter, req *http.Request) { | ||||||
|  | |||||||
							
								
								
									
										37
									
								
								server/cmd/schema.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								server/cmd/schema.sql
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,37 @@ | |||||||
|  | create table meta ( | ||||||
|  |   version text      -- schema version | ||||||
|  | ); | ||||||
|  | 
 | ||||||
|  | create table users ( | ||||||
|  |   user_id text,     -- string (uuid1) | ||||||
|  |   user_name text,   -- string | ||||||
|  |   auth_hash text,   -- string (sha256 hash) | ||||||
|  |   quip text,        -- string (possibly empty) | ||||||
|  |   bio text,         -- string (possibly empty) | ||||||
|  |   color int,        -- int (from 0 to 6) | ||||||
|  |   is_admin int,     -- bool | ||||||
|  |   created real      -- floating point unix timestamp (when this user registered) | ||||||
|  | ); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | create table threads ( | ||||||
|  |   thread_id text,   -- uuid string | ||||||
|  |   author text,      -- string (uuid1, user.user_id) | ||||||
|  |   title text,       -- string | ||||||
|  |   last_mod real,    -- floating point unix timestamp (of last post or post edit) | ||||||
|  |   created real,     -- floating point unix timestamp (when thread was made) | ||||||
|  |   reply_count int,  -- integer (incremental, starting with 0) | ||||||
|  |   pinned int,       -- boolean | ||||||
|  |   last_author text  -- uuid string | ||||||
|  | ); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | create table messages ( | ||||||
|  |   thread_id text,   -- string (uuid1 of parent thread) | ||||||
|  |   post_id int,      -- integer (incrementing from 1) | ||||||
|  |   author text,      -- string (uuid1, user.user_id) | ||||||
|  |   created real,     -- floating point unix timestamp (when reply was posted) | ||||||
|  |   edited int,       -- bool | ||||||
|  |   body text,        -- string | ||||||
|  |   send_raw int      -- bool (1/true == never apply formatting) | ||||||
|  | ); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user