grim/devweb

Initial revision, just a skeleton
draft
2020-06-30, Gary Kramlich
e85cdaf6e3d5
Parents
Children 4fa325daec62
Initial revision, just a skeleton
  • +4 -0
    .hgignore
  • +5 -0
    consts/consts.go
  • +34 -0
    db/db.go
  • +7 -0
    db/types.go
  • +12 -0
    go.mod
  • +31 -0
    go.sum
  • +72 -0
    main.go
  • +15 -0
    server/api.go
  • +37 -0
    server/server.go
  • +14 -0
    v1/books/book.go
  • +11 -0
    v1/books/books.go
  • +18 -0
    v1/books/mux.go
  • +15 -0
    v1/v1.go
  • --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/.hgignore Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,4 @@
    +syntax: glob
    +*.db
    +devweb
    +
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/consts/consts.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,5 @@
    +package consts
    +
    +const (
    + Version = "0.0.1dev"
    +)
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/db/db.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,34 @@
    +package db
    +
    +import (
    + "github.com/jmoiron/sqlx"
    + _ "github.com/mattn/go-sqlite3"
    +)
    +
    +var (
    + _db *sqlx.DB
    +)
    +
    +func Setup(driver, options string) error {
    + db, err := sqlx.Connect(driver, options)
    + if err != nil {
    + return err
    + }
    +
    + err = db.Ping()
    + if err != nil {
    + return err
    + }
    +
    + _db = db
    +
    + return nil
    +}
    +
    +func Teardown() error {
    + return _db.Close()
    +}
    +
    +func Get() *sqlx.DB {
    + return _db
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/db/types.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,7 @@
    +package db
    +
    +type Book struct {
    + Name string `db:"name"`
    + Type string `db:"type"`
    + Version string `db:"version"`
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/go.mod Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,12 @@
    +module keep.imfreedom.org/grim/devweb
    +
    +go 1.14
    +
    +require (
    + github.com/alecthomas/kingpin v2.2.6+incompatible
    + github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
    + github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
    + github.com/gorilla/mux v1.7.4
    + github.com/jmoiron/sqlx v1.2.0
    + github.com/mattn/go-sqlite3 v1.14.0
    +)
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/go.sum Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,31 @@
    +github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
    +github.com/alecthomas/kingpin v1.3.7 h1:GLMgiQ5nZb3rg5pozvF7KDbseQ12eNO1p7bB2t5K1fw=
    +github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvrbUHiqye8wRJMlnYI=
    +github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
    +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
    +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
    +github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
    +github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
    +github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
    +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
    +github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
    +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
    +github.com/gorilla/mux v1.7.4/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.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
    +github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
    +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
    +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
    +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
    +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
    +golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
    +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
    +golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
    +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
    +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
    +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
    +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
    +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/main.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,72 @@
    +package main
    +
    +import (
    + "fmt"
    + "os"
    + "os/signal"
    + "syscall"
    +
    + "github.com/alecthomas/kingpin"
    +
    + "keep.imfreedom.org/grim/devweb/consts"
    + "keep.imfreedom.org/grim/devweb/db"
    + "keep.imfreedom.org/grim/devweb/server"
    +)
    +
    +var (
    + app = kingpin.New("devweb", "A web based documentation viewer").Version(consts.Version)
    + listenAddr = app.Flag("listen-addr", "Address to listen on").Default(":1234").Envar("LISTEN_ADDR").String()
    +
    + db_engine = app.Flag("database-engine", "The name of the database engine to use").Default("sqlite3").Envar("DB_ENGINE").String()
    + db_options = app.Flag("database-options", "The options to pass to the database engine").Default("./webdev.db").Envar("DB_OPTIONS").String()
    +)
    +
    +func main() {
    + var err error
    +
    + _, err = app.Parse(os.Args[1:])
    + if err != nil {
    + fmt.Printf("%v\n", err)
    +
    + os.Exit(1)
    + }
    +
    + err = db.Setup(*db_engine, *db_options)
    + if err != nil {
    + fmt.Printf("%v\n", err)
    +
    + os.Exit(1)
    + }
    + defer db.Teardown()
    +
    + sigChan := make(chan os.Signal, 1)
    + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
    +
    + errChan := make(chan error, 10)
    +
    + server := server.NewServer(*listenAddr)
    + defer server.Close()
    +
    + go func() {
    + if err := server.Listen(); err != nil {
    + errChan <- err
    + }
    + }()
    +
    + for {
    + select {
    + case err := <-errChan:
    + ec := 0
    +
    + if err != nil {
    + fmt.Printf("error: %v\n", err)
    + ec = 1
    + }
    +
    + os.Exit(ec)
    + case s := <-sigChan:
    + fmt.Printf("Captured signal %v. Exiting...\n", s)
    + errChan <- nil
    + }
    + }
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/server/api.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,15 @@
    +package server
    +
    +import (
    + "net/http"
    +
    + "keep.imfreedom.org/grim/devweb/v1"
    +)
    +
    +func APIMux() http.Handler {
    + m := http.NewServeMux()
    +
    + m.Handle("/v1/", http.StripPrefix("/v1", v1.Mux()))
    +
    + return m
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/server/server.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,37 @@
    +package server
    +
    +import (
    + "fmt"
    + "net/http"
    +)
    +
    +type Server struct {
    + listenAddr string
    +
    + server *http.Server
    +}
    +
    +func NewServer(listenAddr string) *Server {
    + return &Server{
    + listenAddr: listenAddr,
    + server: &http.Server{
    + Addr: listenAddr,
    + },
    + }
    +}
    +
    +func (s *Server) Listen() error {
    + mux := http.NewServeMux()
    +
    + mux.Handle("/api/", http.StripPrefix("/api", APIMux()))
    +
    + s.server.Handler = mux
    +
    + fmt.Printf("Listening on %s\n", s.listenAddr)
    +
    + return s.server.ListenAndServe()
    +}
    +
    +func (s *Server) Close() error {
    + return s.server.Close()
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/v1/books/book.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,14 @@
    +package books
    +
    +import (
    + "net/http"
    +)
    +
    +func getBook(w http.ResponseWriter, r *http.Request) {
    +}
    +
    +func updateBook(w http.ResponseWriter, r *http.Request) {
    +}
    +
    +func deleteBook(w http.ResponseWriter, r *http.Request) {
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/v1/books/books.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,11 @@
    +package books
    +
    +import (
    + "net/http"
    +)
    +
    +func getBooks(w http.ResponseWriter, r *http.Request) {
    +}
    +
    +func createBook(w http.ResponseWriter, r *http.Request) {
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/v1/books/mux.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,18 @@
    +package books
    +
    +import (
    + "github.com/gorilla/mux"
    + "net/http"
    +)
    +
    +func Mux() http.Handler {
    + r := mux.NewRouter()
    +
    + r.HandleFunc("/{id}", getBook).Methods("GET")
    + r.HandleFunc("/{id}", updateBook).Methods("PUT")
    + r.HandleFunc("/{id}", deleteBook).Methods("DELETE")
    + r.HandleFunc("/", getBooks).Methods("GET")
    + r.HandleFunc("/", createBook).Methods("POST")
    +
    + return r
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/v1/v1.go Tue Jun 30 05:48:37 2020 -0500
    @@ -0,0 +1,15 @@
    +package v1
    +
    +import (
    + "net/http"
    +
    + "keep.imfreedom.org/grim/devweb/v1/books"
    +)
    +
    +func Mux() http.Handler {
    + m := http.NewServeMux()
    +
    + m.Handle("/books/", http.StripPrefix("/books", books.Mux()))
    +
    + return m
    +}