Initial commit
Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
commit
3936ee1ef8
3 changed files with 41 additions and 0 deletions
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module wbrawner.com/twigs
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/go-sql-driver/mysql v1.6.0
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -0,0 +1,2 @@
|
|||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
34
server.go
Normal file
34
server.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package twigs
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func getEnvOrDefault(env string, defaultVal string) string {
|
||||
envVal := os.Getenv(env)
|
||||
if envVal == "" {
|
||||
return defaultVal
|
||||
} else {
|
||||
return envVal
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
dbName := getEnvOrDefault("TWIGS_DB_NAME", "budget")
|
||||
dbUser := getEnvOrDefault("TWIGS_DB_USER", "budget")
|
||||
dbPass := getEnvOrDefault("TWIGS_DB_PASS", "budget")
|
||||
dbHost := getEnvOrDefault("TWIGS_DB_HOST", "localhost")
|
||||
dbPort := getEnvOrDefault("TWIGS_DB_PORT", "3306")
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@%s:%s/%s", dbUser, dbPass, dbHost, dbPort, dbName))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database %s on %s", dbName, dbHost)
|
||||
}
|
||||
http.HandleFunc("/users", user)
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
Loading…
Reference in a new issue