🎉 initial commit

This commit is contained in:
2021-01-13 23:39:00 +01:00
commit 8e9cbe797c
9 changed files with 161 additions and 0 deletions

9
go.mod Normal file
View File

@@ -0,0 +1,9 @@
module github.com/ldrogou/goauth20
go 1.15
require (
github.com/gorilla/mux v1.8.0
github.com/jmoiron/sqlx v1.2.0
github.com/mattn/go-sqlite3 v1.14.6
)

9
go.sum Normal file
View File

@@ -0,0 +1,9 @@
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=

BIN
goauth20 Executable file

Binary file not shown.

38
main.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
fmt.Println("OAuth RCA")
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s)\n", err)
os.Exit(1)
}
}
func run() error {
srv := newServer()
srv.store = &dbStore{}
err := srv.store.Open()
if err != nil {
return err
}
defer srv.store.Close()
http.HandleFunc("/", srv.serveHTTP)
port := 8080
log.Printf("servering http port %v", port)
err = http.ListenAndServe(":8080", nil)
if err != nil {
return err
}
return nil
}

14
middleware.go Normal file
View File

@@ -0,0 +1,14 @@
package main
import (
"log"
"net/http"
)
func logRequestMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
log.Printf("[%v] %v", r.Method, r.RequestURI)
next.ServeHTTP(rw, r)
}
}

19
routes.auth.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"fmt"
"net/http"
)
func (s *server) handleIndex() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "Welcome to Goflix")
}
}
func (s *server) handleRedirect() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
}
}

6
routes.go Normal file
View File

@@ -0,0 +1,6 @@
package main
func (s *server) routes() {
s.router.HandleFunc("/", s.handleIndex()).Methods("GET")
s.router.HandleFunc("/oauth/redirect", s.handleRedirect()).Methods("GET")
}

23
server.go Normal file
View File

@@ -0,0 +1,23 @@
package main
import (
"net/http"
"github.com/gorilla/mux"
)
type server struct {
router *mux.Router
store Store
}
func newServer() *server {
s := &server{
router: mux.NewRouter(),
}
return s
}
func (s *server) serveHTTP(rw http.ResponseWriter, r *http.Request) {
logRequestMiddleware(s.router.ServeHTTP).ServeHTTP(rw, r)
}

43
store.go Normal file
View File

@@ -0,0 +1,43 @@
package main
import (
"log"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
type Store interface {
Open() error
Close() error
}
type dbStore struct {
db *sqlx.DB
}
var schema = `
CREATE TABLE IF NOT EXISTS auth
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
access_token TEXT,
expire_in TEXT,
refreh_token TEXT
)
`
func (store *dbStore) Open() error {
db, err := sqlx.Connect("sqlite3", "auth.go")
if err != nil {
return err
}
log.Println("Connected db")
db.MustExec(schema)
store.db = db
return nil
}
func (store *dbStore) Close() error {
return store.db.Close()
}