title: go rest api server design good practice
body: <p>I am new to golang and would like to make a small web app of to-do list to polish my go skills. I would like to know what is a good practice to organize the code.</p>
  
  <p>Right now, I define two packages for this project: <code>ticket</code> and <code>server</code>. <code>ticket</code> is about database and <code>server</code> is about http handlers.</p>
  
  <p>My data has two types: Ticket and Todo. One Ticket can have multiple Todo in it. They are defined in the <code>ticket</code> package.</p>
  
  <pre><code>type Ticket struct {                                                               
      Id          int64      `db:"id" json:"id"`                                     
      Label       string     `db:"label" json:"label"`                               
      Description string     `db:"description" json:"description"`                   
      StartTime   time.Time  `db:"start_time" json:"start_time"`                     
      EndTime     *time.Time `db:"end_time" json:"end_time"`                         
      Priority    bool       `db:"priority" json:"priority"`                         
  }                                                                                  
  type Todo struct {                                                                 
      Id       int64 `db:"id" json:"id"`                                             
      Item     int64 `db:"item" json:"item"`                                         
      TicketId int64 `db:"ticket_id" json:"ticket_id"`                               
      Active   bool  `db:"active" json:"active"`                                     
  }  
  </code></pre>
  
  <p>In the <code>ticket</code> package, I also define </p>
  
  <pre><code>type AppDB struct {                                                                
      db *sqlx.DB                                                                    
  }   
  </code></pre>
  
  <p>This wrapping around <code>*sqlx.DB</code> allows me to put all the database access functions inside <code>ticket</code> package, e.g.,</p>
  
  <pre><code>func (adb *AppDB) MustInit() 
  func (adb *AppDB) AddTicket(i *Ticket) (int64, error)
  </code></pre>
  
  <p>In the <code>server</code> package, I define</p>
  
  <pre><code>type Application struct {                                                          
      db ticket.AppDB                                                                
  }   
  </code></pre>
  
  <p>And the http handler functions are defined as methods of <code>Application</code>, e.g.,</p>
  
  <pre><code>func (app *Application) CreateTicket(w http.ResponseWriter, req *http.Request)
  </code></pre>
  
  <p>In the <code>main.go</code>, I register the handle functions.</p>
  
  <pre><code>func main() {                                                                      
      app := server.NewApplication()                                                 
  
      fmt.Println("now listening...")                                                
      router := mux.NewRouter()                                                      
      router.HandleFunc("/", app.Hello).Methods("GET")                               
      router.HandleFunc("/get", app.Get).Methods("GET")                              
      log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), router))                  
  }  
  </code></pre>
  
  <p>I have the following questions:</p>
  
  <ol>
  <li>Is this design a good practice? If not, what is the right way?</li>
  <li>In Donovan and Kernighan's Go programming book p.194, they give an example where the http handlers are defined as the database's method directly. Would that be a better design? It makes sense because my <code>server</code> package only works for data types in <code>ticket</code>. It also makes the code a little cleaner to write. On the other hand it mixes http and database, which I am not sure if it is a good thing.</li>
  <li>How to deal with database table creation? Shall I create another <code>main.go</code> which only creates the database tables and build it into executable such that I can run it on the server just once?</li>
  </ol>