Skip to content

Commit

Permalink
Add DB
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Jun 14, 2022
1 parent a5942bf commit 15c41c6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module hackathon-22-spring-16/backend
module github.com/hackathon-22-spring-16/backend

go 1.17

require github.com/labstack/echo/v4 v4.7.2
require (
github.com/jmoiron/sqlx v1.3.5
github.com/labstack/echo/v4 v4.7.2
)

require (
github.com/labstack/gommon v0.3.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/labstack/echo/v4 v4.7.2 h1:Kv2/p8OaQ+M6Ex4eGimg9b9e6icoxA42JSlOR3msKtI=
github.com/labstack/echo/v4 v4.7.2/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks=
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
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=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
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=
Expand Down
19 changes: 18 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
package main

import (
"fmt"
"net/http"
"os"

"github.com/labstack/echo/v4"

"github.com/hackathon-22-spring-16/backend/model"
)

func main() {
db, err := model.InitDB()

e := echo.New()
e.GET("/ping", func(c echo.Context) error {
return c.String(http.StatusOK, "pong")
})
e.Start(":80")
e.GET("/db-info", func(c echo.Context) error {
if err != nil {
return c.String(http.StatusInternalServerError, fmt.Sprintf("db error: %s", err))
}
return c.String(http.StatusOK, fmt.Sprintf("driver name: %s", db.DriverName()))
})

port := os.Getenv("PORT")
if port == "" {
port = ":3000"
}
e.Start(port)
}
42 changes: 42 additions & 0 deletions model/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package model

import (
"fmt"
"os"

"github.com/jmoiron/sqlx"
)

var (
db *sqlx.DB
)

func InitDB() (*sqlx.DB, error) {
user := os.Getenv("MARIADB_USERNAME")
if user == "" {
user = "root"
}

pass := os.Getenv("MARIADB_PASSWORD")
if pass == "" {
pass = "password"
}

host := os.Getenv("MARIADB_HOSTNAME")
if host == "" {
host = "localhost"
}

dbname := os.Getenv("MARIADB_DATABASE")
if dbname == "" {
dbname = "22hack16"
}

_db, err := sqlx.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", user, pass, host, dbname)+"?parseTime=True&loc=Asia%2FTokyo&charset=utf8mb4")
if err != nil {
return nil, err
}
db = _db

return db, err
}
3 changes: 3 additions & 0 deletions mysql/init/create_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP DATABASE IF EXISTS 22hack16;
CREATE DATABASE 22hack16;
USE 22hack16;

0 comments on commit 15c41c6

Please sign in to comment.