Skip to content

Commit 46eefca

Browse files
committed
MVP functionality
0 parents  commit 46eefca

8 files changed

Lines changed: 827 additions & 0 deletions

File tree

.air.toml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
root = "."
2+
testdata_dir = "testdata"
3+
tmp_dir = "tmp"
4+
5+
[build]
6+
args_bin = []
7+
bin = "./tmp/main"
8+
cmd = "go build -o ./tmp/main ."
9+
delay = 1000
10+
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
11+
exclude_file = []
12+
exclude_regex = ["_test.go"]
13+
exclude_unchanged = false
14+
follow_symlink = false
15+
full_bin = ""
16+
include_dir = []
17+
include_ext = ["go", "tpl", "tmpl", "html"]
18+
include_file = []
19+
kill_delay = "0s"
20+
log = "build-errors.log"
21+
poll = false
22+
poll_interval = 0
23+
post_cmd = []
24+
pre_cmd = []
25+
rerun = false
26+
rerun_delay = 500
27+
send_interrupt = false
28+
stop_on_error = false
29+
30+
[color]
31+
app = ""
32+
build = "yellow"
33+
main = "magenta"
34+
runner = "green"
35+
watcher = "cyan"
36+
37+
[log]
38+
main_only = false
39+
time = false
40+
41+
[misc]
42+
clean_on_exit = false
43+
44+
[screen]
45+
clear_on_rebuild = false
46+
keep_scroll = true

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with "go test -c"
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
# Go workspace file
18+
go.work
19+
tmp/
20+
21+
# IDE specific files
22+
.vscode
23+
.idea
24+
25+
# .env file
26+
.env
27+
28+
# Project build
29+
main
30+
*templ.go
31+
32+
# OS X generated file
33+
.DS_Store

examples/chi/chi.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/go-chi/chi/v5"
10+
"github.com/go-chi/chi/v5/middleware"
11+
"github.com/go-chi/cors"
12+
"github.com/joelseq/sqliteadmin-go"
13+
_ "github.com/mattn/go-sqlite3"
14+
)
15+
16+
const addr string = ":8080"
17+
18+
func main() {
19+
db, err := sql.Open("sqlite3", "test.db")
20+
if err != nil {
21+
log.Fatalf("Error opening database: %v", err)
22+
}
23+
sh := sqliteadmin.NewHandler(db, "user", "password")
24+
25+
r := chi.NewRouter()
26+
r.Use(middleware.Logger)
27+
r.Use(cors.Handler(cors.Options{
28+
AllowedOrigins: []string{"https://*", "http://*"},
29+
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
30+
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
31+
AllowCredentials: true,
32+
MaxAge: 300,
33+
}))
34+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
35+
w.Write([]byte("welcome"))
36+
})
37+
// Setup the handler for SQLiteAdmin
38+
r.Post("/admin", sh.Handle)
39+
40+
fmt.Printf(`
41+
42+
_______ __ __ __ _______ __ __
43+
| __|.-----.| |__| |_.-----.| _ |.--| |.--------.|__|.-----.
44+
|__ || _ || | | _| -__|| || _ || || || |
45+
|_______||__ ||__|__|____|_____||___|___||_____||__|__|__||__||__|__|
46+
|__|
47+
48+
`)
49+
fmt.Printf("--> Starting server on %s\n", addr)
50+
http.ListenAndServe(":8080", r)
51+
}

examples/stdlib/stdlib.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
"os/signal"
10+
"syscall"
11+
"time"
12+
13+
"github.com/joelseq/sqliteadmin-go"
14+
_ "github.com/mattn/go-sqlite3"
15+
"github.com/rs/cors"
16+
// "github.com/rs/cors"
17+
)
18+
19+
func main() {
20+
db, err := sql.Open("sqlite3", "test.db")
21+
if err != nil {
22+
log.Fatalf("Error opening database: %v", err)
23+
}
24+
sHandler := sqliteadmin.NewHandler(db, "user", "password")
25+
26+
mux := http.NewServeMux()
27+
28+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
29+
w.Write([]byte("Hello, World!"))
30+
})
31+
32+
mux.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
33+
if r.Method == "POST" {
34+
sHandler.Handle(w, r)
35+
}
36+
})
37+
38+
handler := cors.Default().Handler(mux)
39+
40+
s := http.Server{
41+
Addr: ":8080",
42+
Handler: handler,
43+
}
44+
45+
// Create a done channel to signal when the shutdown is complete
46+
done := make(chan bool, 1)
47+
48+
// Run graceful shutdown in a separate goroutine
49+
go gracefulShutdown(&s, done)
50+
51+
fmt.Println("--> Server listening on port 8080")
52+
53+
err = s.ListenAndServe()
54+
if err != nil && err != http.ErrServerClosed {
55+
panic(fmt.Sprintf("http server error: %s", err))
56+
}
57+
58+
// Wait for the graceful shutdown to complete
59+
<-done
60+
log.Println("Graceful shutdown complete.")
61+
}
62+
63+
func gracefulShutdown(apiServer *http.Server, done chan bool) {
64+
// Create context that listens for the interrupt signal from the OS.
65+
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
66+
defer stop()
67+
68+
// Listen for the interrupt signal.
69+
<-ctx.Done()
70+
71+
log.Println("shutting down gracefully, press Ctrl+C again to force")
72+
73+
// The context is used to inform the server it has 5 seconds to finish
74+
// the request it is currently handling
75+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
76+
defer cancel()
77+
if err := apiServer.Shutdown(ctx); err != nil {
78+
log.Printf("Server forced to shutdown with error: %v", err)
79+
}
80+
81+
log.Println("Server exiting")
82+
83+
// Notify the main goroutine that the shutdown is complete
84+
done <- true
85+
}
86+
87+
// func enableCors(w http.ResponseWriter) {
88+
// w.Header().Set("Access-Control-Allow-Origin", "*")
89+
// w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
90+
// w.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type")
91+
// w.Header().Set("Access-Control-Allow-Credentials", "true")
92+
// }

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/joelseq/sqliteadmin-go
2+
3+
go 1.23.3
4+
5+
require (
6+
github.com/go-chi/chi/v5 v5.2.0 // indirect
7+
github.com/go-chi/cors v1.2.1 // indirect
8+
github.com/mattn/go-sqlite3 v1.14.24 // indirect
9+
github.com/mitchellh/mapstructure v1.5.0 // indirect
10+
github.com/rs/cors v1.11.1 // indirect
11+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/go-chi/chi/v5 v5.2.0 h1:Aj1EtB0qR2Rdo2dG4O94RIU35w2lvQSj6BRA4+qwFL0=
2+
github.com/go-chi/chi/v5 v5.2.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
3+
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
4+
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
5+
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
6+
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
7+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
8+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
9+
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
10+
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=

0 commit comments

Comments
 (0)