Skip to content

Instantly share code, notes, and snippets.

@Phalacrocorax
Last active December 18, 2020 13:50
Show Gist options
  • Save Phalacrocorax/ffe5610ce8a95a9f106c031dfe0aee07 to your computer and use it in GitHub Desktop.
Save Phalacrocorax/ffe5610ce8a95a9f106c031dfe0aee07 to your computer and use it in GitHub Desktop.
Go Template
package main
import (
"{project}/models"
"{project}/routers"
)
func main() {
router := routers.InitRouter()
// Connect to database
models.ConnectDatabase()
// s := &http.Server{
// Addr: fmt.Sprintf(":%d", setting.HTTPPort),
// Handler: router,
// ReadTimeout: setting.ReadTimeout,
// WriteTimeout: setting.WriteTimeout,
// MaxHeaderBytes: 1 << 20,
// }
// s.ListenAndServe()
router.Run(":5000")
}
package models
import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// DB instance
var DB *gorm.DB
// ConnectDatabase cadasomment
func ConnectDatabase() {
// https://github.com/jackc/pgx
dsn := "user={db_user} password={db_pass} DB.name={db_name} port=5432 sslmode=disable TimeZone=Asia/Tokyo"
database, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic("Failed to connect to database!")
}
// database.AutoMigrate(&Model{})
DB = database
}
wget https://gist.githubusercontent.com/Phalacrocorax/ffe5610ce8a95a9f106c031dfe0aee07/raw/fe662596996e5a9df721f06ef21ed9e1d5be5739/main.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment