-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a bare-bones outline of MySQL-based example personality and storage
- Loading branch information
Showing
5 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,4 @@ | |
# Please keep the list sorted. | ||
|
||
Al Cutter <[email protected]> <[email protected]> | ||
|
||
Roger Ng <[email protected]> <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2024 The Tessera authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// example-mysql is a simple personality showing how to use the Tessera MySQL storage implmentation. | ||
package main | ||
|
||
import ( | ||
"crypto/sha256" | ||
"database/sql" | ||
"flag" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
tessera "github.com/transparency-dev/trillian-tessera" | ||
"github.com/transparency-dev/trillian-tessera/storage/mysql" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
var ( | ||
mysqlURI = flag.String("mysql_uri", "user:password@tcp(db:3306)/tessera", "Connection string for a MySQL database") | ||
dbConnMaxLifetime = flag.Duration("db_conn_max_lifetime", 3*time.Minute, "") | ||
dbMaxOpenConns = flag.Int("db_max_open_conns", 64, "") | ||
dbMaxIdleConns = flag.Int("db_max_idle_conns", 64, "") | ||
listen = flag.String("listen", ":2024", "Address:port to listen on") | ||
) | ||
|
||
func main() { | ||
klog.InitFlags(nil) | ||
flag.Parse() | ||
|
||
db, err := sql.Open("mysql", *mysqlURI) | ||
if err != nil { | ||
klog.Exitf("Failed to connect to DB: %v", err) | ||
} | ||
db.SetConnMaxLifetime(*dbConnMaxLifetime) | ||
db.SetMaxOpenConns(*dbMaxOpenConns) | ||
db.SetMaxIdleConns(*dbMaxIdleConns) | ||
|
||
_, err = mysql.New(db) | ||
if err != nil { | ||
klog.Exitf("Failed to create new MySQL storage: %v", err) | ||
} | ||
|
||
http.HandleFunc("POST /add", func(w http.ResponseWriter, r *http.Request) { | ||
b, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
defer r.Body.Close() | ||
|
||
id := sha256.Sum256(b) | ||
_ = tessera.NewEntry(b, tessera.WithIdentity(id[:])) | ||
|
||
// TODO: Add entry to log and return assigned index. | ||
}) | ||
|
||
if err := http.ListenAndServe(*listen, http.DefaultServeMux); err != nil { | ||
klog.Exitf("ListenAndServe: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2024 The Tessera authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package mysql contains a MySQL-based storage implementation for Tessera. | ||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// Storage is a MySQL-based storage implementation for Tessera. | ||
type Storage struct { | ||
db *sql.DB | ||
} | ||
|
||
// New creates a new instance of the MySQL-based Storage. | ||
func New(db *sql.DB) (*Storage, error) { | ||
s := &Storage{ | ||
db: db, | ||
} | ||
if err := s.db.Ping(); err != nil { | ||
klog.Exitf("Failed to ping database: %v", err) | ||
} | ||
|
||
return s, nil | ||
} |