github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/examples/library/main.go (about) 1 package main 2 3 import ( 4 "database/sql" 5 "flag" 6 "fmt" 7 "log" 8 "net" 9 10 "github.com/emcfarlane/larking" 11 "github.com/emcfarlane/larking/examples/library/apipb" 12 _ "modernc.org/sqlite" 13 ) 14 15 // Server implement LibraryServer. 16 // TODO: add all implemented methods to this struct. 17 type Server struct { 18 apipb.UnimplementedLibraryServer 19 20 db *sql.DB 21 } 22 23 var ( 24 flagPort = flag.String("port", "8000", "Port") 25 ) 26 27 func run() error { 28 flag.Parse() 29 30 db, err := sql.Open("sqlite", "sqlite:file::memory:?cache=shared") 31 if err != nil { 32 return err 33 } 34 defer db.Close() 35 if err := createTables(db); err != nil { 36 return err 37 } 38 39 s := &Server{db: db} 40 41 mux, err := larking.NewMux() 42 if err != nil { 43 return err 44 } 45 mux.RegisterService(&apipb.Library_ServiceDesc, s) 46 47 svr, err := larking.NewServer(mux, 48 larking.InsecureServerOption(), 49 ) 50 if err != nil { 51 return err 52 } 53 54 l, err := net.Listen("tcp", fmt.Sprintf(":%s", *flagPort)) 55 if err != nil { 56 return err 57 } 58 log.Printf("listening on %s", l.Addr().String()) 59 return svr.Serve(l) 60 } 61 62 func main() { 63 if err := run(); err != nil { 64 log.Fatal(err) 65 } 66 }